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