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