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