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