Whamcloud - gitweb
LU-1538 tests: standardize test script init dne-part-1
[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 [ "$mds1_FSTYPE" = 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 [ "$mds1_FSTYPE" == ldiskfs ] &&
48         do_facet $SINGLEMDS "! $DEBUGFS -c -R supported_features |
49                 grep -q 'quota'"; then
50         skip_env "e2fsprogs doesn't support quota"
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 [ "$mds1_FSTYPE" = 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         [ "$mds1_FSTYPE" = 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
1138         if at_is_enabled; then
1139                 at_max_saved=$(at_max_get ost1)
1140                 at_max_set $TIMEOUT ost1
1141
1142                 # write to enforced ID ($TSTUSR) to exceed limit to make sure
1143                 # DQACQ is sent, which makes at_max to take effect
1144                 $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync \
1145                                                                 conv=notrunc
1146                 rm -f $TESTFILE
1147                 wait_delete_completed
1148         fi
1149
1150         sync; sync
1151         sync_all_data || true
1152
1153         #define QUOTA_DQACQ 601
1154         #define OBD_FAIL_PTLRPC_DROP_REQ_OPC 0x513
1155         lustre_fail mds 0x513 601
1156
1157         do_facet ost1 $LCTL set_param \
1158                         osd-*.$FSNAME-OST*.quota_slave.timeout=$((TIMEOUT / 2))
1159
1160         # write to un-enforced ID ($TSTUSR2) should succeed
1161         $RUNAS2 $DD of=$TESTFILE2 count=$LIMIT seek=1 oflag=sync conv=notrunc ||
1162                 error "write failure, expect success"
1163
1164         # write to enforced ID ($TSTUSR) in background, exceeding limit
1165         # to make sure DQACQ is sent
1166         $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync conv=notrunc &
1167         DDPID=$!
1168
1169         # watchdog timer uses a factor of 2
1170         echo "Sleep for $((TIMEOUT * 2 + 1)) seconds ..."
1171         sleep $((TIMEOUT * 2 + 1))
1172
1173         [ $at_max_saved -ne 0 ] && at_max_set $at_max_saved ost1
1174
1175         # write should be blocked and never finished
1176         if ! ps -p $DDPID  > /dev/null 2>&1; then
1177                 lustre_fail mds 0 0
1178                 error "write finished incorrectly!"
1179         fi
1180
1181         lustre_fail mds 0 0
1182
1183         # no watchdog is triggered
1184         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1185         watchdog=$(awk '/[Ss]ervice thread pid/ && /was inactive/ \
1186                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
1187         [ -z "$watchdog" ] || error "$watchdog"
1188
1189         rm -f $TMP/lustre-log-${TESTNAME}.log
1190
1191         # write should continue then fail with EDQUOT
1192         local count=0
1193         local c_size
1194         while [ true ]; do
1195                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1196                 if [ $count -ge 240 ]; then
1197                         quota_error u $TSTUSR "dd not finished in $count secs"
1198                 fi
1199                 count=$((count + 1))
1200                 if [ $((count % 30)) -eq 0 ]; then
1201                         c_size=$(stat -c %s $TESTFILE)
1202                         echo "Waiting $count secs. $c_size"
1203                         $SHOW_QUOTA_USER
1204                 fi
1205                 sleep 1
1206         done
1207
1208         cleanup_quota_test
1209 }
1210 run_test 6 "Test dropping acquire request on master"
1211
1212 # quota reintegration (global index)
1213 test_7a() {
1214         local TESTFILE=$DIR/$tdir/$tfile
1215         local LIMIT=20 # 20M
1216
1217         [ "$SLOW" = "no" ] && LIMIT=5
1218
1219         setup_quota_test || error "setup quota failed with $?"
1220         trap cleanup_quota_test EXIT
1221
1222         # make sure the system is clean
1223         local USED=$(getquota -u $TSTUSR global curspace)
1224         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1225
1226         # make sure no granted quota on ost1
1227         set_ost_qtype $QTYPE || error "enable ost quota failed"
1228         resetquota -u $TSTUSR
1229         set_ost_qtype "none" || error "disable ost quota failed"
1230
1231         local OSTUUID=$(ostuuid_from_index 0)
1232         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1233         [ $USED -ne 0 ] &&
1234                 error "limit($USED) on $OSTUUID for user $TSTUSR isn't 0"
1235
1236         # create test file
1237         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1238         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1239
1240         echo "Stop ost1..."
1241         stop ost1
1242
1243         echo "Enable quota & set quota limit for $TSTUSR"
1244         set_ost_qtype $QTYPE || error "enable ost quota failed"
1245         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1246                 error "set quota failed"
1247
1248         echo "Start ost1..."
1249         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1250         quota_init
1251
1252         wait_ost_reint $QTYPE || error "reintegration failed"
1253
1254         # hardlimit should have been fetched by slave during global
1255         # reintegration, write will exceed quota
1256         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1257                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1258
1259         rm -f $TESTFILE
1260         wait_delete_completed
1261         sync_all_data || true
1262         sleep 3
1263
1264         echo "Stop ost1..."
1265         stop ost1
1266
1267         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
1268                 error "clear quota failed"
1269
1270         echo "Start ost1..."
1271         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1272         quota_init
1273
1274         wait_ost_reint $QTYPE || error "reintegration failed"
1275
1276         # hardlimit should be cleared on slave during reintegration
1277         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync ||
1278                 quota_error u $TSTUSR "write error, but expect success"
1279
1280         cleanup_quota_test
1281 }
1282 run_test 7a "Quota reintegration (global index)"
1283
1284 # quota reintegration (slave index)
1285 test_7b() {
1286         local LIMIT="100G"
1287         local TESTFILE=$DIR/$tdir/$tfile
1288
1289         setup_quota_test || error "setup quota failed with $?"
1290         trap cleanup_quota_test EXIT
1291
1292         # make sure the system is clean
1293         local USED=$(getquota -u $TSTUSR global curspace)
1294         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1295
1296         # make sure no granted quota on ost1
1297         set_ost_qtype $QTYPE || error "enable ost quota failed"
1298         resetquota -u $TSTUSR
1299         set_ost_qtype "none" || error "disable ost quota failed"
1300
1301         local OSTUUID=$(ostuuid_from_index 0)
1302         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1303         [ $USED -ne 0 ] &&
1304                 error "limit($USED) on $OSTUUID for user $TSTUSR isn't 0"
1305
1306         # create test file
1307         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1308         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1309
1310         # consume some space to make sure the granted space will not
1311         # be released during reconciliation
1312         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1313                 error "consume space failure, expect success"
1314
1315         # define OBD_FAIL_QUOTA_EDQUOT 0xa02
1316         lustre_fail mds 0xa02
1317
1318         set_ost_qtype $QTYPE || error "enable ost quota failed"
1319         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1320                 error "set quota failed"
1321
1322         # ignore the write error
1323         $RUNAS $DD of=$TESTFILE count=1 seek=1 oflag=sync conv=notrunc
1324
1325         local old_used=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1326
1327         lustre_fail mds 0
1328
1329         echo "Restart ost to trigger reintegration..."
1330         stop ost1
1331         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1332         quota_init
1333
1334         wait_ost_reint $QTYPE || error "reintegration failed"
1335
1336         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1337         [ $USED -gt $old_used ] || error "limit on $OSTUUID $USED <= $old_used"
1338
1339         cleanup_quota_test
1340         $SHOW_QUOTA_USER
1341 }
1342 run_test 7b "Quota reintegration (slave index)"
1343
1344 # quota reintegration (restart mds during reintegration)
1345 test_7c() {
1346         local LIMIT=20 # 20M
1347         local TESTFILE=$DIR/$tdir/$tfile
1348
1349         [ "$SLOW" = "no" ] && LIMIT=5
1350
1351         setup_quota_test || error "setup quota failed with $?"
1352         trap cleanup_quota_test EXIT
1353
1354         # make sure the system is clean
1355         local USED=$(getquota -u $TSTUSR global curspace)
1356         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1357
1358         set_ost_qtype "none" || error "disable ost quota failed"
1359         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1360                 error "set quota failed"
1361
1362         # define OBD_FAIL_QUOTA_DELAY_REINT 0xa03
1363         lustre_fail ost 0xa03
1364
1365         # enable ost quota
1366         set_ost_qtype $QTYPE || error "enable ost quota failed"
1367         # trigger reintegration
1368         local procf="osd-$ost1_FSTYPE.$FSNAME-OST*."
1369         procf=${procf}quota_slave.force_reint
1370         do_facet ost1 $LCTL set_param $procf=1 ||
1371                 error "force reintegration failed"
1372
1373         echo "Stop mds..."
1374         stop mds1
1375
1376         lustre_fail ost 0
1377
1378         echo "Start mds..."
1379         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
1380         quota_init
1381
1382         # wait longer than usual to make sure the reintegration
1383         # is triggered by quota wb thread.
1384         wait_ost_reint $QTYPE 200 || error "reintegration failed"
1385
1386         # hardlimit should have been fetched by slave during global
1387         # reintegration, write will exceed quota
1388         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1389                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1390
1391         cleanup_quota_test
1392 }
1393 run_test 7c "Quota reintegration (restart mds during reintegration)"
1394
1395 # Quota reintegration (Transfer index in multiple bulks)
1396 test_7d(){
1397         local TESTFILE=$DIR/$tdir/$tfile
1398         local TESTFILE1="$DIR/$tdir/$tfile"-1
1399         local limit=20 #20M
1400
1401         setup_quota_test || error "setup quota failed with $?"
1402         trap cleanup_quota_test EXIT
1403
1404         set_ost_qtype "none" || error "disable ost quota failed"
1405         $LFS setquota -u $TSTUSR -B ${limit}M $DIR ||
1406                 error "set quota for $TSTUSR failed"
1407         $LFS setquota -u $TSTUSR2 -B ${limit}M $DIR ||
1408                 error "set quota for $TSTUSR2 failed"
1409
1410         #define OBD_FAIL_OBD_IDX_READ_BREAK 0x608
1411         lustre_fail mds 0x608 0
1412
1413         # enable quota to tirgger reintegration
1414         set_ost_qtype "u" || error "enable ost quota failed"
1415         wait_ost_reint "u" || error "reintegration failed"
1416
1417         lustre_fail mds 0
1418
1419         # hardlimit should have been fetched by slave during global
1420         # reintegration, write will exceed quota
1421         $RUNAS $DD of=$TESTFILE count=$((limit + 1)) oflag=sync &&
1422                 quota_error u $TSTUSR "$TSTUSR write success, expect EDQUOT"
1423
1424         $RUNAS2 $DD of=$TESTFILE1 count=$((limit + 1)) oflag=sync &&
1425                 quota_error u $TSTUSR2 "$TSTUSR2 write success, expect EDQUOT"
1426
1427         cleanup_quota_test
1428 }
1429 run_test 7d "Quota reintegration (Transfer index in multiple bulks)"
1430
1431 # quota reintegration (inode limits)
1432 test_7e() {
1433         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
1434
1435         # LU-2435: skip this quota test if underlying zfs version has not
1436         # supported native dnode accounting
1437         [ "$mds1_FSTYPE" == zfs ] && {
1438                 local F="feature@userobj_accounting"
1439                 local pool=$(zpool_name mds1)
1440                 local feature=$(do_facet mds1 $ZPOOL get -H $F $pool)
1441
1442                 [[ "$feature" != *" active "* ]] &&
1443                         skip "requires zpool with active userobj_accounting"
1444         }
1445
1446         local ilimit=$((1024 * 2)) # 2k inodes
1447         local TESTFILE=$DIR/${tdir}-1/$tfile
1448
1449         setup_quota_test || error "setup quota failed with $?"
1450         trap cleanup_quota_test EXIT
1451
1452         # make sure the system is clean
1453         local USED=$(getquota -u $TSTUSR global curinodes)
1454         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
1455
1456         # make sure no granted quota on mdt1
1457         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1458         resetquota -u $TSTUSR
1459         set_mdt_qtype "none" || error "disable mdt quota failed"
1460
1461         local MDTUUID=$(mdtuuid_from_index $((MDSCOUNT - 1)))
1462         USED=$(getquota -u $TSTUSR $MDTUUID ihardlimit)
1463         [ $USED -ne 0 ] && error "limit($USED) on $MDTUUID for user" \
1464                 "$TSTUSR isn't 0."
1465
1466         echo "Stop mds${MDSCOUNT}..."
1467         stop mds${MDSCOUNT}
1468
1469         echo "Enable quota & set quota limit for $TSTUSR"
1470         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1471         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
1472                 error "set quota failed"
1473
1474         echo "Start mds${MDSCOUNT}..."
1475         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
1476         quota_init
1477
1478         wait_mdt_reint $QTYPE || error "reintegration failed"
1479
1480         echo "create remote dir"
1481         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/${tdir}-1 ||
1482                 error "create remote dir failed"
1483         chmod 0777 $DIR/${tdir}-1
1484
1485         # hardlimit should have been fetched by slave during global
1486         # reintegration, create will exceed quota
1487         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) &&
1488                 quota_error u $TSTUSR "create succeeded, expect EDQUOT"
1489
1490         $RUNAS unlinkmany $TESTFILE $ilimit || error "unlink files failed"
1491         wait_delete_completed
1492         sync_all_data || true
1493
1494         echo "Stop mds${MDSCOUNT}..."
1495         stop mds${MDSCOUNT}
1496
1497         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
1498                 error "clear quota failed"
1499
1500         echo "Start mds${MDSCOUNT}..."
1501         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
1502         quota_init
1503
1504         wait_mdt_reint $QTYPE || error "reintegration failed"
1505
1506         # hardlimit should be cleared on slave during reintegration
1507         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) ||
1508                 quota_error u $TSTUSR "create failed, expect success"
1509
1510         $RUNAS unlinkmany $TESTFILE $((ilimit + 1)) || error "unlink failed"
1511         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
1512
1513         cleanup_quota_test
1514 }
1515 run_test 7e "Quota reintegration (inode limits)"
1516
1517 # run dbench with quota enabled
1518 test_8() {
1519         local BLK_LIMIT="100g" #100G
1520         local FILE_LIMIT=1000000
1521
1522         setup_quota_test || error "setup quota failed with $?"
1523         trap cleanup_quota_test EXIT
1524
1525         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1526         set_ost_qtype $QTYPE || error "enable ost quota failed"
1527
1528         echo "Set enough high limit for user: $TSTUSR"
1529         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1530                 error "set user quota failed"
1531         echo "Set enough high limit for group: $TSTUSR"
1532         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1533                 error "set group quota failed"
1534         if is_project_quota_supported; then
1535                 change_project -sp $TSTPRJID $DIR/$tdir
1536                 echo "Set enough high limit for project: $TSTPRJID"
1537                 $LFS setquota -p $TSTPRJID -b 0 \
1538                         -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1539                         error "set project quota failed"
1540         fi
1541
1542         local duration=""
1543         [ "$SLOW" = "no" ] && duration=" -t 120"
1544         $RUNAS bash rundbench -D $DIR/$tdir 3 $duration ||
1545                 quota_error a $TSTUSR "dbench failed!"
1546
1547         is_project_quota_supported && change_project -C $DIR/$tdir
1548         cleanup_quota_test
1549 }
1550 run_test 8 "Run dbench with quota enabled"
1551
1552 # this check is just for test_9
1553 OST0_MIN=4900000 #4.67G
1554
1555 check_whether_skip () {
1556         local OST0_SIZE=$($LFS df $DIR | awk '/\[OST:0\]/ {print $4}')
1557         log "OST0_SIZE: $OST0_SIZE  required: $OST0_MIN"
1558         if [ $OST0_SIZE -lt $OST0_MIN ]; then
1559                 echo "WARN: OST0 has less than $OST0_MIN free, skip this test."
1560                 return 0
1561         else
1562                 return 1
1563         fi
1564 }
1565
1566 # run for fixing bug10707, it needs a big room. test for 64bit
1567 test_9() {
1568         local filesize=$((1024 * 9 / 2)) # 4.5G
1569
1570         check_whether_skip && return 0
1571
1572         setup_quota_test || error "setup quota failed with $?"
1573         trap cleanup_quota_test EXIT
1574
1575         set_ost_qtype "ug" || error "enable ost quota failed"
1576
1577         local TESTFILE="$DIR/$tdir/$tfile-0"
1578         local BLK_LIMIT=100G #100G
1579         local FILE_LIMIT=1000000
1580
1581         echo "Set block limit $BLK_LIMIT bytes to $TSTUSR.$TSTUSR"
1582
1583         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1584                 "for user: $TSTUSR"
1585         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1586                 error "set user quota failed"
1587
1588         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1589                 "for group: $TSTUSR"
1590         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1591                 error "set group quota failed"
1592
1593         quota_show_check a u $TSTUSR
1594         quota_show_check a g $TSTUSR
1595
1596         echo "Create test file"
1597         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1598         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1599
1600         log "Write the big file of 4.5G ..."
1601         $RUNAS $DD of=$TESTFILE count=$filesize ||
1602                 quota_error a $TSTUSR "write 4.5G file failure, expect success"
1603
1604         $SHOW_QUOTA_USER
1605         $SHOW_QUOTA_GROUP
1606
1607         cleanup_quota_test
1608
1609         $SHOW_QUOTA_USER
1610         $SHOW_QUOTA_GROUP
1611 }
1612 run_test 9 "Block limit larger than 4GB (b10707)"
1613
1614 test_10() {
1615         local TESTFILE=$DIR/$tdir/$tfile
1616
1617         setup_quota_test || error "setup quota failed with $?"
1618         trap cleanup_quota_test EXIT
1619
1620         # set limit to root user should fail
1621         $LFS setquota -u root -b 100G -B 500G -i 1K -I 1M $DIR &&
1622                 error "set limit for root user successfully, expect failure"
1623         $LFS setquota -g root -b 1T -B 10T -i 5K -I 100M $DIR &&
1624                 error "set limit for root group successfully, expect failure"
1625         $LFS setquota -p 0 -b 1T -B 10T -i 5K -I 100M $DIR &&
1626                 error "set limit for project 0 successfully, expect failure"
1627
1628         # root user can overrun quota
1629         set_ost_qtype "ug" || error "enable ost quota failed"
1630
1631         $LFS setquota -u $TSTUSR -b 0 -B 2M -i 0 -I 0 $DIR ||
1632                 error "set quota failed"
1633         quota_show_check b u $TSTUSR
1634
1635         $LFS setstripe $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
1636         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1637
1638         runas -u 0 -g 0 $DD of=$TESTFILE count=3 oflag=sync ||
1639                 error "write failure, expect success"
1640
1641         cleanup_quota_test
1642 }
1643 run_test 10 "Test quota for root user"
1644
1645 test_11() {
1646         local TESTFILE=$DIR/$tdir/$tfile
1647         setup_quota_test || error "setup quota failed with $?"
1648         trap cleanup_quota_test EXIT
1649
1650         set_mdt_qtype "ug" || error "enable mdt quota failed"
1651         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 1 $DIR ||
1652                 error "set quota failed"
1653
1654         touch "$TESTFILE"-0 || error "touch $TESTFILE-0 failed"
1655         touch "$TESTFILE"-1 || error "touch $TESTFILE-0 failed"
1656
1657         chown $TSTUSR.$TSTUSR "$TESTFILE"-0 || error "chown $TESTFILE-0 failed"
1658         chown $TSTUSR.$TSTUSR "$TESTFILE"-1 || error "chown $TESTFILE-1 failed"
1659
1660         $SHOW_QUOTA_USER
1661         local USED=$(getquota -u $TSTUSR global curinodes)
1662         [ $USED -ge 2 ] || error "Used inodes($USED) is less than 2"
1663
1664         cleanup_quota_test
1665 }
1666 run_test 11 "Chown/chgrp ignores quota"
1667
1668 test_12a() {
1669         [ "$OSTCOUNT" -lt "2" ] && skip "needs >= 2 OSTs"
1670
1671         local blimit=22 # 22M
1672         local blk_cnt=$((blimit - 5))
1673         local TESTFILE0="$DIR/$tdir/$tfile"-0
1674         local TESTFILE1="$DIR/$tdir/$tfile"-1
1675
1676         setup_quota_test || error "setup quota failed with $?"
1677         trap cleanup_quota_test EXIT
1678
1679         set_ost_qtype "u" || error "enable ost quota failed"
1680         quota_show_check b u $TSTUSR
1681
1682         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $DIR ||
1683                 error "set quota failed"
1684
1685         $LFS setstripe $TESTFILE0 -c 1 -i 0 || error "setstripe $TESTFILE0 failed"
1686         $LFS setstripe $TESTFILE1 -c 1 -i 1 || error "setstripe $TESTFILE1 failed"
1687         chown $TSTUSR.$TSTUSR $TESTFILE0 || error "chown $TESTFILE0 failed"
1688         chown $TSTUSR.$TSTUSR $TESTFILE1 || error "chown $TESTFILE1 failed"
1689
1690         echo "Write to ost0..."
1691         $RUNAS $DD of=$TESTFILE0 count=$blk_cnt oflag=sync ||
1692                 quota_error a $TSTUSR "dd failed"
1693
1694         echo "Write to ost1..."
1695         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync &&
1696                 quota_error a $TSTUSR "dd succeed, expect EDQUOT"
1697
1698         echo "Free space from ost0..."
1699         rm -f $TESTFILE0
1700         wait_delete_completed
1701         sync_all_data || true
1702
1703         echo "Write to ost1 after space freed from ost0..."
1704         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync ||
1705                 quota_error a $TSTUSR "rebalancing failed"
1706
1707         cleanup_quota_test
1708 }
1709 run_test 12a "Block quota rebalancing"
1710
1711 test_12b() {
1712         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
1713
1714         local ilimit=$((1024 * 2)) # 2k inodes
1715         local TESTFILE0=$DIR/$tdir/$tfile
1716         local TESTFILE1=$DIR/${tdir}-1/$tfile
1717
1718         setup_quota_test || error "setup quota failed with $?"
1719         trap cleanup_quota_test EXIT
1720
1721         $LFS mkdir -i 1 $DIR/${tdir}-1 || error "create remote dir failed"
1722         chmod 0777 $DIR/${tdir}-1
1723
1724         set_mdt_qtype "u" || error "enable mdt quota failed"
1725         quota_show_check f u $TSTUSR
1726
1727         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
1728                 error "set quota failed"
1729
1730         echo "Create $ilimit files on mdt0..."
1731         $RUNAS createmany -m $TESTFILE0 $ilimit ||
1732                 quota_error u $TSTUSR "create failed, but expect success"
1733
1734         echo "Create files on mdt1..."
1735         $RUNAS createmany -m $TESTFILE1 1 &&
1736                 quota_error a $TSTUSR "create succeeded, expect EDQUOT"
1737
1738         echo "Free space from mdt0..."
1739         $RUNAS unlinkmany $TESTFILE0 $ilimit || error "unlink mdt0 files failed"
1740         wait_delete_completed
1741         sync_all_data || true
1742
1743         echo "Create files on mdt1 after space freed from mdt0..."
1744         $RUNAS createmany -m $TESTFILE1 $((ilimit / 2)) ||
1745                 quota_error a $TSTUSR "rebalancing failed"
1746
1747         $RUNAS unlinkmany $TESTFILE1 $((ilimit / 2)) ||
1748                 error "unlink mdt1 files failed"
1749         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
1750
1751         cleanup_quota_test
1752 }
1753 run_test 12b "Inode quota rebalancing"
1754
1755 test_13(){
1756         local TESTFILE=$DIR/$tdir/$tfile
1757         # the name of lwp on ost1 name is MDT0000-lwp-OST0000
1758         local procf="ldlm.namespaces.*MDT0000-lwp-OST0000.lru_size"
1759
1760         setup_quota_test || error "setup quota failed with $?"
1761         trap cleanup_quota_test EXIT
1762
1763         set_ost_qtype "u" || error "enable ost quota failed"
1764         quota_show_check b u $TSTUSR
1765
1766         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
1767                 error "set quota failed"
1768         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1769         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1770
1771         # clear the locks in cache first
1772         do_facet ost1 $LCTL set_param -n $procf=clear
1773         local nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1774         [ $nlock -eq 0 ] || error "$nlock cached locks"
1775
1776         # write to acquire the per-ID lock
1777         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1778                 quota_error a $TSTUSR "dd failed"
1779
1780         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1781         [ $nlock -eq 1 ] || error "lock count($nlock) isn't 1"
1782
1783         # clear quota doesn't trigger per-ID lock cancellation
1784         resetquota -u $TSTUSR
1785         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1786         [ $nlock -eq 1 ] || error "per-ID lock is lost on quota clear"
1787
1788         # clear the per-ID lock
1789         do_facet ost1 $LCTL set_param -n $procf=clear
1790         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1791         [ $nlock -eq 0 ] || error "per-ID lock isn't cleared"
1792
1793         # spare quota should be released
1794         local OSTUUID=$(ostuuid_from_index 0)
1795         local limit=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1796         local space=$(getquota -u $TSTUSR $OSTUUID curspace)
1797         [ $limit -le $space ] ||
1798                 error "spare quota isn't released, limit:$limit, space:$space"
1799
1800         cleanup_quota_test
1801 }
1802 run_test 13 "Cancel per-ID lock in the LRU list"
1803
1804 test_15(){
1805         local LIMIT=$((24 * 1024 * 1024 * 1024 * 1024)) # 24 TB
1806
1807         wait_delete_completed
1808         sync_all_data || true
1809
1810         # test for user
1811         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1812                 error "set user quota failed"
1813         local TOTAL_LIMIT=$(getquota -u $TSTUSR global bhardlimit)
1814         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1815                 error "(user) limit:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1816         resetquota -u $TSTUSR
1817
1818         # test for group
1819         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1820                 error "set group quota failed"
1821         TOTAL_LIMIT=$(getquota -g $TSTUSR global bhardlimit)
1822         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1823                 error "(group) limits:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1824         resetquota -g $TSTUSR
1825 }
1826 run_test 15 "Set over 4T block quota"
1827
1828 test_17sub() {
1829         local err_code=$1
1830         local BLKS=1    # 1M less than limit
1831         local TESTFILE=$DIR/$tdir/$tfile
1832
1833         setup_quota_test || error "setup quota failed with $?"
1834         trap cleanup_quota_test EXIT
1835
1836         # make sure the system is clean
1837         local USED=$(getquota -u $TSTUSR global curspace)
1838         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1839
1840         set_ost_qtype "ug" || error "enable ost quota failed"
1841         # make sure no granted quota on ost
1842         resetquota -u $TSTUSR
1843         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
1844                 error "set quota failed"
1845
1846         quota_show_check b u $TSTUSR
1847
1848         #define OBD_FAIL_QUOTA_RECOVERABLE_ERR 0xa04
1849         lustre_fail mds 0xa04 $err_code
1850
1851         # write in background
1852         $RUNAS $DD of=$TESTFILE count=$BLKS oflag=direct &
1853         local DDPID=$!
1854
1855         sleep 2
1856         # write should be blocked and never finished
1857         if ! ps -p $DDPID  > /dev/null 2>&1; then
1858                 lustre_fail mds 0 0
1859                 quota_error u $TSTUSR "write finished incorrectly!"
1860         fi
1861
1862         lustre_fail mds 0 0
1863
1864         local count=0
1865         local timeout=30
1866         while [ true ]; do
1867                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1868                 count=$((count+1))
1869                 if [ $count -gt $timeout ]; then
1870                         quota_error u $TSTUSR "dd is not finished!"
1871                 fi
1872                 sleep 1
1873         done
1874
1875         sync; sync_all_data || true
1876
1877         USED=$(getquota -u $TSTUSR global curspace)
1878         [ $USED -ge $((BLKS * 1024)) ] || quota_error u $TSTUSR \
1879                 "Used space(${USED}K) is less than ${BLKS}M"
1880
1881         cleanup_quota_test
1882 }
1883
1884 # DQACQ return recoverable error
1885 test_17() {
1886         echo "DQACQ return -ENOLCK"
1887         #define ENOLCK  37
1888         test_17sub 37 || error "Handle -ENOLCK failed"
1889
1890         echo "DQACQ return -EAGAIN"
1891         #define EAGAIN  11
1892         test_17sub 11 || error "Handle -EAGAIN failed"
1893
1894         echo "DQACQ return -ETIMEDOUT"
1895         #define ETIMEDOUT 110
1896         test_17sub 110 || error "Handle -ETIMEDOUT failed"
1897
1898         echo "DQACQ return -ENOTCONN"
1899         #define ENOTCONN 107
1900         test_17sub 107 || error "Handle -ENOTCONN failed"
1901 }
1902
1903 run_test 17 "DQACQ return recoverable error"
1904
1905 test_18_sub () {
1906         local io_type=$1
1907         local blimit="200m" # 200M
1908         local TESTFILE="$DIR/$tdir/$tfile"
1909
1910         setup_quota_test || error "setup quota failed with $?"
1911         trap cleanup_quota_test EXIT
1912
1913         set_ost_qtype "u" || error "enable ost quota failed"
1914         log "User quota (limit: $blimit)"
1915         $LFS setquota -u $TSTUSR -b 0 -B $blimit -i 0 -I 0 $MOUNT ||
1916                 error "set quota failed"
1917         quota_show_check b u $TSTUSR
1918
1919         $LFS setstripe $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
1920         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1921
1922         local timeout=$(sysctl -n lustre.timeout)
1923
1924         if [ $io_type = "directio" ]; then
1925                 log "Write 100M (directio) ..."
1926                 $RUNAS $DD of=$TESTFILE count=100 oflag=direct &
1927         else
1928                 log "Write 100M (buffered) ..."
1929                 $RUNAS $DD of=$TESTFILE count=100 &
1930         fi
1931         local DDPID=$!
1932
1933         replay_barrier $SINGLEMDS
1934         log "Fail mds for $((2 * timeout)) seconds"
1935         fail $SINGLEMDS $((2 * timeout))
1936
1937         local count=0
1938         if at_is_enabled; then
1939                 timeout=$(at_max_get mds)
1940         else
1941                 timeout=$(lctl get_param -n timeout)
1942         fi
1943
1944         while [ true ]; do
1945                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1946                 if [ $((++count % (2 * timeout) )) -eq 0 ]; then
1947                         log "it took $count second"
1948                 fi
1949                 sleep 1
1950         done
1951
1952         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
1953         sync
1954         cancel_lru_locks mdc
1955         cancel_lru_locks osc
1956         $SHOW_QUOTA_USER
1957
1958         local testfile_size=$(stat -c %s $TESTFILE)
1959         if [ $testfile_size -ne $((BLK_SZ * 1024 * 100)) ] ; then
1960                 quota_error u $TSTUSR "expect $((BLK_SZ * 1024 * 100))," \
1961                         "got ${testfile_size}. Verifying file failed!"
1962         fi
1963         cleanup_quota_test
1964 }
1965
1966 # test when mds does failover, the ost still could work well
1967 # this test shouldn't trigger watchdog b=14840
1968 test_18() {
1969         # Clear dmesg so watchdog is not triggered by previous
1970         # test output
1971         do_facet ost1 dmesg -c > /dev/null
1972
1973         test_18_sub normal
1974         test_18_sub directio
1975
1976         # check if watchdog is triggered
1977         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1978         local watchdog=$(awk '/[Ss]ervice thread pid/ && /was inactive/ \
1979                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
1980         [ -z "$watchdog" ] || error "$watchdog"
1981         rm -f $TMP/lustre-log-${TESTNAME}.log
1982 }
1983 run_test 18 "MDS failover while writing, no watchdog triggered (b14840)"
1984
1985 test_19() {
1986         local blimit=5 # 5M
1987         local TESTFILE=$DIR/$tdir/$tfile
1988
1989         setup_quota_test || error "setup quota failed with $?"
1990         trap cleanup_quota_test EXIT
1991
1992         set_ost_qtype $QTYPE || error "enable ost quota failed"
1993
1994         # bind file to a single OST
1995         $LFS setstripe -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
1996         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1997
1998         echo "Set user quota (limit: ${blimit}M)"
1999         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2000                 error "set user quota failed"
2001         quota_show_check b u $TSTUSR
2002         echo "Update quota limits"
2003         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2004                 error "set group quota failed"
2005         quota_show_check b u $TSTUSR
2006
2007         # first wirte might be cached
2008         $RUNAS $DD of=$TESTFILE count=$((blimit + 1))
2009         cancel_lru_locks osc
2010         $SHOW_QUOTA_USER
2011         $RUNAS $DD of=$TESTFILE count=$((blimit + 1)) seek=$((blimit + 1)) &&
2012                 quota_error u $TSTUSR "Write success, expect failure"
2013         $SHOW_QUOTA_USER
2014
2015         cleanup_quota_test
2016 }
2017 run_test 19 "Updating admin limits doesn't zero operational limits(b14790)"
2018
2019 test_20() { # b15754
2020         local LSTR=(2g 1t 4k 3m) # limits strings
2021         # limits values
2022         local LVAL=($((2*1024*1024)) $((1*1024*1024*1024)) $((4*1024)) \
2023                     $((3*1024*1024)))
2024
2025         resetquota -u $TSTUSR
2026
2027         $LFS setquota -u $TSTUSR --block-softlimit ${LSTR[0]} \
2028                 $MOUNT || error "could not set quota limits"
2029         $LFS setquota -u $TSTUSR --block-hardlimit ${LSTR[1]} \
2030                                 --inode-softlimit ${LSTR[2]} \
2031                                 --inode-hardlimit ${LSTR[3]} \
2032                                 $MOUNT || error "could not set quota limits"
2033
2034         [ "$(getquota -u $TSTUSR global bsoftlimit)" = "${LVAL[0]}" ] ||
2035                 error "bsoftlimit was not set properly"
2036         [ "$(getquota -u $TSTUSR global bhardlimit)" = "${LVAL[1]}" ] ||
2037                 error "bhardlimit was not set properly"
2038         [ "$(getquota -u $TSTUSR global isoftlimit)" = "${LVAL[2]}" ] ||
2039                 error "isoftlimit was not set properly"
2040         [ "$(getquota -u $TSTUSR global ihardlimit)" = "${LVAL[3]}" ] ||
2041                 error "ihardlimit was not set properly"
2042
2043         resetquota -u $TSTUSR
2044 }
2045 run_test 20 "Test if setquota specifiers work properly (b15754)"
2046
2047 test_21_sub() {
2048         local testfile=$1
2049         local blk_number=$2
2050         local seconds=$3
2051
2052         local time=$(($(date +%s) + seconds))
2053         while [ $(date +%s) -lt $time ]; do
2054                 $RUNAS $DD of=$testfile count=$blk_number > /dev/null 2>&1
2055         done
2056 }
2057
2058 # run for fixing bug16053, setquota shouldn't fail when writing and
2059 # deleting are happening
2060 test_21() {
2061         local TESTFILE="$DIR/$tdir/$tfile"
2062         local BLIMIT=10 # 10G
2063         local ILIMIT=1000000
2064
2065         setup_quota_test || error "setup quota failed with $?"
2066         trap cleanup_quota_test EXIT
2067
2068         set_ost_qtype $QTYPE || error "Enable ost quota failed"
2069
2070         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for user: $TSTUSR"
2071         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}G -i 0 -I $ILIMIT $MOUNT ||
2072                 error "set user quota failed"
2073         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for group: $TSTUSR"
2074         $LFS setquota -g $TSTUSR -b 0 -B $BLIMIT -i 0 -I $ILIMIT $MOUNT ||
2075                 error "set group quota failed"
2076         if is_project_quota_supported; then
2077                 log "Set limit(block:${BLIMIT}G; file:$LIMIT) for " \
2078                         "project: $TSTPRJID"
2079                 $LFS setquota -p $TSTPRJID -b 0 -B $BLIMIT -i 0 -I $ILIMIT \
2080                          $MOUNT || error "set project quota failed"
2081         fi
2082
2083         # repeat writing on a 1M file
2084         test_21_sub ${TESTFILE}_1 1 30 &
2085         local DDPID1=$!
2086         # repeat writing on a 128M file
2087         test_21_sub ${TESTFILE}_2 128 30 &
2088         local DDPID2=$!
2089
2090         local time=$(($(date +%s) + 30))
2091         local i=1
2092         while [ $(date +%s) -lt $time ]; do
2093                 log "Set quota for $i times"
2094                 $LFS setquota -u $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
2095                         -I $((ILIMIT + i)) $MOUNT ||
2096                                 error "Set user quota failed"
2097                 $LFS setquota -g $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
2098                         -I $((ILIMIT + i)) $MOUNT ||
2099                                 error "Set group quota failed"
2100                 if is_project_quota_supported; then
2101                         $LFS setquota -p $TSTPRJID -b 0 -B \
2102                         "$((BLIMIT + i))G"  -i 0 -I $((ILIMIT + i)) $MOUNT ||
2103                                 error "Set project quota failed"
2104                 fi
2105                 i=$((i+1))
2106                 sleep 1
2107         done
2108
2109         local count=0
2110         while [ true ]; do
2111                 if ! ps -p ${DDPID1} > /dev/null 2>&1; then break; fi
2112                 count=$((count+1))
2113                 if [ $count -gt 60 ]; then
2114                         quota_error a $TSTUSR "dd should be finished!"
2115                 fi
2116                 sleep 1
2117         done
2118         echo "(dd_pid=$DDPID1, time=$count)successful"
2119
2120         count=0
2121         while [ true ]; do
2122                 if ! ps -p ${DDPID2} > /dev/null 2>&1; then break; fi
2123                 count=$((count+1))
2124                 if [ $count -gt 60 ]; then
2125                         quota_error a $TSTUSR "dd should be finished!"
2126                 fi
2127                 sleep 1
2128         done
2129         echo "(dd_pid=$DDPID2, time=$count)successful"
2130
2131         cleanup_quota_test
2132 }
2133 run_test 21 "Setquota while writing & deleting (b16053)"
2134
2135 # enable/disable quota enforcement permanently
2136 test_22() {
2137         echo "Set both mdt & ost quota type as ug"
2138         local qtype="ug"
2139         is_project_quota_supported && qtype=$QTYPE
2140         set_mdt_qtype $qtype || error "enable mdt quota failed"
2141         set_ost_qtype $qtype || error "enable ost quota failed"
2142
2143         echo "Restart..."
2144         stopall || error "failed to stopall (1)"
2145         mount
2146         setupall
2147
2148         echo "Verify if quota is enabled"
2149         local qtype1=$(mdt_quota_type)
2150         [ $qtype1 != $qtype ] && error "mdt quota setting is lost"
2151         qtype=$(ost_quota_type)
2152         [ $qtype1 != $qtype ] && error "ost quota setting is lost"
2153
2154         echo "Set both mdt & ost quota type as none"
2155         set_mdt_qtype "none" || error "disable mdt quota failed"
2156         set_ost_qtype "none" || error "disable ost quota failed"
2157
2158         echo "Restart..."
2159         stopall || error "failed to stopall (2)"
2160         mount
2161         setupall
2162         quota_init
2163
2164         echo "Verify if quota is disabled"
2165         qtype=$(mdt_quota_type)
2166         [ $qtype != "none" ] && error "mdt quota setting is lost"
2167         qtype=$(ost_quota_type)
2168         [ $qtype != "none" ] && error "ost quota setting is lost"
2169
2170         return 0
2171 }
2172 run_test 22 "enable/disable quota by 'lctl conf_param/set_param -P'"
2173
2174 test_23_sub() {
2175         local TESTFILE="$DIR/$tdir/$tfile"
2176         local LIMIT=$1
2177
2178         setup_quota_test || error "setup quota failed with $?"
2179         trap cleanup_quota_test EXIT
2180
2181         set_ost_qtype $QTYPE || error "Enable ost quota failed"
2182
2183         # test for user
2184         log "User quota (limit: $LIMIT MB)"
2185         $LFS setquota -u $TSTUSR -b 0 -B "$LIMIT"M -i 0 -I 0 $DIR ||
2186                 error "set quota failed"
2187         quota_show_check b u $TSTUSR
2188
2189         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
2190         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2191
2192         log "Step1: trigger EDQUOT with O_DIRECT"
2193         log "Write half of file"
2194         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) oflag=direct ||
2195                 quota_error u $TSTUSR "(1) Write failure, expect success." \
2196                         "limit=$LIMIT"
2197         log "Write out of block quota ..."
2198         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 + 1)) seek=$((LIMIT/2)) \
2199                 oflag=direct conv=notrunc &&
2200                 quota_error u $TSTUSR "(2) Write success, expect EDQUOT." \
2201                         "limit=$LIMIT"
2202         log "Step1: done"
2203
2204         log "Step2: rewrite should succeed"
2205         $RUNAS $DD of=$TESTFILE count=1 oflag=direct conv=notrunc||
2206                 quota_error u $TSTUSR "(3) Write failure, expect success." \
2207                         "limit=$LIMIT"
2208         log "Step2: done"
2209
2210         cleanup_quota_test
2211
2212         local OST0_UUID=$(ostuuid_from_index 0)
2213         local OST0_QUOTA_USED=$(getquota -u $TSTUSR $OST0_UUID curspace)
2214         [ $OST0_QUOTA_USED -ne 0 ] &&
2215                 ($SHOW_QUOTA_USER; \
2216                 quota_error u $TSTUSR "quota isn't released")
2217         $SHOW_QUOTA_USER
2218 }
2219
2220 test_23() {
2221         [ "$ost1_FSTYPE" == zfs ] &&
2222                 skip "Overwrite in place is not guaranteed to be " \
2223                 "space neutral on ZFS"
2224
2225         local OST0_MIN=$((6 * 1024)) # 6MB, extra space for meta blocks.
2226         check_whether_skip && return 0
2227         log "run for 4MB test file"
2228         test_23_sub 4
2229
2230         OST0_MIN=$((60 * 1024)) # 60MB, extra space for meta blocks.
2231         check_whether_skip && return 0
2232         log "run for 40MB test file"
2233         test_23_sub 40
2234 }
2235 run_test 23 "Quota should be honored with directIO (b16125)"
2236
2237 test_24() {
2238         local blimit=5 # 5M
2239         local TESTFILE="$DIR/$tdir/$tfile"
2240
2241         setup_quota_test || error "setup quota failed with $?"
2242         trap cleanup_quota_test EXIT
2243
2244         set_ost_qtype $QTYPE || error "enable ost quota failed"
2245
2246         # bind file to a single OST
2247         $LFS setstripe -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
2248         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2249
2250         echo "Set user quota (limit: ${blimit}M)"
2251         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2252                 error "set quota failed"
2253
2254         # overrun quota by root user
2255         runas -u 0 -g 0 $DD of=$TESTFILE count=$((blimit + 1)) ||
2256                 error "write failure, expect success"
2257         cancel_lru_locks osc
2258         sync_all_data || true
2259
2260         $SHOW_QUOTA_USER | grep '*' || error "no matching *"
2261
2262         cleanup_quota_test
2263 }
2264 run_test 24 "lfs draws an asterix when limit is reached (b16646)"
2265
2266 test_27a() { # b19612
2267         $LFS quota $TSTUSR $DIR &&
2268                 error "lfs succeeded with no type, but should have failed"
2269         $LFS setquota $TSTUSR $DIR &&
2270                 error "lfs succeeded with no type, but should have failed"
2271         return 0
2272 }
2273 run_test 27a "lfs quota/setquota should handle wrong arguments (b19612)"
2274
2275 test_27b() { # b20200
2276         $LFS setquota -u $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2277                 error "lfs setquota failed with uid argument"
2278         $LFS setquota -g $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2279                 error "lfs stequota failed with gid argument"
2280         if is_project_quota_supported; then
2281                 $LFS setquota -p $TSTPRJID -b 1000 -B 1000 -i 1000 -I \
2282                         1000 $DIR || error \
2283                                 "lfs stequota failed with projid argument"
2284         fi
2285         $SHOW_QUOTA_USERID || error "lfs quota failed with uid argument"
2286         $SHOW_QUOTA_GROUPID || error "lfs quota failed with gid argument"
2287         if is_project_quota_supported; then
2288                 $SHOW_QUOTA_PROJID ||
2289                         error "lfs quota failed with projid argument"
2290         fi
2291         resetquota -u $TSTUSR
2292         resetquota -g $TSTUSR
2293         resetquota -p $TSTPRJID
2294         return 0
2295 }
2296 run_test 27b "lfs quota/setquota should handle user/group/project ID (b20200)"
2297
2298 test_27c() {
2299         local limit
2300
2301         $LFS setquota -u $TSTID -b 30M -B 3T $DIR ||
2302                 error "lfs setquota failed"
2303
2304         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
2305         [ $limit != "30M" ] && error "softlimit $limit isn't human-readable"
2306         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
2307         [ $limit != "3T" ] && error "hardlimit $limit isn't human-readable"
2308
2309         $LFS setquota -u $TSTID -b 1500M -B 18500G $DIR ||
2310                 error "lfs setquota for $TSTID failed"
2311
2312         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
2313         [ $limit != "1.465G" ] && error "wrong softlimit $limit"
2314         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
2315         [ $limit != "18.07T" ] && error "wrong hardlimit $limit"
2316
2317         $LFS quota -u $TSTID -v -h $DIR | grep -q "Total allocated" ||
2318                 error "total allocated inode/block limit not printed"
2319
2320         resetquota -u $TSTUSR
2321 }
2322 run_test 27c "lfs quota should support human-readable output"
2323
2324 test_27d() {
2325         local softlimit=1.5
2326         local hardlimit=2.3
2327         local limit
2328
2329         $LFS setquota -u $TSTID -b ${softlimit}p -B ${hardlimit}P $DIR ||
2330                 error "set fraction block limit failed"
2331         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $3}')
2332         [ $limit == ${softlimit}P ] || error "get fraction softlimit failed"
2333         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $4}')
2334         [ $limit == ${hardlimit}P ] || error "get fraction hardlimit failed"
2335
2336         resetquota -u $TSTUSR
2337 }
2338 run_test 27d "lfs setquota should support fraction block limit"
2339
2340 test_30() {
2341         local LIMIT=4 # 4MB
2342         local TESTFILE="$DIR/$tdir/$tfile"
2343         local GRACE=10
2344
2345         setup_quota_test || error "setup quota failed with $?"
2346         trap cleanup_quota_test EXIT
2347
2348         set_ost_qtype "u" || error "enable ost quota failed"
2349
2350         $LFS setstripe $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
2351         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2352
2353         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
2354                 $MAX_IQ_TIME $DIR || error "set grace time failed"
2355         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
2356                 error "set quota failed"
2357         $RUNAS $DD of=$TESTFILE count=$((LIMIT * 2)) || true
2358         cancel_lru_locks osc
2359         sleep $GRACE
2360         $LFS setquota -u $TSTUSR -B 0 $DIR || error "clear quota failed"
2361         # over-quota flag has not yet settled since we do not trigger async
2362         # events based on grace time period expiration
2363         $SHOW_QUOTA_USER
2364         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 || true
2365         cancel_lru_locks osc
2366         # now over-quota flag should be settled and further writes should fail
2367         $SHOW_QUOTA_USER
2368         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 &&
2369                 error "grace times were reset"
2370         # cleanup
2371         cleanup_quota_test
2372         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
2373                 $MAX_IQ_TIME $DIR || error "restore grace time failed"
2374 }
2375 run_test 30 "Hard limit updates should not reset grace times"
2376
2377 # basic usage tracking for user & group
2378 test_33() {
2379         local INODES=10 # 10 files
2380         local BLK_CNT=2 # of 2M each
2381         local TOTAL_BLKS=$((INODES * BLK_CNT * 1024))
2382
2383         setup_quota_test || error "setup quota failed with $?"
2384         trap cleanup_quota_test EXIT
2385
2386         # make sure the system is clean
2387         local USED=$(getquota -u $TSTID global curspace)
2388         [ $USED -ne 0 ] &&
2389                 error "Used space ($USED) for user $TSTID isn't 0."
2390         USED=$(getquota -g $TSTID global curspace)
2391         [ $USED -ne 0 ] &&
2392                 error "Used space ($USED) for group $TSTID isn't 0."
2393         if is_project_quota_supported; then
2394                 USED=$(getquota -p $TSTPRJID global curspace)
2395                 [ $USED -ne 0 ] && error \
2396                         "Used space ($USED) for project $TSTPRJID isn't 0."
2397         fi
2398
2399         echo "Write files..."
2400         for i in $(seq 0 $INODES); do
2401                 $RUNAS $DD of=$DIR/$tdir/$tfile-$i count=$BLK_CNT 2>/dev/null ||
2402                         error "write failed"
2403                         is_project_quota_supported &&
2404                                 change_project -p $TSTPRJID $DIR/$tdir/$tfile-$i
2405                 echo "Iteration $i/$INODES completed"
2406         done
2407         cancel_lru_locks osc
2408
2409         echo "Wait for setattr on objects finished..."
2410         wait_delete_completed
2411
2412         sync; sync_all_data || true
2413
2414         echo "Verify disk usage after write"
2415         USED=$(getquota -u $TSTID global curspace)
2416         [ $USED -lt $TOTAL_BLKS ] &&
2417                 error "Used space for user $TSTID:$USED, expected:$TOTAL_BLKS"
2418         USED=$(getquota -g $TSTID global curspace)
2419         [ $USED -lt $TOTAL_BLKS ] &&
2420                 error "Used space for group $TSTID:$USED, expected:$TOTAL_BLKS"
2421         if is_project_quota_supported; then
2422                 USED=$(getquota -p $TSTPRJID global curspace)
2423                 [ $USED -lt $TOTAL_BLKS ] && error \
2424                         "Used space for project $TSTPRJID:$USED, expected:$TOTAL_BLKS"
2425         fi
2426
2427         echo "Verify inode usage after write"
2428         USED=$(getquota -u $TSTID global curinodes)
2429         [ $USED -lt $INODES ] &&
2430                 error "Used inode for user $TSTID is $USED, expected $INODES"
2431         USED=$(getquota -g $TSTID global curinodes)
2432         [ $USED -lt $INODES ] &&
2433                 error "Used inode for group $TSTID is $USED, expected $INODES"
2434         if is_project_quota_supported; then
2435                 USED=$(getquota -p $TSTPRJID global curinodes)
2436                 [ $USED -lt $INODES ] && error \
2437                         "Used inode for project $TSTPRJID is $USED, expected $INODES"
2438         fi
2439
2440         cleanup_quota_test
2441
2442         echo "Verify disk usage after delete"
2443         USED=$(getquota -u $TSTID global curspace)
2444         [ $USED -eq 0 ] || error "Used space for user $TSTID isn't 0. $USED"
2445         USED=$(getquota -u $TSTID global curinodes)
2446         [ $USED -eq 0 ] || error "Used inodes for user $TSTID isn't 0. $USED"
2447         USED=$(getquota -g $TSTID global curspace)
2448         [ $USED -eq 0 ] || error "Used space for group $TSTID isn't 0. $USED"
2449         USED=$(getquota -g $TSTID global curinodes)
2450         [ $USED -eq 0 ] || error "Used inodes for group $TSTID isn't 0. $USED"
2451         if is_project_quota_supported; then
2452                 USED=$(getquota -p $TSTPRJID global curspace)
2453                 [ $USED -eq 0 ] ||
2454                         error "Used space for project $TSTPRJID isn't 0. $USED"
2455                 USED=$(getquota -p $TSTPRJID global curinodes)
2456                 [ $USED -eq 0 ] ||
2457                         error "Used inodes for project $TSTPRJID isn't 0. $USED"
2458         fi
2459 }
2460 run_test 33 "Basic usage tracking for user & group & project"
2461
2462 # usage transfer test for user & group & project
2463 test_34() {
2464         local BLK_CNT=2 # 2MB
2465         local project_supported="no"
2466
2467         is_project_quota_supported && project_supported="yes"
2468         setup_quota_test || error "setup quota failed with $?"
2469         trap cleanup_quota_test EXIT
2470
2471         # make sure the system is clean
2472         local USED=$(getquota -u $TSTID global curspace)
2473         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID isn't 0."
2474         USED=$(getquota -g $TSTID global curspace)
2475         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTID isn't 0."
2476
2477         local USED=$(getquota -u $TSTID2 global curspace)
2478         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID2 isn't 0."
2479         if [ $project_supported == "yes" ]; then
2480                 USED=$(getquota -p $TSTPRJID global curspace)
2481                 [ $USED -ne 0 ] && error \
2482                         "Used space ($USED) for Project $TSTPRJID isn't 0."
2483         fi
2484
2485         echo "Write file..."
2486         $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
2487                 error "write failed"
2488         cancel_lru_locks osc
2489         sync; sync_all_data || true
2490
2491         echo "chown the file to user $TSTID"
2492         chown $TSTID $DIR/$tdir/$tfile || error "chown failed"
2493
2494         echo "Wait for setattr on objects finished..."
2495         wait_delete_completed
2496
2497         BLK_CNT=$((BLK_CNT * 1024))
2498
2499         echo "Verify disk usage for user $TSTID"
2500         USED=$(getquota -u $TSTID global curspace)
2501         [ $USED -lt $BLK_CNT ] &&
2502                 error "Used space for user $TSTID is ${USED}, expected $BLK_CNT"
2503         USED=$(getquota -u $TSTID global curinodes)
2504         [ $USED -ne 1 ] &&
2505                 error "Used inodes for user $TSTID is $USED, expected 1"
2506
2507         echo "chgrp the file to group $TSTID"
2508         chgrp $TSTID $DIR/$tdir/$tfile || error "chgrp failed"
2509
2510         echo "Wait for setattr on objects finished..."
2511         wait_delete_completed
2512
2513         echo "Verify disk usage for group $TSTID"
2514         USED=$(getquota -g $TSTID global curspace)
2515         [ $USED -ge $BLK_CNT ] ||
2516                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
2517         USED=$(getquota -g $TSTID global curinodes)
2518         [ $USED -eq 1 ] ||
2519                 error "Used inodes for group $TSTID is $USED, expected 1"
2520
2521         # chown won't change the ost object group. LU-4345 */
2522         echo "chown the file to user $TSTID2"
2523         chown $TSTID2 $DIR/$tdir/$tfile || error "chown to $TSTID2 failed"
2524
2525         echo "Wait for setattr on objects finished..."
2526         wait_delete_completed
2527
2528         echo "change_project project id to $TSTPRJID"
2529         [ $project_supported == "yes" ] &&
2530                 change_project -p $TSTPRJID $DIR/$tdir/$tfile
2531         echo "Wait for setattr on objects finished..."
2532         wait_delete_completed
2533
2534         echo "Verify disk usage for user $TSTID2/$TSTID and group $TSTID"
2535         USED=$(getquota -u $TSTID2 global curspace)
2536         [ $USED -lt $BLK_CNT ] &&
2537                 error "Used space for user $TSTID2 is $USED, expected $BLK_CNT"
2538         USED=$(getquota -u $TSTID global curspace)
2539         [ $USED -ne 0 ] &&
2540                 error "Used space for user $TSTID is $USED, expected 0"
2541         USED=$(getquota -g $TSTID global curspace)
2542         [ $USED -lt $BLK_CNT ] &&
2543                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
2544         if [ $project_supported == "yes" ]; then
2545                 USED=$(getquota -p $TSTPRJID global curspace)
2546                 [ $USED -lt $BLK_CNT ] && error \
2547                         "Used space for group $TSTPRJID is $USED, expected $BLK_CNT"
2548         fi
2549
2550         cleanup_quota_test
2551 }
2552 run_test 34 "Usage transfer for user & group & project"
2553
2554 # usage is still accessible across restart
2555 test_35() {
2556         local BLK_CNT=2 # 2 MB
2557
2558         setup_quota_test || error "setup quota failed with $?"
2559         trap cleanup_quota_test EXIT
2560
2561         echo "Write file..."
2562         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
2563                 error "write failed"
2564         is_project_quota_supported &&
2565                 change_project -p $TSTPRJID $DIR/$tdir/$tfile
2566         cancel_lru_locks osc
2567
2568         echo "Wait for setattr on objects finished..."
2569         wait_delete_completed
2570
2571         sync; sync_all_data || true
2572
2573         echo "Save disk usage before restart"
2574         local ORIG_USR_SPACE=$(getquota -u $TSTID global curspace)
2575         [ $ORIG_USR_SPACE -eq 0 ] &&
2576                 error "Used space for user $TSTID is 0, expected ${BLK_CNT}M"
2577         local ORIG_USR_INODES=$(getquota -u $TSTID global curinodes)
2578         [ $ORIG_USR_INODES -eq 0 ] &&
2579                 error "Used inodes for user $TSTID is 0, expected 1"
2580         echo "User $TSTID: ${ORIG_USR_SPACE}KB $ORIG_USR_INODES inodes"
2581         local ORIG_GRP_SPACE=$(getquota -g $TSTID global curspace)
2582         [ $ORIG_GRP_SPACE -eq 0 ] &&
2583                 error "Used space for group $TSTID is 0, expected ${BLK_CNT}M"
2584         local ORIG_GRP_INODES=$(getquota -g $TSTID global curinodes)
2585         [ $ORIG_GRP_INODES -eq 0 ] &&
2586                 error "Used inodes for group $TSTID is 0, expected 1"
2587         echo "Group $TSTID: ${ORIG_GRP_SPACE}KB $ORIG_GRP_INODES inodes"
2588
2589         if is_project_quota_supported; then
2590                 local ORIG_PRJ_SPACE=$(getquota -p $TSTPRJID global curspace)
2591                 [ $ORIG_PRJ_SPACE -eq 0 ] && error \
2592                         "Used space for project $TSTPRJID is 0, expected ${BLK_CNT}M"
2593                 local ORIG_PRJ_INODES=$(getquota -p $TSTPRJID global curinodes)
2594                 [ $ORIG_PRJ_INODES -eq 0 ] && error \
2595                         "Used inodes for project $TSTPRJID is 0, expected 1"
2596                 echo "Project $TSTPRJID: ${ORIG_PRJ_SPACE}KB $ORIG_PRJ_INODES inodes"
2597         fi
2598
2599         log "Restart..."
2600         stopall
2601         setupall
2602         quota_init
2603
2604         echo "Verify disk usage after restart"
2605         local USED=$(getquota -u $TSTID global curspace)
2606         [ $USED -eq $ORIG_USR_SPACE ] ||
2607                 error "Used space for user $TSTID changed from " \
2608                         "$ORIG_USR_SPACE to $USED"
2609         USED=$(getquota -u $TSTID global curinodes)
2610         [ $USED -eq $ORIG_USR_INODES ] ||
2611                 error "Used inodes for user $TSTID changed from " \
2612                         "$ORIG_USR_INODES to $USED"
2613         USED=$(getquota -g $TSTID global curspace)
2614         [ $USED -eq $ORIG_GRP_SPACE ] ||
2615                 error "Used space for group $TSTID changed from " \
2616                         "$ORIG_GRP_SPACE to $USED"
2617         USED=$(getquota -g $TSTID global curinodes)
2618         [ $USED -eq $ORIG_GRP_INODES ] ||
2619                 error "Used inodes for group $TSTID changed from " \
2620                         "$ORIG_GRP_INODES to $USED"
2621         if [ $project_supported == "yes" ]; then
2622                 USED=$(getquota -p $TSTPRJID global curinodes)
2623                 [ $USED -eq $ORIG_PRJ_INODES ] ||
2624                         error "Used inodes for project $TSTPRJID " \
2625                                 "changed from $ORIG_PRJ_INODES to $USED"
2626                 USED=$(getquota -p $TSTPRJID global curspace)
2627                 [ $USED -eq $ORIG_PRJ_SPACE ] ||
2628                         error "Used space for project $TSTPRJID "\
2629                                 "changed from $ORIG_PRJ_SPACE to $USED"
2630         fi
2631
2632         # check if the vfs_dq_init() is called before writing
2633         echo "Append to the same file..."
2634         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT seek=1 2>/dev/null ||
2635                 error "write failed"
2636         cancel_lru_locks osc
2637         sync; sync_all_data || true
2638
2639         echo "Verify space usage is increased"
2640         USED=$(getquota -u $TSTID global curspace)
2641         [ $USED -gt $ORIG_USR_SPACE ] ||
2642                 error "Used space for user $TSTID isn't increased" \
2643                         "orig:$ORIG_USR_SPACE, now:$USED"
2644         USED=$(getquota -g $TSTID global curspace)
2645         [ $USED -gt $ORIG_GRP_SPACE ] ||
2646                 error "Used space for group $TSTID isn't increased" \
2647                         "orig:$ORIG_GRP_SPACE, now:$USED"
2648         if [ $project_supported == "yes" ]; then
2649                 USED=$(getquota -p $TSTPRJID global curspace)
2650                 [ $USED -gt $ORIG_PRJ_SPACE ] ||
2651                         error "Used space for project $TSTPRJID isn't " \
2652                                 "increased orig:$ORIG_PRJ_SPACE, now:$USED"
2653         fi
2654
2655         cleanup_quota_test
2656 }
2657 run_test 35 "Usage is still accessible across reboot"
2658
2659 # chown/chgrp to the file created with MDS_OPEN_DELAY_CREATE
2660 # LU-5006
2661 test_37() {
2662         [ "$MDS1_VERSION" -lt $(version_code 2.6.93) ] &&
2663                 skip "Old server doesn't have LU-5006 fix."
2664
2665         setup_quota_test || error "setup quota failed with $?"
2666         trap cleanup_quota_test EXIT
2667
2668         # make sure the system is clean
2669         local USED=$(getquota -u $TSTID global curspace)
2670         [ $USED -ne 0 ] &&
2671                 error "Used space ($USED) for user $TSTID isn't 0."
2672
2673         # create file with MDS_OPEN_DELAY_CREATE flag
2674         $LFS setstripe -c 1 -i 0 $DIR/$tdir/$tfile ||
2675                 error "Create file failed"
2676         # write to file
2677         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 conv=notrunc \
2678                 oflag=sync || error "Write file failed"
2679         # chown to the file
2680         chown $TSTID $DIR/$tdir/$tfile || error "Chown to file failed"
2681
2682         # wait for setattr on objects finished..."
2683         wait_delete_completed
2684
2685         USED=$(getquota -u $TSTID global curspace)
2686         [ $USED -ne 0 ] || quota_error u $TSTUSR "Used space is 0"
2687
2688         cleanup_quota_test
2689 }
2690 run_test 37 "Quota accounted properly for file created by 'lfs setstripe'"
2691
2692 # LU-8801
2693 test_38() {
2694         [ "$MDS1_VERSION" -lt $(version_code 2.8.60) ] &&
2695                 skip "Old server doesn't have LU-8801 fix."
2696
2697         [ "$UID" != 0 ] && skip_env "must run as root" && return
2698
2699         setup_quota_test || error "setup quota failed with $?"
2700         trap cleanup_quota_test EXIT
2701
2702         # make sure the system is clean
2703         local USED=$(getquota -u $TSTID global curspace)
2704         [ $USED -ne 0 ] &&
2705                 error "Used space ($USED) for user $TSTID isn't 0."
2706         USED=$(getquota -u $TSTID2 global curspace)
2707         [ $USED -ne 0 ] &&
2708                 error "Used space ($USED) for user $TSTID2 isn't 0."
2709
2710         local TESTFILE="$DIR/$tdir/$tfile"
2711         local file_cnt=10000
2712
2713         # Generate id entries in accounting file
2714         echo "Create $file_cnt files..."
2715         for i in `seq $file_cnt`; do
2716                 touch $TESTFILE-$i
2717                 chown $((file_cnt - i)):$((file_cnt - i)) $TESTFILE-$i ||
2718                         error "failed to chown $TESTFILE-$i"
2719         done
2720         cancel_lru_locks osc
2721         sync; sync_all_data || true
2722
2723         local procf="osd-$mds1_FSTYPE.$FSNAME-MDT0000"
2724         procf=${procf}.quota_slave.acct_user
2725         local accnt_cnt
2726
2727         acct_cnt=$(do_facet mds1 $LCTL get_param $procf | grep "id:" | \
2728                    awk '{if ($3 < 10000) {print $3}}' | wc -l)
2729         echo "Found $acct_cnt id entries"
2730
2731         [ $file_cnt -eq $acct_cnt ] || {
2732                 do_facet mds1 $LCTL get_param $procf
2733                 error "skipped id entries"
2734         }
2735
2736         cleanup_quota_test
2737 }
2738 run_test 38 "Quota accounting iterator doesn't skip id entries"
2739
2740 test_39() {
2741         local TESTFILE="$DIR/$tdir/project"
2742         ! is_project_quota_supported &&
2743                 skip "Project quota is not supported"
2744
2745         setup_quota_test || error "setup quota failed with $?"
2746
2747         touch $TESTFILE
2748         projectid=$(lfs project $TESTFILE | awk '{print $1}')
2749         [ $projectid -ne 0 ] &&
2750                 error "Project id should be 0 not $projectid"
2751         change_project -p 1024 $TESTFILE
2752         projectid=$(lfs project $TESTFILE | awk '{print $1}')
2753         [ $projectid -ne 1024 ] &&
2754                 error "Project id should be 1024 not $projectid"
2755
2756         stopall || error "failed to stopall (1)"
2757         mount
2758         setupall
2759         projectid=$(lfs project $TESTFILE | awk '{print $1}')
2760         [ $projectid -ne 1024 ] &&
2761                 error "Project id should be 1024 not $projectid"
2762
2763         cleanup_quota_test
2764 }
2765 run_test 39 "Project ID interface works correctly"
2766
2767 test_40a() {
2768         ! is_project_quota_supported &&
2769                 skip "Project quota is not supported"
2770         local dir1="$DIR/$tdir/dir1"
2771         local dir2="$DIR/$tdir/dir2"
2772
2773         setup_quota_test || error "setup quota failed with $?"
2774
2775         mkdir -p $dir1 $dir2
2776         change_project -sp 1 $dir1 && touch $dir1/1
2777         change_project -sp 2 $dir2
2778
2779         ln $dir1/1 $dir2/1_link &&
2780                 error "Hard link across different project quota should fail"
2781         rm -rf $dir1 $dir2
2782
2783         cleanup_quota_test
2784 }
2785 run_test 40a "Hard link across different project ID"
2786
2787 test_40b() {
2788         ! is_project_quota_supported &&
2789                 skip "Project quota is not supported"
2790         local dir1="$DIR/$tdir/dir1"
2791         local dir2="$DIR/$tdir/dir2"
2792
2793         setup_quota_test || error "setup quota failed with $?"
2794         mkdir -p $dir1 $dir2
2795         change_project -sp 1 $dir1 && touch $dir1/1
2796         change_project -sp 2 $dir2
2797
2798         mv $dir1/1 $dir2/2 || error "mv failed $?"
2799         local projid=$(lfs project $dir2/2 | awk '{print $1}')
2800         if [ "$projid" != "2" ]; then
2801                 error "project id expected 2 not $projid"
2802         fi
2803         rm -rf $dir1 $dir2
2804         cleanup_quota_test
2805 }
2806 run_test 40b "Mv across different project ID"
2807
2808 test_40c() {
2809         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
2810                 ! is_project_quota_supported &&
2811                         skip "Project quota is not supported"
2812
2813         setup_quota_test || error "setup quota failed with $?"
2814         local dir="$DIR/$tdir/dir"
2815
2816         mkdir -p $dir && change_project -sp 1 $dir
2817         $LFS mkdir -i 1 $dir/remote_dir || error "create remote dir failed"
2818         local projid=$(lfs project -d $dir/remote_dir | awk '{print $1}')
2819         [ "$projid" != "1" ] && error "projid id expected 1 not $projid"
2820         touch $dir/remote_dir/file
2821         #verify inherit works file for remote dir.
2822         local projid=$(lfs project -d $dir/remote_dir/file | awk '{print $1}')
2823         [ "$projid" != "1" ] &&
2824                 error "file under remote dir expected 1 not $projid"
2825
2826         #Agent inode should be ignored for project quota
2827         USED=$(getquota -p 1 global curinodes)
2828         [ "$USED" != "3" ] &&
2829                 error "file count expected 3 got $USED"
2830
2831         rm -rf $dir
2832         cleanup_quota_test
2833         return 0
2834 }
2835 run_test 40c "Remote child Dir inherit project quota properly"
2836
2837 test_50() {
2838         ! is_project_quota_supported &&
2839                 skip "Project quota is not supported"
2840
2841         setup_quota_test || error "setup quota failed with $?"
2842         local dir1="$DIR/$tdir/dir1"
2843         local dir2="$DIR/$tdir/dir2"
2844
2845         mkdir -p $dir1 && change_project -sp 1 $dir1
2846         mkdir -p $dir2 && change_project -sp 2 $dir2
2847         for num in $(seq 1 10); do
2848                 touch $dir1/file_$num $dir2/file_$num
2849                 ln -s $dir1/file_$num $dir1/file_$num"_link"
2850                 ln -s $dir2/file_$num $dir2/file_$num"_link"
2851         done
2852
2853         count=$($LFS find --projid 1 $DIR | wc -l)
2854         [ "$count" != 21 ] && error "expected 21 but got $count"
2855
2856         # 1(projid 0 dir) + 1(projid 2 dir) + 20(projid 2 files)
2857         count=$($LFS find ! --projid 1 $DIR/$tdir | wc -l)
2858         [ "$count" != 22 ] && error "expected 22 but got $count"
2859
2860         rm -rf $dir1 $dir2
2861         cleanup_quota_test
2862 }
2863 run_test 50 "Test if lfs find --projid works"
2864
2865 test_51() {
2866         ! is_project_quota_supported &&
2867                 skip "Project quota is not supported"
2868         setup_quota_test || error "setup quota failed with $?"
2869         local dir="$DIR/$tdir/dir"
2870
2871         mkdir $dir && change_project -sp 1 $dir
2872         local used=$(getquota -p 1 global curinodes)
2873         [ $used != "1" ] && error "expected 1 got $used"
2874
2875         touch $dir/1
2876         touch $dir/2
2877         cp $dir/2 $dir/3
2878         used=$(getquota -p 1 global curinodes)
2879         [ $used != "4" ] && error "expected 4 got $used"
2880
2881         $DD if=/dev/zero of=$DIR/$tdir/6 bs=1M count=1
2882         #try cp to dir
2883         cp $DIR/$tdir/6 $dir/6
2884         used=$(getquota -p 1 global curinodes)
2885         [ $used != "5" ] && error "expected 5 got $used"
2886
2887         #try mv to dir
2888         mv $DIR/$tdir/6 $dir/7
2889         used=$(getquota -p 1 global curinodes)
2890         [ $used != "6" ] && error "expected 6 got $used"
2891
2892         rm -rf $dir
2893         cleanup_quota_test
2894 }
2895 run_test 51 "Test project accounting with mv/cp"
2896
2897 test_52() {
2898         ! is_project_quota_supported &&
2899                 skip "Project quota is not supported"
2900         setup_quota_test || error "setup quota failed with $?"
2901         local dir="$DIR/$tdir/dir"
2902         mkdir $dir && change_project -sp 1 $dir
2903
2904         touch $DIR/$tdir/file
2905         #Try renaming a file into the project.  This should fail.
2906         for num in $(seq 1 2000); do
2907                 mrename $DIR/$tdir/file $dir/file >&/dev/null &&
2908                         error "rename should fail"
2909         done
2910         rm -rf $dir
2911         cleanup_quota_test
2912 }
2913 run_test 52 "Rename across different project ID"
2914
2915 test_53() {
2916         ! is_project_quota_supported &&
2917                 skip "Project quota is not supported"
2918         setup_quota_test || error "setup quota failed with $?"
2919         local dir="$DIR/$tdir/dir"
2920         mkdir $dir && change_project -s $dir
2921         lfs project -d $dir | grep P || error "inherit attribute should be set"
2922
2923         change_project -C $dir
2924         lfs project -d $dir | grep P &&
2925                 error "inherit attribute should be cleared"
2926
2927         rm -rf $dir
2928         cleanup_quota_test
2929 }
2930 run_test 53 "Project inherit attribute could be cleared"
2931
2932 test_54() {
2933         ! is_project_quota_supported &&
2934                 skip "Project quota is not supported"
2935         setup_quota_test || error "setup quota failed with $?"
2936         trap cleanup_quota_test EXIT
2937         local testfile="$DIR/$tdir/$tfile-0"
2938
2939         #set project ID/inherit attribute
2940         change_project -sp $TSTPRJID $DIR/$tdir
2941         $RUNAS createmany -m ${testfile} 100 ||
2942                 error "create many files failed"
2943
2944         local proj_count=$(lfs project -r $DIR/$tdir | wc -l)
2945         # one more count for directory itself */
2946         ((proj_count++))
2947
2948         #check project
2949         local proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
2950         [ $proj_count1 -eq 0 ] || error "c1: expected 0 got $proj_count1"
2951
2952         proj_count1=$(lfs project -rcp $((TSTPRJID+1)) $DIR/$tdir | wc -l)
2953         [ $proj_count1 -eq $proj_count ] ||
2954                         error "c2: expected $proj_count got $proj_count1"
2955
2956         #clear project but with kept projid
2957         change_project -rCk $DIR/$tdir
2958         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
2959         [ $proj_count1 -eq 1 ] ||
2960                         error "c3: expected 1 got $proj_count1"
2961
2962         #verify projid untouched.
2963         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
2964         ((proj_count1++))
2965         [ $proj_count1 -eq $proj_count ] ||
2966                         error "c4: expected $proj_count got $proj_count1"
2967
2968         # test -0 option
2969         lfs project $DIR/$tdir -cr -0 | xargs -0 lfs project -s
2970         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
2971         [ $proj_count1 -eq 0 ] || error "c5: expected 0 got $proj_count1"
2972
2973         #this time clear all
2974         change_project -rC $DIR/$tdir
2975         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
2976         [ $proj_count1 -eq 0 ] ||
2977                         error "c6: expected 0 got $proj_count1"
2978         #cleanup
2979         unlinkmany ${testfile} 100 ||
2980                 error "unlink many files failed"
2981
2982         cleanup_quota_test
2983 }
2984 run_test 54 "basic lfs project interface test"
2985
2986 test_55() {
2987         [ "$MDS1_VERSION" -lt $(version_code 2.10.58) ] &&
2988                 skip "Not supported before 2.10.58."
2989         setup_quota_test || error "setup quota failed with $?"
2990
2991         set_ost_qtype $QTYPE || error "enable ost quota failed"
2992         quota_init
2993
2994         #add second group to TSTUSR
2995         usermod -G $TSTUSR,$TSTUSR2 $TSTUSR
2996
2997         #prepare test file
2998         $RUNAS dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1024 count=100000 ||
2999         error "failed to dd"
3000
3001         cancel_lru_locks osc
3002         sync; sync_all_data || true
3003
3004         $LFS setquota -g $TSTUSR2 -b 0 -B 50M $DIR ||
3005         error "failed to setquota on group $TSTUSR2"
3006
3007         $LFS quota -v -g $TSTUSR2 $DIR
3008
3009         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile &&
3010         error "chgrp should failed with -EDQUOT"
3011
3012         USED=$(getquota -g $TSTUSR2 global curspace)
3013         echo "$USED"
3014
3015         $LFS setquota -g $TSTUSR2 -b 0 -B 300M $DIR ||
3016         error "failed to setquota on group $TSTUSR2"
3017
3018         $LFS quota -v -g $TSTUSR2 $DIR
3019
3020         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile ||
3021         error "chgrp should succeed"
3022
3023         $LFS quota -v -g $TSTUSR2 $DIR
3024
3025         cleanup_quota_test
3026 }
3027 run_test 55 "Chgrp should be affected by group quota"
3028
3029 test_56() {
3030         setup_quota_test || error "setup quota failed with $?"
3031
3032         set_ost_qtype $QTYPE || error "enable ost quota failed"
3033         quota_init
3034
3035         $LFS setquota -t -u -b 10 -i 10 $DIR ||
3036                 erro "failed to set grace time for usr quota"
3037         grace_time=$($LFS quota -t -u $DIR | grep "Block grace time:" |
3038                      awk '{print $4 $8}')
3039         if [ "x$grace_time" != "x10s;10s" ]; then
3040                 $LFS quota -t -u $DIR
3041                 error "expected grace time: 10s;10s, got:$grace_time"
3042         fi
3043
3044         cleanup_quota_test
3045 }
3046 run_test 56 "lfs quota -t should work well"
3047
3048 test_57() {
3049         setup_quota_test || error "setup quota failed with $?"
3050
3051         local dir="$DIR/$tdir/dir"
3052         mkdir -p $dir
3053         mkfifo $dir/pipe
3054         #try to change pipe file should not hang and return failure
3055         wait_update_facet client "$LFS project -sp 1 $dir/pipe 2>&1 |
3056                 awk -F ':' '{ print \\\$2 }'" \
3057                         " unable to get xattr for fifo '$dir/pipe'" || return 1
3058         #command can process further if it hit some errors
3059         touch $dir/aaa $dir/bbb
3060         mkdir $dir/subdir -p
3061         touch $dir/subdir/aaa $dir/subdir/bbb
3062         #create one invalid link file
3063         ln -s $dir/not_exist_file $dir/ccc
3064         local cnt=$(lfs project -r $dir 2>/dev/null | wc -l)
3065         [ $cnt -eq 5 ] || error "expected 5 got $cnt"
3066
3067         cleanup_quota_test
3068 }
3069 run_test 57 "lfs project could tolerate errors"
3070
3071 test_59() {
3072         [ "$mds1_FSTYPE" != ldiskfs ] &&
3073                 skip "ldiskfs only test"
3074         disable_project_quota
3075         setup_quota_test || error "setup quota failed with $?"
3076         quota_init
3077
3078         local testfile="$DIR/$tdir/$tfile-0"
3079         #make sure it did not crash kernel
3080         touch $testfile && lfs project -sp 1 $testfile
3081
3082         enable_project_quota
3083         cleanup_quota_test
3084 }
3085 run_test 59 "lfs project dosen't crash kernel with project disabled"
3086
3087 test_60() {
3088         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
3089                 skip "Needs MDS version 2.11.53 or later."
3090         setup_quota_test || error "setup quota failed with $?"
3091         trap cleanup_quota_test EXIT
3092
3093         local testfile=$DIR/$tdir/$tfile
3094         local limit=100
3095
3096         set_mdt_qtype "ug" || error "enable mdt quota failed"
3097
3098         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $limit $DIR ||
3099                 error "set quota failed"
3100         quota_show_check a g $TSTUSR
3101
3102         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $DIR/$tdir failed"
3103         chmod g+s $DIR/$tdir || error "chmod g+s failed"
3104         $RUNAS createmany -m ${testfile} $((limit-1)) ||
3105                 error "create many files failed"
3106
3107         $RUNAS touch $DIR/$tdir/foo && error "regular user should fail"
3108
3109         # root user can overrun quota
3110         runas -u 0 -g 0 touch $DIR/$tdir/foo ||
3111                 error "root user should succeed"
3112
3113         cleanup_quota_test
3114 }
3115 run_test 60 "Test quota for root with setgid"
3116
3117 # test default quota
3118 test_default_quota() {
3119         [ "$MDS1_VERSION" -lt $(version_code 2.11.51) ] &&
3120                 skip "Not supported before 2.11.51."
3121
3122         local qtype=$1
3123         local qpool=$2
3124         local qid=$TSTUSR
3125         local qprjid=$TSTPRJID
3126         local qdtype="-U"
3127         local qs="-b"
3128         local qh="-B"
3129         local LIMIT=20480 #20M disk space
3130         local TESTFILE="$DIR/$tdir/$tfile-0"
3131
3132         [ $qtype == "-p" ] && ! is_project_quota_supported &&
3133                 echo "Project quota is not supported" && return 0
3134
3135         [ $qtype == "-u" ] && qdtype="-U"
3136         [ $qtype == "-g" ] && qdtype="-G"
3137         [ $qtype == "-p" ] && {
3138                 qdtype="-P"
3139                 qid=$qprjid
3140         }
3141
3142         [ $qpool == "meta" ] && {
3143                 LIMIT=10240 #10K inodes
3144                 qs="-i"
3145                 qh="-I"
3146         }
3147
3148         setup_quota_test || error "setup quota failed with $?"
3149         trap cleanup_quota_test EXIT
3150
3151         quota_init
3152
3153         # enable mdt/ost quota
3154         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
3155         set_ost_qtype $QTYPE || error "enable ost quota failed"
3156
3157         log "set to use default quota"
3158         $LFS setquota $qtype $qid -d $DIR ||
3159                 error "set $qid to use default quota failed"
3160
3161         log "set default quota"
3162         $LFS setquota $qdtype $qs ${LIMIT} $qh ${LIMIT} $DIR ||
3163                 error "set $qid default quota failed"
3164
3165         log "get default quota"
3166         $LFS quota $qdtype $DIR || error "get default quota failed"
3167
3168         if [ $qpool == "data" ]; then
3169                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
3170                                                         awk '{print $2}')
3171                 [ $SLIMIT -eq $LIMIT ] ||
3172                         error "the returned default quota is wrong"
3173         else
3174                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
3175                                                         awk '{print $5}')
3176                 [ $SLIMIT -eq $LIMIT ] ||
3177                         error "the returned default quota is wrong"
3178         fi
3179
3180         # make sure the system is clean
3181         local USED=$(getquota $qtype $qid global curspace)
3182         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
3183
3184         $LFS setstripe $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
3185         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
3186
3187         [ $qtype == "-p" ] && change_project -sp $TSTPRJID $DIR/$tdir
3188
3189         log "Test not out of quota"
3190         if [ $qpool == "data" ]; then
3191                 $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 >> 10)) oflag=sync ||
3192                         quota_error $qtype $qid "write failed, expect succeed"
3193         else
3194                 $RUNAS createmany -m $TESTFILE $((LIMIT/2)) ||
3195                         quota_error $qtype $qid "create failed, expect succeed"
3196
3197                 unlinkmany $TESTFILE $((LIMIT/2))
3198         fi
3199
3200         log "Test out of quota"
3201         # flush cache, ensure noquota flag is set on client
3202         cancel_lru_locks osc
3203         cancel_lru_locks mdc
3204         sync; sync_all_data || true
3205         if [ $qpool == "data" ]; then
3206                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
3207                         quota_error $qtype $qid "write succeed, expect EDQUOT"
3208         else
3209                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
3210                         quota_error $qtype $qid "create succeed, expect EDQUOT"
3211
3212                 unlinkmany $TESTFILE $((LIMIT*2))
3213         fi
3214
3215         log "Increase default quota"
3216         # increase default quota
3217         $LFS setquota $qdtype $qs $((LIMIT*3)) $qh $((LIMIT*3)) $DIR ||
3218                 error "set default quota failed"
3219
3220         cancel_lru_locks osc
3221         cancel_lru_locks mdc
3222         sync; sync_all_data || true
3223         if [ $qpool == "data" ]; then
3224                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
3225                         quota_error $qtype $qid "write failed, expect succeed"
3226         else
3227                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
3228                         quota_error $qtype $qid "create failed, expect succeed"
3229
3230                 unlinkmany $TESTFILE $((LIMIT*2))
3231         fi
3232
3233         log "Set quota to override default quota"
3234         $LFS setquota $qtype $qid $qs ${LIMIT} $qh ${LIMIT} $DIR ||
3235                 error "set $qid quota failed"
3236
3237         cancel_lru_locks osc
3238         cancel_lru_locks mdc
3239         sync; sync_all_data || true
3240         if [ $qpool == "data" ]; then
3241                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
3242                         quota_error $qtype $qid "write succeed, expect EQUOT"
3243         else
3244                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
3245                         quota_error $qtype $qid "create succeed, expect EQUOT"
3246
3247                 unlinkmany $TESTFILE $((LIMIT*2))
3248         fi
3249
3250         log "Set to use default quota again"
3251         $LFS setquota $qtype $qid -d $DIR ||
3252                 error "set $qid to use default quota failed"
3253
3254         cancel_lru_locks osc
3255         cancel_lru_locks mdc
3256         sync; sync_all_data || true
3257         if [ $qpool == "data" ]; then
3258                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
3259                         quota_error $qtype $qid "write failed, expect succeed"
3260         else
3261                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
3262                         quota_error $qtype $qid "create failed, expect succeed"
3263
3264                 unlinkmany $TESTFILE $((LIMIT*2))
3265         fi
3266
3267         log "Cleanup"
3268         rm -f $TESTFILE
3269         wait_delete_completed || error "wait_delete_completed failed"
3270         sync_all_data || true
3271         $LFS setquota $qdtype -b 0 -B 0 -i 0 -I 0 $DIR ||
3272                 error "reset default quota failed"
3273         $LFS setquota $qtype $qid -b 0 -B 0 -i 0 -I 0 $DIR ||
3274                 error "reset quota failed"
3275
3276         cleanup_quota_test
3277 }
3278
3279 test_61() {
3280         test_default_quota "-u" "data"
3281         test_default_quota "-u" "meta"
3282         test_default_quota "-g" "data"
3283         test_default_quota "-g" "meta"
3284         test_default_quota "-p" "data"
3285         test_default_quota "-p" "meta"
3286 }
3287 run_test 61 "default quota tests"
3288
3289 test_62() {
3290         ! is_project_quota_supported &&
3291                 skip "Project quota is not supported"
3292          [[ "$(chattr -h 2>&1)" =~ "project" ]] ||
3293                 skip "chattr did not support project quota"
3294         setup_quota_test || error "setup quota failed with $?"
3295         local testdir=$DIR/$tdir/
3296
3297         $RUNAS mkdir -p $testdir || error "failed to mkdir"
3298         change_project -s $testdir
3299         [[ $($LFS project -d $testdir) =~ "P" ]] ||
3300                 error "inherit attribute should be set"
3301         # chattr used FS_IOC_SETFLAGS ioctl
3302         $RUNAS chattr -P $testdir &&
3303                 error "regular user clear inherit should fail"
3304         [[ $($LFS project -d $testdir) =~ "P" ]] ||
3305                 error "inherit attribute should still be set"
3306         chattr -P $testdir || error "root failed to clear inherit"
3307         [[ $($LFS project -d $testdir) =~ "P" ]] &&
3308                 error "inherit attribute should be cleared"
3309         cleanup_quota_test
3310 }
3311 run_test 62 "Project inherit should be only changed by root"
3312
3313 test_dom() {
3314         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ] &&
3315                 skip "Not supported before 2.11.55"
3316
3317         local qtype=$1
3318         local qid=$TSTUSR
3319         local dd_failed=false
3320         local tdir_dom=${tdir}_dom
3321         local LIMIT=20480 #20M
3322
3323         [ $qtype == "p" ] && ! is_project_quota_supported &&
3324                 echo "Project quota is not supported" && return 0
3325
3326         [ $qtype == "p" ] && qid=$TSTPRJID
3327
3328         setup_quota_test || error "setup quota failed with $?"
3329         trap cleanup_quota_test EXIT
3330
3331         quota_init
3332
3333         # enable mdt/ost quota
3334         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
3335         set_ost_qtype $QTYPE || error "enable ost quota failed"
3336
3337         # make sure the system is clean
3338         local USED=$(getquota -$qtype $qid global curspace)
3339         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
3340
3341         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $tdir failed"
3342
3343         mkdir $DIR/$tdir_dom || error "mkdir $tdir_dom failed"
3344         $LFS setstripe -E 1M -L mdt $DIR/$tdir_dom ||
3345                 error "setstripe $tdir_dom failed"
3346         chown $TSTUSR.$TSTUSR $DIR/$tdir_dom || error "chown $tdir_dom failed"
3347
3348         [ $qtype == "p" ] && {
3349                 change_project -sp $TSTPRJID $DIR/$tdir
3350                 change_project -sp $TSTPRJID $DIR/$tdir_dom
3351         }
3352
3353         $LFS setquota -$qtype $qid -b $LIMIT -B $LIMIT $DIR ||
3354                 error "set $qid quota failed"
3355
3356         for ((i = 0; i < $((LIMIT/2048)); i++)); do
3357                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3358                                                                 dd_failed=true
3359         done
3360
3361         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
3362
3363         for ((i = $((LIMIT/2048)); i < $((LIMIT/1024 + 10)); i++)); do
3364                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3365                                                                 dd_failed=true
3366         done
3367
3368         $dd_failed || quota_error $qtype $qid "write succeed, expect EDQUOT"
3369
3370         rm -f $DIR/$tdir_dom/*
3371
3372         # flush cache, ensure noquota flag is set on client
3373         cancel_lru_locks osc
3374         cancel_lru_locks mdc
3375         sync; sync_all_data || true
3376
3377         dd_failed=false
3378
3379         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048)) oflag=sync ||
3380                 quota_error $qtype $qid "write failed, expect succeed"
3381
3382         for ((i = 0; i < $((LIMIT/2048 + 10)); i++)); do
3383                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3384                                                                 dd_failed=true
3385         done
3386
3387         $dd_failed || quota_error $qtype $TSTID "write succeed, expect EDQUOT"
3388
3389         rm -f $DIR/$tdir/*
3390         rm -f $DIR/$tdir_dom/*
3391
3392         # flush cache, ensure noquota flag is set on client
3393         cancel_lru_locks osc
3394         cancel_lru_locks mdc
3395         sync; sync_all_data || true
3396
3397         dd_failed=false
3398
3399         for ((i = 0; i < $((LIMIT/2048)); i++)); do
3400                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3401                                                                 dd_failed=true
3402         done
3403
3404         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
3405
3406         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048 + 10)) oflag=sync &&
3407                 quota_error $qtype $qid "write succeed, expect EDQUOT"
3408
3409         rm -f $DIR/$tdir/*
3410         rm -fr $DIR/$tdir_dom
3411
3412         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
3413                 error "reset usr quota failed"
3414
3415         cleanup_quota_test
3416 }
3417
3418 test_63() {
3419         test_dom "u"
3420         test_dom "g"
3421         test_dom "p"
3422 }
3423 run_test 63 "quota on DoM tests"
3424
3425 test_64() {
3426         ! is_project_quota_supported &&
3427                 skip "Project quota is not supported"
3428         setup_quota_test || error "setup quota failed with $?"
3429         local dir1="$DIR/$tdir/"
3430
3431         touch $dir1/file
3432         ln -s $dir1/file $dir1/file_link
3433
3434         $LFS project -sp $TSTPRJID $dir1/file_link >&/dev/null &&
3435                 error "set symlink file's project should fail"
3436
3437         $LFS project $TSTPRJID $dir1/file_link >&/dev/null &&
3438                 error "get symlink file's project should fail"
3439
3440         cleanup_quota_test
3441 }
3442 run_test 64 "lfs project on symlink files should fail"
3443
3444 test_65() {
3445         local SIZE=10 #10M
3446         local TESTFILE="$DIR/$tdir/$tfile-0"
3447
3448         setup_quota_test || error "setup quota failed with $?"
3449         set_ost_qtype $QTYPE || error "enable ost quota failed"
3450         quota_init
3451
3452         echo "Write..."
3453         $RUNAS $DD of=$TESTFILE count=$SIZE ||
3454                 error "failed to write"
3455         # flush cache, ensure noquota flag is set on client
3456         cancel_lru_locks osc
3457         sync; sync_all_data || true
3458
3459         local quota_u=$($LFS quota -u $TSTUSR $DIR)
3460         local quota_g=$($LFS quota -g $TSTUSR $DIR)
3461         local quota_all=$($RUNAS $LFS quota $DIR)
3462
3463         [ "$(echo "$quota_all" | head -n3)" != "$quota_u" ] &&
3464                 error "usr quota not match"
3465         [ "$(echo "$quota_all" | tail -n3)" != "$quota_g" ] &&
3466                 error "grp quota not match"
3467
3468         rm -f $TESTFILE
3469         # cleanup
3470         cleanup_quota_test
3471 }
3472 run_test 65 "Check lfs quota result"
3473
3474 quota_fini()
3475 {
3476         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=-quota"
3477         if $PQ_CLEANUP; then
3478                 disable_project_quota
3479         fi
3480 }
3481 reset_quota_settings
3482 quota_fini
3483
3484 cd $ORIG_PWD
3485 complete $SECONDS
3486 check_and_cleanup_lustre
3487 export QUOTA_AUTO=$QUOTA_AUTO_OLD
3488 exit_status