Whamcloud - gitweb
LU-13810 tests: increase limit for 1g
[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 --pool $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 --pool $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 --pool $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 --pool $qpool1 $DIR ||
790                 error "set user quota failed"
791
792         $LFS setquota -u $TSTUSR -B 0M --pool $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 --pool $qpool1 $DIR ||
852                 error "set user quota failed"
853
854         $LFS setquota -u $TSTUSR -B ${limit2}M --pool $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 --pool $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 u $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 --pool $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_1g() {
1003         local limit=20  # 20M
1004         local global_limit=40  # 40M
1005         local testfile="$DIR/$tdir/$tfile-0"
1006         local qpool="qpool1"
1007         local mdmb_param="osc.*.max_dirty_mb"
1008         local max_dirty_mb=$($LCTL get_param -n $mdmb_param | head -1)
1009
1010         mds_supports_qp
1011         setup_quota_test || error "setup quota failed with $?"
1012         stack_trap cleanup_quota_test EXIT
1013         $LCTL set_param $mdmb_param=1
1014         stack_trap "$LCTL set_param $mdmb_param=$max_dirty_mb" EXIT
1015
1016         # enable ost quota
1017         set_ost_qtype $QTYPE || error "enable ost quota failed"
1018
1019         # test for user
1020         log "User quota (block hardlimit:$global_limit MB)"
1021         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
1022                 error "set user quota failed"
1023
1024         pool_add $qpool || error "pool_add failed"
1025         pool_add_targets $qpool 0 $(($OSTCOUNT - 1)) ||
1026                 error "pool_add_targets failed"
1027
1028         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
1029                 error "set user quota failed"
1030
1031         # make sure the system is clean
1032         local used=$(getquota -u $TSTUSR global curspace)
1033         echo "used $used"
1034         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
1035
1036         $LFS setstripe $testfile -C 200 || error "setstripe $testfile failed"
1037         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
1038
1039         log "Write..."
1040         $RUNAS $DD of=$testfile count=$((limit/2)) ||
1041                 quota_error u $TSTUSR \
1042                         "$qtype write failure, but expect success"
1043         log "Write out of block quota ..."
1044         # this time maybe cache write,  ignore it's failure
1045         $RUNAS $DD of=$testfile count=$((limit/2)) seek=$((limit/2)) || true
1046         # flush cache, ensure noquota flag is set on client
1047         cancel_lru_locks osc
1048         sync; sync_all_data || true
1049         sleep 5
1050         $RUNAS $DD of=$testfile count=$OSTCOUNT seek=$limit &&
1051                 quota_error u $TSTUSR \
1052                         "user write success, but expect EDQUOT"
1053
1054         rm -f $testfile
1055         wait_delete_completed || error "wait_delete_completed failed"
1056         sync_all_data || true
1057
1058         used=$(getquota -u $TSTUSR global curspace $qpool)
1059         [ $used -ne 0 ] && quota_error u $TSTUSR \
1060                 "user quota isn't released after deletion"
1061         return 0
1062 }
1063 run_test 1g "Quota pools: Block hard limit with wide striping"
1064
1065 # test inode hardlimit
1066 test_2() {
1067         local TESTFILE="$DIR/$tdir/$tfile-0"
1068         local LIMIT=$(do_facet mds1 $LCTL get_param -n \
1069                 qmt.$FSNAME-QMT0000.md-0x0.info |
1070                 awk '/least qunit/{ print $3 }')
1071         local L2=$(do_facet mds1 $LCTL get_param -n \
1072                 qmt.$FSNAME-QMT0000.md-0x0.soft_least_qunit)
1073
1074         [ $L2 -le $LIMIT ] || LIMIT=$L2
1075
1076         [ "$SLOW" = "no" ] || LIMIT=$((LIMIT * 1024))
1077
1078         local FREE_INODES=$(mdt_free_inodes 0)
1079         echo "$FREE_INODES free inodes on master MDT"
1080         [ $FREE_INODES -lt $LIMIT ] &&
1081                 skip "not enough free inodes $FREE_INODES required $LIMIT"
1082
1083         setup_quota_test || error "setup quota failed with $?"
1084         trap cleanup_quota_test EXIT
1085
1086         # enable mdt quota
1087         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1088
1089         # test for user
1090         log "User quota (inode hardlimit:$LIMIT files)"
1091         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
1092                 error "set user quota failed"
1093
1094         # make sure the system is clean
1095         local USED=$(getquota -u $TSTUSR global curinodes)
1096         [ $USED -ne 0 ] && error "Used inodes($USED) for user $TSTUSR isn't 0."
1097
1098         log "Create $LIMIT files ..."
1099         $RUNAS createmany -m ${TESTFILE} $LIMIT ||
1100                 quota_error u $TSTUSR "user create failure, but expect success"
1101         log "Create out of file quota ..."
1102         $RUNAS touch ${TESTFILE}_xxx &&
1103                 quota_error u $TSTUSR "user create success, but expect EDQUOT"
1104
1105         # cleanup
1106         unlinkmany ${TESTFILE} $LIMIT || error "unlinkmany $TESTFILE failed"
1107         rm -f ${TESTFILE}_xxx
1108         wait_delete_completed
1109
1110         USED=$(getquota -u $TSTUSR global curinodes)
1111         [ $USED -ne 0 ] && quota_error u $TSTUSR \
1112                 "user quota isn't released after deletion"
1113         resetquota -u $TSTUSR
1114
1115         # test for group
1116         log "--------------------------------------"
1117         log "Group quota (inode hardlimit:$LIMIT files)"
1118         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
1119                 error "set group quota failed"
1120
1121         TESTFILE=$DIR/$tdir/$tfile-1
1122         # make sure the system is clean
1123         USED=$(getquota -g $TSTUSR global curinodes)
1124         [ $USED -ne 0 ] && error "Used inodes($USED) for group $TSTUSR isn't 0."
1125
1126         log "Create $LIMIT files ..."
1127         $RUNAS createmany -m ${TESTFILE} $LIMIT ||
1128                 quota_error g $TSTUSR "group create failure, but expect success"
1129         log "Create out of file quota ..."
1130         $RUNAS touch ${TESTFILE}_xxx &&
1131                 quota_error g $TSTUSR "group create success, but expect EDQUOT"
1132
1133         # cleanup
1134         unlinkmany ${TESTFILE} $LIMIT || error "unlinkmany $TESTFILE failed"
1135         rm -f ${TESTFILE}_xxx
1136         wait_delete_completed
1137
1138         USED=$(getquota -g $TSTUSR global curinodes)
1139         [ $USED -ne 0 ] && quota_error g $TSTUSR \
1140                 "user quota isn't released after deletion"
1141
1142         resetquota -g $TSTUSR
1143         ! is_project_quota_supported && cleanup_quota_test &&
1144                 echo "Skip project quota is not supported" && return 0
1145
1146         # test for project
1147         log "--------------------------------------"
1148         log "Project quota (inode hardlimit:$LIMIT files)"
1149         $LFS setquota -p $TSTPRJID -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
1150                 error "set project quota failed"
1151
1152         TESTFILE=$DIR/$tdir/$tfile-1
1153         # make sure the system is clean
1154         USED=$(getquota -p $TSTPRJID global curinodes)
1155         [ $USED -ne 0 ] &&
1156                 error "Used inodes($USED) for project $TSTPRJID isn't 0"
1157
1158         change_project -sp $TSTPRJID $DIR/$tdir
1159         log "Create $LIMIT files ..."
1160         $RUNAS createmany -m ${TESTFILE} $((LIMIT-1)) || quota_error p \
1161                 $TSTPRJID "project create fail, but expect success"
1162         log "Create out of file quota ..."
1163         $RUNAS touch ${TESTFILE}_xxx && quota_error p $TSTPRJID \
1164                 "project create success, but expect EDQUOT"
1165         change_project -C $DIR/$tdir
1166
1167         cleanup_quota_test
1168         USED=$(getquota -p $TSTPRJID global curinodes)
1169         [ $USED -eq 0 ] || quota_error p $TSTPRJID \
1170                 "project quota isn't released after deletion"
1171
1172 }
1173 run_test 2 "File hard limit (normal use and out of quota)"
1174
1175 test_block_soft() {
1176         local testfile=$1
1177         local grace=$2
1178         local limit=$3
1179         local OFFSET=0
1180         local qtype=$4
1181         local pool=$5
1182
1183         setup_quota_test
1184         stack_trap cleanup_quota_test EXIT
1185
1186         $LFS setstripe $testfile -c 1 -i 0
1187         chown $TSTUSR.$TSTUSR $testfile
1188         [ "$qtype" == "p" ] && is_project_quota_supported &&
1189                 change_project -p $TSTPRJID $testfile
1190
1191         echo "Write up to soft limit"
1192         $RUNAS $DD of=$testfile count=$limit ||
1193                 quota_error a $TSTUSR "write failure, but expect success"
1194         OFFSET=$((limit * 1024))
1195         cancel_lru_locks osc
1196
1197         echo "Write to exceed soft limit"
1198         $RUNAS dd if=/dev/zero of=$testfile bs=1K count=10 seek=$OFFSET ||
1199                 quota_error a $TSTUSR "write failure, but expect success"
1200         OFFSET=$((OFFSET + 1024)) # make sure we don't write to same block
1201         cancel_lru_locks osc
1202
1203         echo "mmap write when over soft limit"
1204         $RUNAS $MULTIOP $testfile.mmap OT40960SMW ||
1205                 quota_error a $TSTUSR "mmap write failure, but expect success"
1206         cancel_lru_locks osc
1207
1208         $SHOW_QUOTA_USER
1209         $SHOW_QUOTA_GROUP
1210         $SHOW_QUOTA_PROJID
1211         $SHOW_QUOTA_INFO_USER
1212         $SHOW_QUOTA_INFO_GROUP
1213         $SHOW_QUOTA_INFO_PROJID
1214
1215         echo "Write before timer goes off"
1216         $RUNAS dd if=/dev/zero of=$testfile bs=1K count=10 seek=$OFFSET ||
1217                 quota_error a $TSTUSR "write failure, but expect success"
1218         OFFSET=$((OFFSET + 1024))
1219         cancel_lru_locks osc
1220
1221         wait_grace_time $qtype "block" $pool
1222
1223         $SHOW_QUOTA_USER
1224         $SHOW_QUOTA_GROUP
1225         $SHOW_QUOTA_PROJID
1226         $SHOW_QUOTA_INFO_USER
1227         $SHOW_QUOTA_INFO_GROUP
1228         $SHOW_QUOTA_INFO_PROJID
1229
1230         log "Write after timer goes off"
1231         # maybe cache write, ignore.
1232         $RUNAS dd if=/dev/zero of=$testfile bs=1K count=10 seek=$OFFSET || true
1233         OFFSET=$((OFFSET + 1024))
1234         cancel_lru_locks osc
1235         log "Write after cancel lru locks"
1236         $RUNAS dd if=/dev/zero of=$testfile bs=1K count=10 seek=$OFFSET &&
1237                 quota_error a $TSTUSR "write success, but expect EDQUOT"
1238
1239         $SHOW_QUOTA_USER
1240         $SHOW_QUOTA_GROUP
1241         $SHOW_QUOTA_PROJID
1242         $SHOW_QUOTA_INFO_USER
1243         $SHOW_QUOTA_INFO_GROUP
1244         $SHOW_QUOTA_INFO_PROJID
1245
1246         echo "Unlink file to stop timer"
1247         rm -f $testfile
1248         wait_delete_completed
1249         sync_all_data || true
1250
1251         $SHOW_QUOTA_USER
1252         $SHOW_QUOTA_GROUP
1253         $SHOW_QUOTA_PROJID
1254         $SHOW_QUOTA_INFO_USER
1255         $SHOW_QUOTA_INFO_GROUP
1256         $SHOW_QUOTA_INFO_PROJID
1257
1258         $LFS setstripe $testfile -c 1 -i 0
1259         chown $TSTUSR.$TSTUSR $testfile
1260         [ "$qtype" == "p" ] && change_project -p $TSTPRJID $testfile
1261
1262         echo "Write ..."
1263         $RUNAS $DD of=$testfile count=$limit ||
1264                 quota_error a $TSTUSR "write failure, but expect success"
1265         # cleanup
1266         cleanup_quota_test
1267 }
1268
1269 # block soft limit
1270 test_3a() {
1271         local grace=20 # 20s
1272         if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
1273             grace=60
1274         fi
1275         local testfile=$DIR/$tdir/$tfile-0
1276
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
1281         set_ost_qtype $QTYPE || error "enable ost quota failed"
1282
1283         echo "User quota (soft limit:$limit MB  grace:$grace seconds)"
1284         # make sure the system is clean
1285         local used=$(getquota -u $TSTUSR global curspace)
1286         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
1287
1288         $LFS setquota -t -u --block-grace $grace --inode-grace \
1289                 $MAX_IQ_TIME $DIR || error "set user grace time failed"
1290         $LFS setquota -u $TSTUSR -b ${limit}M -B 0 -i 0 -I 0 $DIR ||
1291                 error "set user quota failed"
1292
1293         test_block_soft $testfile $grace $limit "u"
1294
1295         echo "Group quota (soft limit:$limit MB  grace:$grace seconds)"
1296         testfile=$DIR/$tdir/$tfile-1
1297         # make sure the system is clean
1298         used=$(getquota -g $TSTUSR global curspace)
1299         [ $used -ne 0 ] && error "Used space($used) for group $TSTUSR isn't 0."
1300
1301         $LFS setquota -t -g --block-grace $grace --inode-grace \
1302                 $MAX_IQ_TIME $DIR || error "set group grace time failed"
1303         $LFS setquota -g $TSTUSR -b ${limit}M -B 0 -i 0 -I 0 $DIR ||
1304                 error "set group quota failed"
1305
1306         test_block_soft $testfile $grace $limit "g"
1307
1308         if is_project_quota_supported; then
1309                 echo "Project quota (soft limit:$limit MB  grace:$grace sec)"
1310                 testfile=$DIR/$tdir/$tfile-2
1311                 # make sure the system is clean
1312                 used=$(getquota -p $TSTPRJID global curspace)
1313                 [ $used -ne 0 ] && error \
1314                         "Used space($used) for project $TSTPRJID isn't 0."
1315
1316                 $LFS setquota -t -p --block-grace $grace --inode-grace \
1317                         $MAX_IQ_TIME $DIR ||
1318                                 error "set project grace time failed"
1319                 $LFS setquota -p $TSTPRJID -b ${limit}M -B 0 -i 0 -I 0 \
1320                         $DIR || error "set project quota failed"
1321
1322                 test_block_soft $testfile $grace $limit "p"
1323                 resetquota -p $TSTPRJID
1324                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
1325                         $MAX_IQ_TIME $DIR ||
1326                                 error "restore project grace time failed"
1327         fi
1328
1329         # cleanup
1330         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1331                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1332         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1333                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1334 }
1335 run_test 3a "Block soft limit (start timer, timer goes off, stop timer)"
1336
1337 test_3b() {
1338         local grace=20 # 20s
1339         local qpool="qpool1"
1340         if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
1341                 grace=60
1342         fi
1343         local testfile=$DIR/$tdir/$tfile-0
1344
1345         mds_supports_qp
1346         # get minimum soft qunit size
1347         local limit=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
1348                 qmt.$FSNAME-QMT0000.dt-0x0.soft_least_qunit) / 1024 ))
1349         local glbl_limit=$((2*limit))
1350         local glbl_grace=$((2*grace))
1351         echo "limit $limit glbl_limit $glbl_limit"
1352         echo "grace $grace glbl_grace $glbl_grace"
1353
1354         set_ost_qtype $QTYPE || error "enable ost quota failed"
1355
1356         echo "User quota in $qpool(soft limit:$limit MB  grace:$grace seconds)"
1357         # make sure the system is clean
1358         local used=$(getquota -u $TSTUSR global curspace)
1359         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
1360
1361         pool_add $qpool || error "pool_add failed"
1362         pool_add_targets $qpool 0 1 ||
1363                 error "pool_add_targets failed"
1364
1365         $LFS setquota -t -u --block-grace $glbl_grace --inode-grace \
1366                 $MAX_IQ_TIME $DIR || error "set user grace time failed"
1367         $LFS setquota -t -u --block-grace $grace \
1368                 --pool $qpool $DIR || error "set user grace time failed"
1369
1370         $LFS setquota -u $TSTUSR -b ${glbl_limit}M -B 0 -i 0 -I 0 $DIR ||
1371                 error "set user quota failed"
1372         $LFS setquota -u $TSTUSR -b ${limit}M -B 0 --pool $qpool $DIR ||
1373                 error "set user quota failed"
1374
1375         test_block_soft $testfile $grace $limit "u" $qpool
1376
1377         echo "Group quota in $qpool(soft limit:$limit MB  grace:$grace seconds)"
1378         testfile=$DIR/$tdir/$tfile-1
1379         # make sure the system is clean
1380         used=$(getquota -g $TSTUSR global curspace)
1381         [ $used -ne 0 ] && error "Used space($used) for group $TSTUSR isn't 0."
1382
1383         $LFS setquota -t -g --block-grace $glbl_grace --inode-grace \
1384                 $MAX_IQ_TIME $DIR || error "set group grace time failed"
1385         $LFS setquota -t -g --block-grace $grace \
1386                 --pool $qpool $DIR || error "set group grace time failed"
1387
1388         $LFS setquota -g $TSTUSR -b ${glbl_limit}M -B 0 -i 0 -I 0 $DIR ||
1389                 error "set group quota failed"
1390         $LFS setquota -g $TSTUSR -b ${limit}M -B 0 --pool $qpool $DIR ||
1391                 error "set group quota failed"
1392
1393         test_block_soft $testfile $grace $limit "g" $qpool
1394
1395         if is_project_quota_supported; then
1396                 echo "Project quota in $qpool(soft:$limit MB  grace:$grace sec)"
1397                 testfile=$DIR/$tdir/$tfile-2
1398                 # make sure the system is clean
1399                 used=$(getquota -p $TSTPRJID global curspace)
1400                 [ $used -ne 0 ] && error \
1401                         "Used space($used) for project $TSTPRJID isn't 0."
1402
1403                 $LFS setquota -t -p --block-grace $glbl_grace --inode-grace \
1404                         $MAX_IQ_TIME $DIR ||
1405                                 error "set project grace time failed"
1406                 $LFS setquota -t -p --block-grace $grace \
1407                         --pool $qpool $DIR ||
1408                                 error "set project grace time failed"
1409
1410                 $LFS setquota -p $TSTPRJID -b ${glbl_limit}M -B 0 -i 0 -I 0 \
1411                         $DIR || error "set project quota failed"
1412                 $LFS setquota -p $TSTPRJID -b ${limit}M -B 0 \
1413                         --pool $qpool $DIR || error "set project quota failed"
1414
1415                 test_block_soft $testfile $grace $limit "p" $qpool
1416                 resetquota -p $TSTPRJID
1417                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
1418                         $MAX_IQ_TIME $DIR ||
1419                                 error "restore project grace time failed"
1420                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --pool $qpool \
1421                         $DIR || error "set project grace time failed"
1422         fi
1423
1424         # cleanup
1425         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1426                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1427         $LFS setquota -t -u --block-grace $MAX_DQ_TIME \
1428                 --pool $qpool $DIR || error "restore user grace time failed"
1429         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1430                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1431         $LFS setquota -t -g --block-grace $MAX_DQ_TIME \
1432                 --pool $qpool $DIR || error "restore group grace time failed"
1433 }
1434 run_test 3b "Quota pools: Block soft limit (start timer, expires, stop timer)"
1435
1436 test_3c() {
1437         local grace=20 # 20s
1438         local qpool="qpool1"
1439         local qpool2="qpool2"
1440         if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
1441                 grace=60
1442         fi
1443         local testfile=$DIR/$tdir/$tfile-0
1444
1445         mds_supports_qp
1446         # get minimum soft qunit size
1447         local limit=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
1448                 qmt.$FSNAME-QMT0000.dt-0x0.soft_least_qunit) / 1024 ))
1449         local limit2=$((limit+4))
1450         local glbl_limit=$((limit+8))
1451         local grace1=$((grace+10))
1452         local grace2=$grace
1453         local glbl_grace=$((grace+20))
1454         echo "limit $limit limit2 $limit2 glbl_limit $glbl_limit"
1455         echo "grace1 $grace1 grace2 $grace2 glbl_grace $glbl_grace"
1456
1457         set_ost_qtype $QTYPE || error "enable ost quota failed"
1458
1459         echo "User quota in qpool2(soft:$limit2 MB grace:$grace2 seconds)"
1460         # make sure the system is clean
1461         local used=$(getquota -u $TSTUSR global curspace)
1462         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
1463
1464         pool_add $qpool || error "pool_add failed"
1465         pool_add_targets $qpool 0 1 ||
1466                 error "pool_add_targets failed"
1467
1468         pool_add $qpool2 || error "pool_add failed"
1469         pool_add_targets $qpool2 0 1 ||
1470                 error "pool_add_targets failed"
1471
1472
1473         $LFS setquota -t -u --block-grace $glbl_grace --inode-grace \
1474                 $MAX_IQ_TIME $DIR || error "set user grace time failed"
1475         $LFS setquota -t -u --block-grace $grace1 \
1476                 --pool $qpool $DIR || error "set user grace time failed"
1477         $LFS setquota -t -u --block-grace $grace2 \
1478                 --pool $qpool2 $DIR || error "set user grace time failed"
1479
1480         $LFS setquota -u $TSTUSR -b ${glbl_limit}M -B 0 -i 0 -I 0 $DIR ||
1481                 error "set user quota failed"
1482         $LFS setquota -u $TSTUSR -b ${limit}M -B 0 --pool $qpool $DIR ||
1483                 error "set user quota failed"
1484         # qpool has minimum soft limit, but it's grace is grater than
1485         # grace period of qpool2. Thus write shouldn't fail when
1486         # hit qpool soft limit - only when reaches up qpool2 limit
1487         # after grace2 seconds.
1488         $LFS setquota -u $TSTUSR -b ${limit2}M -B 0 --pool $qpool2 $DIR ||
1489                 error "set user quota failed"
1490
1491         test_block_soft $testfile $grace2 $limit2 "u" $qpool2
1492
1493         # cleanup
1494         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1495                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1496         $LFS setquota -t -u --block-grace $MAX_DQ_TIME \
1497                 --pool $qpool $DIR || error "restore user grace time failed"
1498         $LFS setquota -t -u --block-grace $MAX_DQ_TIME \
1499                 --pool $qpool2 $DIR || error "restore user grace time failed"
1500 }
1501 run_test 3c "Quota pools: check block soft limit on different pools"
1502
1503 test_file_soft() {
1504         local TESTFILE=$1
1505         local LIMIT=$2
1506         local grace=$3
1507         local qtype=$4
1508
1509         setup_quota_test
1510         trap cleanup_quota_test EXIT
1511         is_project_quota_supported && change_project -sp $TSTPRJID $DIR/$tdir
1512
1513         echo "Create files to exceed soft limit"
1514         $RUNAS createmany -m ${TESTFILE}_ $((LIMIT + 1)) ||
1515                 quota_error a $TSTUSR "create failure, but expect success"
1516         local trigger_time=$(date +%s)
1517
1518         sync_all_data || true
1519
1520         local cur_time=$(date +%s)
1521         [ $(($cur_time - $trigger_time)) -ge $grace ] &&
1522                 error "Passed grace time $grace, $trigger_time, $cur_time"
1523
1524         echo "Create file before timer goes off"
1525         $RUNAS touch ${TESTFILE}_before ||
1526                 quota_error a $TSTUSR "failed create before timer expired," \
1527                         "but expect success. $trigger_time, $cur_time"
1528         sync_all_data || true
1529
1530         wait_grace_time $qtype "file"
1531
1532         $SHOW_QUOTA_USER
1533         $SHOW_QUOTA_GROUP
1534         $SHOW_QUOTA_PROJID
1535         $SHOW_QUOTA_INFO_USER
1536         $SHOW_QUOTA_INFO_GROUP
1537         $SHOW_QUOTA_INFO_PROJID
1538
1539         echo "Create file after timer goes off"
1540         # There is a window that space is accounted in the quota usage but
1541         # hasn't been decreased from the pending write, if we acquire quota
1542         # in this window, we'll acquire more than we needed.
1543         $RUNAS touch ${TESTFILE}_after_1 ${TESTFILE}_after_2 || true
1544         sync_all_data || true
1545         $RUNAS touch ${TESTFILE}_after_3 &&
1546                 quota_error a $TSTUSR "create after timer expired," \
1547                         "but expect EDQUOT"
1548         sync_all_data || true
1549
1550         $SHOW_QUOTA_USER
1551         $SHOW_QUOTA_GROUP
1552         $SHOW_QUOTA_PROJID
1553         $SHOW_QUOTA_INFO_USER
1554         $SHOW_QUOTA_INFO_GROUP
1555         $SHOW_QUOTA_INFO_PROJID
1556
1557         echo "Unlink files to stop timer"
1558         find $(dirname $TESTFILE) -name "$(basename ${TESTFILE})*" | xargs rm -f
1559         wait_delete_completed
1560
1561         echo "Create file"
1562         $RUNAS touch ${TESTFILE}_xxx ||
1563                 quota_error a $TSTUSR "touch after timer stop failure," \
1564                         "but expect success"
1565         sync_all_data || true
1566
1567         # cleanup
1568         cleanup_quota_test
1569 }
1570
1571 # file soft limit
1572 test_4a() {
1573         local LIMIT=$(do_facet $SINGLEMDS $LCTL get_param -n \
1574                 qmt.$FSNAME-QMT0000.md-0x0.soft_least_qunit)
1575         local TESTFILE=$DIR/$tdir/$tfile-0
1576         local GRACE=12
1577
1578         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1579
1580         echo "User quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
1581         # make sure the system is clean
1582         local USED=$(getquota -u $TSTUSR global curinodes)
1583         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1584
1585         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1586                 $GRACE $DIR || error "set user grace time failed"
1587         $LFS setquota -u $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
1588                 error "set user quota failed"
1589
1590         [ "$mds1_FSTYPE" = zfs ] && GRACE=20
1591
1592         test_file_soft $TESTFILE $LIMIT $GRACE "u"
1593
1594         echo "Group quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
1595         # make sure the system is clean
1596         USED=$(getquota -g $TSTUSR global curinodes)
1597         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
1598
1599         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1600                 $GRACE $DIR || error "set group grace time failed"
1601         $LFS setquota -g $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
1602                 error "set group quota failed"
1603         TESTFILE=$DIR/$tdir/$tfile-1
1604
1605         test_file_soft $TESTFILE $LIMIT $GRACE "g"
1606
1607         if is_project_quota_supported; then
1608                 echo "Project quota (soft limit:$LIMIT files grace:$GRACE sec)"
1609                 # make sure the system is clean
1610                 USED=$(getquota -p $TSTPRJID global curinodes)
1611                 [ $USED -ne 0 ] && error \
1612                         "Used space($USED) for project $TSTPRJID isn't 0."
1613
1614                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
1615                         $GRACE $DIR || error "set project grace time failed"
1616                 $LFS setquota -p $TSTPRJID -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
1617                         error "set project quota failed"
1618
1619                 TESTFILE=$DIR/$tdir/$tfile-1
1620                 # one less than limit, because of parent directory included.
1621                 test_file_soft $TESTFILE $((LIMIT-1)) $GRACE "p"
1622                 resetquota -p $TSTPRJID
1623                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
1624                         $MAX_IQ_TIME $DIR ||
1625                                 error "restore project grace time failed"
1626         fi
1627
1628         # cleanup
1629         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1630                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1631         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1632                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1633 }
1634 run_test 4a "File soft limit (start timer, timer goes off, stop timer)"
1635
1636 test_4b() {
1637         local GR_STR1="1w3d"
1638         local GR_STR2="1000s"
1639         local GR_STR3="5s"
1640         local GR_STR4="1w2d3h4m5s"
1641         local GR_STR5="5c"
1642         local GR_STR6="18446744073709551615"
1643         local GR_STR7="-1"
1644
1645         wait_delete_completed
1646
1647         # test of valid grace strings handling
1648         echo "Valid grace strings test"
1649         $LFS setquota -t -u --block-grace $GR_STR1 --inode-grace \
1650                 $GR_STR2 $DIR || error "set user grace time failed"
1651         $LFS quota -u -t $DIR | grep "Block grace time: $GR_STR1"
1652         $LFS setquota -t -g --block-grace $GR_STR3 --inode-grace \
1653                 $GR_STR4 $DIR || error "set group grace time quota failed"
1654         $LFS quota -g -t $DIR | grep "Inode grace time: $GR_STR4"
1655
1656         # test of invalid grace strings handling
1657         echo "  Invalid grace strings test"
1658         ! $LFS setquota -t -u --block-grace $GR_STR4 --inode-grace $GR_STR5 $DIR
1659         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace $GR_STR6 $DIR
1660         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace \
1661                 $GR_STR7 $DIR
1662
1663         # cleanup
1664         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1665                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1666         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1667                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1668 }
1669 run_test 4b "Grace time strings handling"
1670
1671 # chown & chgrp (chown & chgrp successfully even out of block/file quota)
1672 test_5() {
1673         local BLIMIT=10 # 10M
1674         local ILIMIT=10 # 10 inodes
1675
1676         setup_quota_test || error "setup quota failed with $?"
1677         trap cleanup_quota_test EXIT
1678
1679         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1680         set_ost_qtype $QTYPE || error "enable ost quota failed"
1681
1682         echo "Set quota limit (0 ${BLIMIT}M 0 $ILIMIT) for $TSTUSR.$TSTUSR"
1683         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
1684                 error "set user quota failed"
1685         $LFS setquota -g $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
1686         if is_project_quota_supported; then
1687                 error "set group quota failed"
1688                 $LFS setquota -p $TSTPRJID -b 0 -B ${BLIMIT}M -i 0 \
1689                         -I $ILIMIT $DIR || error "set project quota failed"
1690         fi
1691
1692         # make sure the system is clean
1693         local USED=$(getquota -u $TSTUSR global curinodes)
1694         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
1695         USED=$(getquota -g $TSTUSR global curinodes)
1696         [ $USED -ne 0 ] && error "Used inode($USED) for group $TSTUSR isn't 0."
1697         USED=$(getquota -u $TSTUSR global curspace)
1698         [ $USED -ne 0 ] && error "Used block($USED) for user $TSTUSR isn't 0."
1699         USED=$(getquota -g $TSTUSR global curspace)
1700         [ $USED -ne 0 ] && error "Used block($USED) for group $TSTUSR isn't 0."
1701         if is_project_quota_supported; then
1702                 USED=$(getquota -p $TSTPRJID global curinodes)
1703                 [ $USED -ne 0 ] &&
1704                         error "Used inode($USED) for project $TSTPRJID isn't 0."
1705                 USED=$(getquota -p $TSTPRJID global curspace)
1706                 [ $USED -ne 0 ] &&
1707                         error "Used block($USED) for project $TSTPRJID isn't 0."
1708         fi
1709
1710         echo "Create more than $ILIMIT files and more than $BLIMIT MB ..."
1711         createmany -m $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
1712                 error "create failure, expect success"
1713         if is_project_quota_supported; then
1714                 touch $DIR/$tdir/$tfile-0_1
1715                 change_project -p $TSTPRJID $DIR/$tdir/$tfile-0_1
1716         fi
1717         $DD of=$DIR/$tdir/$tfile-0_1 count=$((BLIMIT+1)) ||
1718                 error "write failure, expect success"
1719
1720         echo "Chown files to $TSTUSR.$TSTUSR ..."
1721         for i in $(seq 0 $ILIMIT); do
1722                 chown $TSTUSR.$TSTUSR $DIR/$tdir/$tfile-0_$i ||
1723                         quota_error a $TSTUSR "chown failure, expect success"
1724         done
1725
1726         # cleanup
1727         unlinkmany $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
1728                 error "unlinkmany $DIR/$tdir/$tfile-0_ failed"
1729         cleanup_quota_test
1730 }
1731 run_test 5 "Chown & chgrp successfully even out of block/file quota"
1732
1733 # test dropping acquire request on master
1734 test_6() {
1735         local LIMIT=3 # 3M
1736
1737         # Clear dmesg so watchdog is not triggered by previous
1738         # test output
1739         do_facet ost1 dmesg -c > /dev/null
1740
1741         setup_quota_test || error "setup quota failed with $?"
1742         trap cleanup_quota_test EXIT
1743
1744         # make sure the system is clean
1745         local USED=$(getquota -u $TSTUSR global curspace)
1746         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1747
1748         # make sure no granted quota on ost
1749         set_ost_qtype $QTYPE || error "enable ost quota failed"
1750         resetquota -u $TSTUSR
1751
1752         # create file for $TSTUSR
1753         local TESTFILE=$DIR/$tdir/$tfile-$TSTUSR
1754         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1755         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1756
1757         # create file for $TSTUSR2
1758         local TESTFILE2=$DIR/$tdir/$tfile-$TSTUSR2
1759         $LFS setstripe $TESTFILE2 -c 1 -i 0 || error "setstripe $TESTFILE2 failed"
1760         chown $TSTUSR2.$TSTUSR2 $TESTFILE2 || error "chown $TESTFILE2 failed"
1761
1762         # cache per-ID lock for $TSTUSR on slave
1763         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1764                 error "set quota failed"
1765         $RUNAS $DD of=$TESTFILE count=1 ||
1766                 error "write $TESTFILE failure, expect success"
1767         $RUNAS2 $DD of=$TESTFILE2 count=1 ||
1768                 error "write $TESTFILE2 failure, expect success"
1769
1770         if at_is_enabled; then
1771                 at_max_saved=$(at_max_get ost1)
1772                 at_max_set $TIMEOUT ost1
1773
1774                 # write to enforced ID ($TSTUSR) to exceed limit to make sure
1775                 # DQACQ is sent, which makes at_max to take effect
1776                 $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync \
1777                                                                 conv=notrunc
1778                 rm -f $TESTFILE
1779                 wait_delete_completed
1780         fi
1781
1782         sync; sync
1783         sync_all_data || true
1784
1785         #define QUOTA_DQACQ 601
1786         #define OBD_FAIL_PTLRPC_DROP_REQ_OPC 0x513
1787         lustre_fail mds 0x513 601
1788
1789         do_facet ost1 $LCTL set_param \
1790                         osd-*.$FSNAME-OST*.quota_slave.timeout=$((TIMEOUT / 2))
1791
1792         # write to un-enforced ID ($TSTUSR2) should succeed
1793         $RUNAS2 $DD of=$TESTFILE2 count=$LIMIT seek=1 oflag=sync conv=notrunc ||
1794                 error "write failure, expect success"
1795
1796         # write to enforced ID ($TSTUSR) in background, exceeding limit
1797         # to make sure DQACQ is sent
1798         $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync conv=notrunc &
1799         DDPID=$!
1800
1801         # watchdog timer uses a factor of 2
1802         echo "Sleep for $((TIMEOUT * 2 + 1)) seconds ..."
1803         sleep $((TIMEOUT * 2 + 1))
1804
1805         [ $at_max_saved -ne 0 ] && at_max_set $at_max_saved ost1
1806
1807         # write should be blocked and never finished
1808         if ! ps -p $DDPID  > /dev/null 2>&1; then
1809                 lustre_fail mds 0 0
1810                 error "write finished incorrectly!"
1811         fi
1812
1813         lustre_fail mds 0 0
1814
1815         # no watchdog is triggered
1816         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1817         watchdog=$(awk '/[Ss]ervice thread pid/ && /was inactive/ \
1818                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
1819         [ -z "$watchdog" ] || error "$watchdog"
1820
1821         rm -f $TMP/lustre-log-${TESTNAME}.log
1822
1823         # write should continue then fail with EDQUOT
1824         local count=0
1825         local c_size
1826         while [ true ]; do
1827                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1828                 if [ $count -ge 240 ]; then
1829                         quota_error u $TSTUSR "dd not finished in $count secs"
1830                 fi
1831                 count=$((count + 1))
1832                 if [ $((count % 30)) -eq 0 ]; then
1833                         c_size=$(stat -c %s $TESTFILE)
1834                         echo "Waiting $count secs. $c_size"
1835                         $SHOW_QUOTA_USER
1836                 fi
1837                 sleep 1
1838         done
1839
1840         cleanup_quota_test
1841 }
1842 run_test 6 "Test dropping acquire request on master"
1843
1844 # quota reintegration (global index)
1845 test_7a() {
1846         local TESTFILE=$DIR/$tdir/$tfile
1847         local LIMIT=20 # 20M
1848
1849         [ "$SLOW" = "no" ] && LIMIT=5
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         echo "Stop ost1..."
1873         stop ost1
1874
1875         echo "Enable quota & set quota limit for $TSTUSR"
1876         set_ost_qtype $QTYPE || error "enable ost quota failed"
1877         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1878                 error "set quota failed"
1879
1880         echo "Start ost1..."
1881         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1882         quota_init
1883
1884         wait_ost_reint $QTYPE || error "reintegration failed"
1885
1886         # hardlimit should have been fetched by slave during global
1887         # reintegration, write will exceed quota
1888         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1889                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1890
1891         rm -f $TESTFILE
1892         wait_delete_completed
1893         sync_all_data || true
1894         sleep 3
1895
1896         echo "Stop ost1..."
1897         stop ost1
1898
1899         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
1900                 error "clear quota failed"
1901
1902         echo "Start ost1..."
1903         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1904         quota_init
1905
1906         wait_ost_reint $QTYPE || error "reintegration failed"
1907
1908         # hardlimit should be cleared on slave during reintegration
1909         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync ||
1910                 quota_error u $TSTUSR "write error, but expect success"
1911
1912         cleanup_quota_test
1913 }
1914 run_test 7a "Quota reintegration (global index)"
1915
1916 # quota reintegration (slave index)
1917 test_7b() {
1918         local LIMIT="100G"
1919         local TESTFILE=$DIR/$tdir/$tfile
1920
1921         setup_quota_test || error "setup quota failed with $?"
1922         trap cleanup_quota_test EXIT
1923
1924         # make sure the system is clean
1925         local USED=$(getquota -u $TSTUSR global curspace)
1926         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1927
1928         # make sure no granted quota on ost1
1929         set_ost_qtype $QTYPE || error "enable ost quota failed"
1930         resetquota -u $TSTUSR
1931         set_ost_qtype "none" || error "disable ost quota failed"
1932
1933         local OSTUUID=$(ostuuid_from_index 0)
1934         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1935         [ $USED -ne 0 ] &&
1936                 error "limit($USED) on $OSTUUID for user $TSTUSR isn't 0"
1937
1938         # create test file
1939         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1940         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1941
1942         # consume some space to make sure the granted space will not
1943         # be released during reconciliation
1944         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1945                 error "consume space failure, expect success"
1946
1947         # define OBD_FAIL_QUOTA_EDQUOT 0xa02
1948         lustre_fail mds 0xa02
1949
1950         set_ost_qtype $QTYPE || error "enable ost quota failed"
1951         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1952                 error "set quota failed"
1953
1954         # ignore the write error
1955         $RUNAS $DD of=$TESTFILE count=1 seek=1 oflag=sync conv=notrunc
1956
1957         local old_used=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1958
1959         lustre_fail mds 0
1960
1961         echo "Restart ost to trigger reintegration..."
1962         stop ost1
1963         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1964         quota_init
1965
1966         wait_ost_reint $QTYPE || error "reintegration failed"
1967
1968         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1969         [ $USED -gt $old_used ] || error "limit on $OSTUUID $USED <= $old_used"
1970
1971         cleanup_quota_test
1972         $SHOW_QUOTA_USER
1973 }
1974 run_test 7b "Quota reintegration (slave index)"
1975
1976 # quota reintegration (restart mds during reintegration)
1977 test_7c() {
1978         local LIMIT=20 # 20M
1979         local TESTFILE=$DIR/$tdir/$tfile
1980
1981         [ "$SLOW" = "no" ] && LIMIT=5
1982
1983         setup_quota_test || error "setup quota failed with $?"
1984         trap cleanup_quota_test EXIT
1985
1986         # make sure the system is clean
1987         local USED=$(getquota -u $TSTUSR global curspace)
1988         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1989
1990         set_ost_qtype "none" || error "disable ost quota failed"
1991         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1992                 error "set quota failed"
1993
1994         # define OBD_FAIL_QUOTA_DELAY_REINT 0xa03
1995         lustre_fail ost 0xa03
1996
1997         # enable ost quota
1998         set_ost_qtype $QTYPE || error "enable ost quota failed"
1999         # trigger reintegration
2000         local procf="osd-$ost1_FSTYPE.$FSNAME-OST*."
2001         procf=${procf}quota_slave.force_reint
2002         do_facet ost1 $LCTL set_param $procf=1 ||
2003                 error "force reintegration failed"
2004
2005         echo "Stop mds..."
2006         stop mds1
2007
2008         lustre_fail ost 0
2009
2010         echo "Start mds..."
2011         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
2012         quota_init
2013
2014         # wait longer than usual to make sure the reintegration
2015         # is triggered by quota wb thread.
2016         wait_ost_reint $QTYPE 200 || error "reintegration failed"
2017
2018         # hardlimit should have been fetched by slave during global
2019         # reintegration, write will exceed quota
2020         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
2021                 quota_error u $TSTUSR "write success, but expect EDQUOT"
2022
2023         cleanup_quota_test
2024 }
2025 run_test 7c "Quota reintegration (restart mds during reintegration)"
2026
2027 # Quota reintegration (Transfer index in multiple bulks)
2028 test_7d(){
2029         local TESTFILE=$DIR/$tdir/$tfile
2030         local TESTFILE1="$DIR/$tdir/$tfile"-1
2031         local limit=20 #20M
2032
2033         setup_quota_test || error "setup quota failed with $?"
2034         trap cleanup_quota_test EXIT
2035
2036         set_ost_qtype "none" || error "disable ost quota failed"
2037         $LFS setquota -u $TSTUSR -B ${limit}M $DIR ||
2038                 error "set quota for $TSTUSR failed"
2039         $LFS setquota -u $TSTUSR2 -B ${limit}M $DIR ||
2040                 error "set quota for $TSTUSR2 failed"
2041
2042         #define OBD_FAIL_OBD_IDX_READ_BREAK 0x608
2043         lustre_fail mds 0x608 0
2044
2045         # enable quota to tirgger reintegration
2046         set_ost_qtype "u" || error "enable ost quota failed"
2047         wait_ost_reint "u" || error "reintegration failed"
2048
2049         lustre_fail mds 0
2050
2051         # hardlimit should have been fetched by slave during global
2052         # reintegration, write will exceed quota
2053         $RUNAS $DD of=$TESTFILE count=$((limit + 1)) oflag=sync &&
2054                 quota_error u $TSTUSR "$TSTUSR write success, expect EDQUOT"
2055
2056         $RUNAS2 $DD of=$TESTFILE1 count=$((limit + 1)) oflag=sync &&
2057                 quota_error u $TSTUSR2 "$TSTUSR2 write success, expect EDQUOT"
2058
2059         cleanup_quota_test
2060 }
2061 run_test 7d "Quota reintegration (Transfer index in multiple bulks)"
2062
2063 # quota reintegration (inode limits)
2064 test_7e() {
2065         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
2066
2067         # LU-2435: skip this quota test if underlying zfs version has not
2068         # supported native dnode accounting
2069         [ "$mds1_FSTYPE" == zfs ] && {
2070                 local F="feature@userobj_accounting"
2071                 local pool=$(zpool_name mds1)
2072                 local feature=$(do_facet mds1 $ZPOOL get -H $F $pool)
2073
2074                 [[ "$feature" != *" active "* ]] &&
2075                         skip "requires zpool with active userobj_accounting"
2076         }
2077
2078         local ilimit=$((1024 * 2)) # 2k inodes
2079         local TESTFILE=$DIR/${tdir}-1/$tfile
2080
2081         setup_quota_test || error "setup quota failed with $?"
2082         trap cleanup_quota_test EXIT
2083
2084         # make sure the system is clean
2085         local USED=$(getquota -u $TSTUSR global curinodes)
2086         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
2087
2088         # make sure no granted quota on mdt1
2089         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
2090         resetquota -u $TSTUSR
2091         set_mdt_qtype "none" || error "disable mdt quota failed"
2092
2093         local MDTUUID=$(mdtuuid_from_index $((MDSCOUNT - 1)))
2094         USED=$(getquota -u $TSTUSR $MDTUUID ihardlimit)
2095         [ $USED -ne 0 ] && error "limit($USED) on $MDTUUID for user" \
2096                 "$TSTUSR isn't 0."
2097
2098         echo "Stop mds${MDSCOUNT}..."
2099         stop mds${MDSCOUNT}
2100
2101         echo "Enable quota & set quota limit for $TSTUSR"
2102         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
2103         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
2104                 error "set quota failed"
2105
2106         echo "Start mds${MDSCOUNT}..."
2107         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
2108         quota_init
2109
2110         wait_mdt_reint $QTYPE || error "reintegration failed"
2111
2112         echo "create remote dir"
2113         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/${tdir}-1 ||
2114                 error "create remote dir failed"
2115         chmod 0777 $DIR/${tdir}-1
2116
2117         # hardlimit should have been fetched by slave during global
2118         # reintegration, create will exceed quota
2119         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) &&
2120                 quota_error u $TSTUSR "create succeeded, expect EDQUOT"
2121
2122         $RUNAS unlinkmany $TESTFILE $ilimit || error "unlink files failed"
2123         wait_delete_completed
2124         sync_all_data || true
2125
2126         echo "Stop mds${MDSCOUNT}..."
2127         stop mds${MDSCOUNT}
2128
2129         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
2130                 error "clear quota failed"
2131
2132         echo "Start mds${MDSCOUNT}..."
2133         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
2134         quota_init
2135
2136         wait_mdt_reint $QTYPE || error "reintegration failed"
2137
2138         # hardlimit should be cleared on slave during reintegration
2139         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) ||
2140                 quota_error u $TSTUSR "create failed, expect success"
2141
2142         $RUNAS unlinkmany $TESTFILE $((ilimit + 1)) || error "unlink failed"
2143         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
2144
2145         cleanup_quota_test
2146 }
2147 run_test 7e "Quota reintegration (inode limits)"
2148
2149 # run dbench with quota enabled
2150 test_8() {
2151         local BLK_LIMIT="100g" #100G
2152         local FILE_LIMIT=1000000
2153
2154         setup_quota_test || error "setup quota failed with $?"
2155         trap cleanup_quota_test EXIT
2156
2157         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
2158         set_ost_qtype $QTYPE || error "enable ost quota failed"
2159
2160         echo "Set enough high limit for user: $TSTUSR"
2161         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
2162                 error "set user quota failed"
2163         echo "Set enough high limit for group: $TSTUSR"
2164         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
2165                 error "set group quota failed"
2166         if is_project_quota_supported; then
2167                 change_project -sp $TSTPRJID $DIR/$tdir
2168                 echo "Set enough high limit for project: $TSTPRJID"
2169                 $LFS setquota -p $TSTPRJID -b 0 \
2170                         -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
2171                         error "set project quota failed"
2172         fi
2173
2174         local duration=""
2175         [ "$SLOW" = "no" ] && duration=" -t 120"
2176         $RUNAS bash rundbench -D $DIR/$tdir 3 $duration ||
2177                 quota_error a $TSTUSR "dbench failed!"
2178
2179         is_project_quota_supported && change_project -C $DIR/$tdir
2180         cleanup_quota_test
2181 }
2182 run_test 8 "Run dbench with quota enabled"
2183
2184 # this check is just for test_9
2185 OST0_MIN=4900000 #4.67G
2186
2187 check_whether_skip () {
2188         local OST0_SIZE=$($LFS df $DIR | awk '/\[OST:0\]/ {print $4}')
2189         log "OST0_SIZE: $OST0_SIZE  required: $OST0_MIN"
2190         if [ $OST0_SIZE -lt $OST0_MIN ]; then
2191                 echo "WARN: OST0 has less than $OST0_MIN free, skip this test."
2192                 return 0
2193         else
2194                 return 1
2195         fi
2196 }
2197
2198 # run for fixing bug10707, it needs a big room. test for 64bit
2199 test_9() {
2200         local filesize=$((1024 * 9 / 2)) # 4.5G
2201
2202         check_whether_skip && return 0
2203
2204         setup_quota_test || error "setup quota failed with $?"
2205         trap cleanup_quota_test EXIT
2206
2207         set_ost_qtype "ug" || error "enable ost quota failed"
2208
2209         local TESTFILE="$DIR/$tdir/$tfile-0"
2210         local BLK_LIMIT=100G #100G
2211         local FILE_LIMIT=1000000
2212
2213         echo "Set block limit $BLK_LIMIT bytes to $TSTUSR.$TSTUSR"
2214
2215         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
2216                 "for user: $TSTUSR"
2217         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
2218                 error "set user quota failed"
2219
2220         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
2221                 "for group: $TSTUSR"
2222         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
2223                 error "set group quota failed"
2224
2225         quota_show_check a u $TSTUSR
2226         quota_show_check a g $TSTUSR
2227
2228         echo "Create test file"
2229         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
2230         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2231
2232         log "Write the big file of 4.5G ..."
2233         $RUNAS $DD of=$TESTFILE count=$filesize ||
2234                 quota_error a $TSTUSR "write 4.5G file failure, expect success"
2235
2236         $SHOW_QUOTA_USER
2237         $SHOW_QUOTA_GROUP
2238
2239         cleanup_quota_test
2240
2241         $SHOW_QUOTA_USER
2242         $SHOW_QUOTA_GROUP
2243 }
2244 run_test 9 "Block limit larger than 4GB (b10707)"
2245
2246 test_10() {
2247         local TESTFILE=$DIR/$tdir/$tfile
2248
2249         setup_quota_test || error "setup quota failed with $?"
2250         trap cleanup_quota_test EXIT
2251
2252         # set limit to root user should fail
2253         $LFS setquota -u root -b 100G -B 500G -i 1K -I 1M $DIR &&
2254                 error "set limit for root user successfully, expect failure"
2255         $LFS setquota -g root -b 1T -B 10T -i 5K -I 100M $DIR &&
2256                 error "set limit for root group successfully, expect failure"
2257         $LFS setquota -p 0 -b 1T -B 10T -i 5K -I 100M $DIR &&
2258                 error "set limit for project 0 successfully, expect failure"
2259
2260         # root user can overrun quota
2261         set_ost_qtype "ug" || error "enable ost quota failed"
2262
2263         $LFS setquota -u $TSTUSR -b 0 -B 2M -i 0 -I 0 $DIR ||
2264                 error "set quota failed"
2265         quota_show_check b u $TSTUSR
2266
2267         $LFS setstripe $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
2268         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2269
2270         runas -u 0 -g 0 $DD of=$TESTFILE count=3 oflag=sync ||
2271                 error "write failure, expect success"
2272
2273         cleanup_quota_test
2274 }
2275 run_test 10 "Test quota for root user"
2276
2277 test_11() {
2278         local TESTFILE=$DIR/$tdir/$tfile
2279         setup_quota_test || error "setup quota failed with $?"
2280         trap cleanup_quota_test EXIT
2281
2282         set_mdt_qtype "ug" || error "enable mdt quota failed"
2283         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 1 $DIR ||
2284                 error "set quota failed"
2285
2286         touch "$TESTFILE"-0 || error "touch $TESTFILE-0 failed"
2287         touch "$TESTFILE"-1 || error "touch $TESTFILE-0 failed"
2288
2289         chown $TSTUSR.$TSTUSR "$TESTFILE"-0 || error "chown $TESTFILE-0 failed"
2290         chown $TSTUSR.$TSTUSR "$TESTFILE"-1 || error "chown $TESTFILE-1 failed"
2291
2292         $SHOW_QUOTA_USER
2293         local USED=$(getquota -u $TSTUSR global curinodes)
2294         [ $USED -ge 2 ] || error "Used inodes($USED) is less than 2"
2295
2296         cleanup_quota_test
2297 }
2298 run_test 11 "Chown/chgrp ignores quota"
2299
2300 test_12a() {
2301         [ "$OSTCOUNT" -lt "2" ] && skip "needs >= 2 OSTs"
2302
2303         local blimit=22 # 22M
2304         local blk_cnt=$((blimit - 5))
2305         local TESTFILE0="$DIR/$tdir/$tfile"-0
2306         local TESTFILE1="$DIR/$tdir/$tfile"-1
2307
2308         setup_quota_test || error "setup quota failed with $?"
2309         trap cleanup_quota_test EXIT
2310
2311         set_ost_qtype "u" || error "enable ost quota failed"
2312         quota_show_check b u $TSTUSR
2313
2314         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $DIR ||
2315                 error "set quota failed"
2316
2317         $LFS setstripe $TESTFILE0 -c 1 -i 0 || error "setstripe $TESTFILE0 failed"
2318         $LFS setstripe $TESTFILE1 -c 1 -i 1 || error "setstripe $TESTFILE1 failed"
2319         chown $TSTUSR.$TSTUSR $TESTFILE0 || error "chown $TESTFILE0 failed"
2320         chown $TSTUSR.$TSTUSR $TESTFILE1 || error "chown $TESTFILE1 failed"
2321
2322         echo "Write to ost0..."
2323         $RUNAS $DD of=$TESTFILE0 count=$blk_cnt oflag=sync ||
2324                 quota_error a $TSTUSR "dd failed"
2325
2326         echo "Write to ost1..."
2327         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync &&
2328                 quota_error a $TSTUSR "dd succeed, expect EDQUOT"
2329
2330         echo "Free space from ost0..."
2331         rm -f $TESTFILE0
2332         wait_delete_completed
2333         sync_all_data || true
2334
2335         echo "Write to ost1 after space freed from ost0..."
2336         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync ||
2337                 quota_error a $TSTUSR "rebalancing failed"
2338
2339         cleanup_quota_test
2340 }
2341 run_test 12a "Block quota rebalancing"
2342
2343 test_12b() {
2344         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
2345
2346         local ilimit=$((1024 * 2)) # 2k inodes
2347         local TESTFILE0=$DIR/$tdir/$tfile
2348         local TESTFILE1=$DIR/${tdir}-1/$tfile
2349
2350         setup_quota_test || error "setup quota failed with $?"
2351         trap cleanup_quota_test EXIT
2352
2353         $LFS mkdir -i 1 $DIR/${tdir}-1 || error "create remote dir failed"
2354         chmod 0777 $DIR/${tdir}-1
2355
2356         set_mdt_qtype "u" || error "enable mdt quota failed"
2357         quota_show_check f u $TSTUSR
2358
2359         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
2360                 error "set quota failed"
2361
2362         echo "Create $ilimit files on mdt0..."
2363         $RUNAS createmany -m $TESTFILE0 $ilimit ||
2364                 quota_error u $TSTUSR "create failed, but expect success"
2365
2366         echo "Create files on mdt1..."
2367         $RUNAS createmany -m $TESTFILE1 1 &&
2368                 quota_error a $TSTUSR "create succeeded, expect EDQUOT"
2369
2370         echo "Free space from mdt0..."
2371         $RUNAS unlinkmany $TESTFILE0 $ilimit || error "unlink mdt0 files failed"
2372         wait_delete_completed
2373         sync_all_data || true
2374
2375         echo "Create files on mdt1 after space freed from mdt0..."
2376         $RUNAS createmany -m $TESTFILE1 $((ilimit / 2)) ||
2377                 quota_error a $TSTUSR "rebalancing failed"
2378
2379         $RUNAS unlinkmany $TESTFILE1 $((ilimit / 2)) ||
2380                 error "unlink mdt1 files failed"
2381         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
2382
2383         cleanup_quota_test
2384 }
2385 run_test 12b "Inode quota rebalancing"
2386
2387 test_13(){
2388         local TESTFILE=$DIR/$tdir/$tfile
2389         # the name of lwp on ost1 name is MDT0000-lwp-OST0000
2390         local procf="ldlm.namespaces.*MDT0000-lwp-OST0000.lru_size"
2391
2392         setup_quota_test || error "setup quota failed with $?"
2393         trap cleanup_quota_test EXIT
2394
2395         set_ost_qtype "u" || error "enable ost quota failed"
2396         quota_show_check b u $TSTUSR
2397
2398         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
2399                 error "set quota failed"
2400         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
2401         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2402
2403         # clear the locks in cache first
2404         do_facet ost1 $LCTL set_param -n $procf=clear
2405         local nlock=$(do_facet ost1 $LCTL get_param -n $procf)
2406         [ $nlock -eq 0 ] || error "$nlock cached locks"
2407
2408         # write to acquire the per-ID lock
2409         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
2410                 quota_error a $TSTUSR "dd failed"
2411
2412         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
2413         [ $nlock -eq 1 ] || error "lock count($nlock) isn't 1"
2414
2415         # clear quota doesn't trigger per-ID lock cancellation
2416         resetquota -u $TSTUSR
2417         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
2418         [ $nlock -eq 1 ] || error "per-ID lock is lost on quota clear"
2419
2420         # clear the per-ID lock
2421         do_facet ost1 $LCTL set_param -n $procf=clear
2422         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
2423         [ $nlock -eq 0 ] || error "per-ID lock isn't cleared"
2424
2425         # spare quota should be released
2426         local OSTUUID=$(ostuuid_from_index 0)
2427         local limit=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
2428         local space=$(getquota -u $TSTUSR $OSTUUID curspace)
2429         [ $limit -le $space ] ||
2430                 error "spare quota isn't released, limit:$limit, space:$space"
2431
2432         cleanup_quota_test
2433 }
2434 run_test 13 "Cancel per-ID lock in the LRU list"
2435
2436 test_15(){
2437         local LIMIT=$((24 * 1024 * 1024 * 1024 * 1024)) # 24 TB
2438
2439         wait_delete_completed
2440         sync_all_data || true
2441
2442         # test for user
2443         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
2444                 error "set user quota failed"
2445         local TOTAL_LIMIT=$(getquota -u $TSTUSR global bhardlimit)
2446         [ $TOTAL_LIMIT -eq $LIMIT ] ||
2447                 error "(user) limit:$TOTAL_LIMIT, expect:$LIMIT, failed!"
2448         resetquota -u $TSTUSR
2449
2450         # test for group
2451         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
2452                 error "set group quota failed"
2453         TOTAL_LIMIT=$(getquota -g $TSTUSR global bhardlimit)
2454         [ $TOTAL_LIMIT -eq $LIMIT ] ||
2455                 error "(group) limits:$TOTAL_LIMIT, expect:$LIMIT, failed!"
2456         resetquota -g $TSTUSR
2457 }
2458 run_test 15 "Set over 4T block quota"
2459
2460 test_17sub() {
2461         local err_code=$1
2462         local BLKS=1    # 1M less than limit
2463         local TESTFILE=$DIR/$tdir/$tfile
2464
2465         setup_quota_test || error "setup quota failed with $?"
2466         trap cleanup_quota_test EXIT
2467
2468         # make sure the system is clean
2469         local USED=$(getquota -u $TSTUSR global curspace)
2470         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
2471
2472         set_ost_qtype "ug" || error "enable ost quota failed"
2473         # make sure no granted quota on ost
2474         resetquota -u $TSTUSR
2475         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
2476                 error "set quota failed"
2477
2478         quota_show_check b u $TSTUSR
2479
2480         #define OBD_FAIL_QUOTA_RECOVERABLE_ERR 0xa04
2481         lustre_fail mds 0xa04 $err_code
2482
2483         # write in background
2484         $RUNAS $DD of=$TESTFILE count=$BLKS oflag=direct &
2485         local DDPID=$!
2486
2487         sleep 2
2488         # write should be blocked and never finished
2489         if ! ps -p $DDPID  > /dev/null 2>&1; then
2490                 lustre_fail mds 0 0
2491                 quota_error u $TSTUSR "write finished incorrectly!"
2492         fi
2493
2494         lustre_fail mds 0 0
2495
2496         local count=0
2497         local timeout=30
2498         while [ true ]; do
2499                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
2500                 count=$((count+1))
2501                 if [ $count -gt $timeout ]; then
2502                         quota_error u $TSTUSR "dd is not finished!"
2503                 fi
2504                 sleep 1
2505         done
2506
2507         sync; sync_all_data || true
2508
2509         USED=$(getquota -u $TSTUSR global curspace)
2510         [ $USED -ge $((BLKS * 1024)) ] || quota_error u $TSTUSR \
2511                 "Used space(${USED}K) is less than ${BLKS}M"
2512
2513         cleanup_quota_test
2514 }
2515
2516 # DQACQ return recoverable error
2517 test_17() {
2518         echo "DQACQ return -ENOLCK"
2519         #define ENOLCK  37
2520         test_17sub 37 || error "Handle -ENOLCK failed"
2521
2522         echo "DQACQ return -EAGAIN"
2523         #define EAGAIN  11
2524         test_17sub 11 || error "Handle -EAGAIN failed"
2525
2526         echo "DQACQ return -ETIMEDOUT"
2527         #define ETIMEDOUT 110
2528         test_17sub 110 || error "Handle -ETIMEDOUT failed"
2529
2530         echo "DQACQ return -ENOTCONN"
2531         #define ENOTCONN 107
2532         test_17sub 107 || error "Handle -ENOTCONN failed"
2533 }
2534
2535 run_test 17 "DQACQ return recoverable error"
2536
2537 test_18_sub () {
2538         local io_type=$1
2539         local blimit="200m" # 200M
2540         local TESTFILE="$DIR/$tdir/$tfile"
2541
2542         setup_quota_test || error "setup quota failed with $?"
2543         trap cleanup_quota_test EXIT
2544
2545         set_ost_qtype "u" || error "enable ost quota failed"
2546         log "User quota (limit: $blimit)"
2547         $LFS setquota -u $TSTUSR -b 0 -B $blimit -i 0 -I 0 $MOUNT ||
2548                 error "set quota failed"
2549         quota_show_check b u $TSTUSR
2550
2551         $LFS setstripe $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
2552         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2553
2554         local timeout=$(sysctl -n lustre.timeout)
2555
2556         if [ $io_type = "directio" ]; then
2557                 log "Write 100M (directio) ..."
2558                 $RUNAS $DD of=$TESTFILE count=100 oflag=direct &
2559         else
2560                 log "Write 100M (buffered) ..."
2561                 $RUNAS $DD of=$TESTFILE count=100 &
2562         fi
2563         local DDPID=$!
2564
2565         replay_barrier $SINGLEMDS
2566         log "Fail mds for $((2 * timeout)) seconds"
2567         fail $SINGLEMDS $((2 * timeout))
2568
2569         local count=0
2570         if at_is_enabled; then
2571                 timeout=$(at_max_get mds)
2572         else
2573                 timeout=$(lctl get_param -n timeout)
2574         fi
2575
2576         while [ true ]; do
2577                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
2578                 if [ $((++count % (2 * timeout) )) -eq 0 ]; then
2579                         log "it took $count second"
2580                 fi
2581                 sleep 1
2582         done
2583
2584         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
2585         sync
2586         cancel_lru_locks mdc
2587         cancel_lru_locks osc
2588         $SHOW_QUOTA_USER
2589
2590         local testfile_size=$(stat -c %s $TESTFILE)
2591         if [ $testfile_size -ne $((BLK_SZ * 1024 * 100)) ] ; then
2592                 quota_error u $TSTUSR "expect $((BLK_SZ * 1024 * 100))," \
2593                         "got ${testfile_size}. Verifying file failed!"
2594         fi
2595         cleanup_quota_test
2596 }
2597
2598 # test when mds does failover, the ost still could work well
2599 # this test shouldn't trigger watchdog b=14840
2600 test_18() {
2601         # Clear dmesg so watchdog is not triggered by previous
2602         # test output
2603         do_facet ost1 dmesg -c > /dev/null
2604
2605         test_18_sub normal
2606         test_18_sub directio
2607
2608         # check if watchdog is triggered
2609         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
2610         local watchdog=$(awk '/[Ss]ervice thread pid/ && /was inactive/ \
2611                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
2612         [ -z "$watchdog" ] || error "$watchdog"
2613         rm -f $TMP/lustre-log-${TESTNAME}.log
2614 }
2615 run_test 18 "MDS failover while writing, no watchdog triggered (b14840)"
2616
2617 test_19() {
2618         local blimit=5 # 5M
2619         local TESTFILE=$DIR/$tdir/$tfile
2620
2621         setup_quota_test || error "setup quota failed with $?"
2622         trap cleanup_quota_test EXIT
2623
2624         set_ost_qtype $QTYPE || error "enable ost quota failed"
2625
2626         # bind file to a single OST
2627         $LFS setstripe -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
2628         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2629
2630         echo "Set user quota (limit: ${blimit}M)"
2631         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2632                 error "set user quota failed"
2633         quota_show_check b u $TSTUSR
2634         echo "Update quota limits"
2635         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2636                 error "set group quota failed"
2637         quota_show_check b u $TSTUSR
2638
2639         # first wirte might be cached
2640         $RUNAS $DD of=$TESTFILE count=$((blimit + 1))
2641         cancel_lru_locks osc
2642         $SHOW_QUOTA_USER
2643         $RUNAS $DD of=$TESTFILE count=$((blimit + 1)) seek=$((blimit + 1)) &&
2644                 quota_error u $TSTUSR "Write success, expect failure"
2645         $SHOW_QUOTA_USER
2646
2647         cleanup_quota_test
2648 }
2649 run_test 19 "Updating admin limits doesn't zero operational limits(b14790)"
2650
2651 test_20() { # b15754
2652         local LSTR=(2g 1t 4k 3m) # limits strings
2653         # limits values
2654         local LVAL=($((2*1024*1024)) $((1*1024*1024*1024)) $((4*1024)) \
2655                     $((3*1024*1024)))
2656
2657         resetquota -u $TSTUSR
2658
2659         $LFS setquota -u $TSTUSR --block-softlimit ${LSTR[0]} \
2660                 $MOUNT || error "could not set quota limits"
2661         $LFS setquota -u $TSTUSR --block-hardlimit ${LSTR[1]} \
2662                                 --inode-softlimit ${LSTR[2]} \
2663                                 --inode-hardlimit ${LSTR[3]} \
2664                                 $MOUNT || error "could not set quota limits"
2665
2666         [ "$(getquota -u $TSTUSR global bsoftlimit)" = "${LVAL[0]}" ] ||
2667                 error "bsoftlimit was not set properly"
2668         [ "$(getquota -u $TSTUSR global bhardlimit)" = "${LVAL[1]}" ] ||
2669                 error "bhardlimit was not set properly"
2670         [ "$(getquota -u $TSTUSR global isoftlimit)" = "${LVAL[2]}" ] ||
2671                 error "isoftlimit was not set properly"
2672         [ "$(getquota -u $TSTUSR global ihardlimit)" = "${LVAL[3]}" ] ||
2673                 error "ihardlimit was not set properly"
2674
2675         resetquota -u $TSTUSR
2676 }
2677 run_test 20 "Test if setquota specifiers work properly (b15754)"
2678
2679 test_21_sub() {
2680         local testfile=$1
2681         local blk_number=$2
2682         local seconds=$3
2683
2684         local time=$(($(date +%s) + seconds))
2685         while [ $(date +%s) -lt $time ]; do
2686                 $RUNAS $DD of=$testfile count=$blk_number > /dev/null 2>&1
2687         done
2688 }
2689
2690 # run for fixing bug16053, setquota shouldn't fail when writing and
2691 # deleting are happening
2692 test_21() {
2693         local TESTFILE="$DIR/$tdir/$tfile"
2694         local BLIMIT=10 # 10G
2695         local ILIMIT=1000000
2696
2697         setup_quota_test || error "setup quota failed with $?"
2698         trap cleanup_quota_test EXIT
2699
2700         set_ost_qtype $QTYPE || error "Enable ost quota failed"
2701
2702         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for user: $TSTUSR"
2703         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}G -i 0 -I $ILIMIT $MOUNT ||
2704                 error "set user quota failed"
2705         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for group: $TSTUSR"
2706         $LFS setquota -g $TSTUSR -b 0 -B $BLIMIT -i 0 -I $ILIMIT $MOUNT ||
2707                 error "set group quota failed"
2708         if is_project_quota_supported; then
2709                 log "Set limit(block:${BLIMIT}G; file:$LIMIT) for " \
2710                         "project: $TSTPRJID"
2711                 $LFS setquota -p $TSTPRJID -b 0 -B $BLIMIT -i 0 -I $ILIMIT \
2712                          $MOUNT || error "set project quota failed"
2713         fi
2714
2715         # repeat writing on a 1M file
2716         test_21_sub ${TESTFILE}_1 1 30 &
2717         local DDPID1=$!
2718         # repeat writing on a 128M file
2719         test_21_sub ${TESTFILE}_2 128 30 &
2720         local DDPID2=$!
2721
2722         local time=$(($(date +%s) + 30))
2723         local i=1
2724         while [ $(date +%s) -lt $time ]; do
2725                 log "Set quota for $i times"
2726                 $LFS setquota -u $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
2727                         -I $((ILIMIT + i)) $MOUNT ||
2728                                 error "Set user quota failed"
2729                 $LFS setquota -g $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
2730                         -I $((ILIMIT + i)) $MOUNT ||
2731                                 error "Set group quota failed"
2732                 if is_project_quota_supported; then
2733                         $LFS setquota -p $TSTPRJID -b 0 -B \
2734                         "$((BLIMIT + i))G"  -i 0 -I $((ILIMIT + i)) $MOUNT ||
2735                                 error "Set project quota failed"
2736                 fi
2737                 i=$((i+1))
2738                 sleep 1
2739         done
2740
2741         local count=0
2742         while [ true ]; do
2743                 if ! ps -p ${DDPID1} > /dev/null 2>&1; then break; fi
2744                 count=$((count+1))
2745                 if [ $count -gt 60 ]; then
2746                         quota_error a $TSTUSR "dd should be finished!"
2747                 fi
2748                 sleep 1
2749         done
2750         echo "(dd_pid=$DDPID1, time=$count)successful"
2751
2752         count=0
2753         while [ true ]; do
2754                 if ! ps -p ${DDPID2} > /dev/null 2>&1; then break; fi
2755                 count=$((count+1))
2756                 if [ $count -gt 60 ]; then
2757                         quota_error a $TSTUSR "dd should be finished!"
2758                 fi
2759                 sleep 1
2760         done
2761         echo "(dd_pid=$DDPID2, time=$count)successful"
2762
2763         cleanup_quota_test
2764 }
2765 run_test 21 "Setquota while writing & deleting (b16053)"
2766
2767 # enable/disable quota enforcement permanently
2768 test_22() {
2769         echo "Set both mdt & ost quota type as ug"
2770         local qtype="ug"
2771         is_project_quota_supported && qtype=$QTYPE
2772         set_mdt_qtype $qtype || error "enable mdt quota failed"
2773         set_ost_qtype $qtype || error "enable ost quota failed"
2774
2775         echo "Restart..."
2776         stopall || error "failed to stopall (1)"
2777         mount
2778         setupall
2779
2780         echo "Verify if quota is enabled"
2781         local qtype1=$(mdt_quota_type)
2782         [ $qtype1 != $qtype ] && error "mdt quota setting is lost"
2783         qtype=$(ost_quota_type)
2784         [ $qtype1 != $qtype ] && error "ost quota setting is lost"
2785
2786         echo "Set both mdt & ost quota type as none"
2787         set_mdt_qtype "none" || error "disable mdt quota failed"
2788         set_ost_qtype "none" || error "disable ost quota failed"
2789
2790         echo "Restart..."
2791         stopall || error "failed to stopall (2)"
2792         mount
2793         setupall
2794         quota_init
2795
2796         echo "Verify if quota is disabled"
2797         qtype=$(mdt_quota_type)
2798         [ $qtype != "none" ] && error "mdt quota setting is lost"
2799         qtype=$(ost_quota_type)
2800         [ $qtype != "none" ] && error "ost quota setting is lost"
2801
2802         return 0
2803 }
2804 run_test 22 "enable/disable quota by 'lctl conf_param/set_param -P'"
2805
2806 test_23_sub() {
2807         local TESTFILE="$DIR/$tdir/$tfile"
2808         local LIMIT=$1
2809
2810         setup_quota_test || error "setup quota failed with $?"
2811         trap cleanup_quota_test EXIT
2812
2813         set_ost_qtype $QTYPE || error "Enable ost quota failed"
2814
2815         # test for user
2816         log "User quota (limit: $LIMIT MB)"
2817         $LFS setquota -u $TSTUSR -b 0 -B "$LIMIT"M -i 0 -I 0 $DIR ||
2818                 error "set quota failed"
2819         quota_show_check b u $TSTUSR
2820
2821         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
2822         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2823
2824         log "Step1: trigger EDQUOT with O_DIRECT"
2825         log "Write half of file"
2826         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) oflag=direct ||
2827                 quota_error u $TSTUSR "(1) Write failure, expect success." \
2828                         "limit=$LIMIT"
2829         log "Write out of block quota ..."
2830         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 + 1)) seek=$((LIMIT/2)) \
2831                 oflag=direct conv=notrunc &&
2832                 quota_error u $TSTUSR "(2) Write success, expect EDQUOT." \
2833                         "limit=$LIMIT"
2834         log "Step1: done"
2835
2836         log "Step2: rewrite should succeed"
2837         $RUNAS $DD of=$TESTFILE count=1 oflag=direct conv=notrunc||
2838                 quota_error u $TSTUSR "(3) Write failure, expect success." \
2839                         "limit=$LIMIT"
2840         log "Step2: done"
2841
2842         cleanup_quota_test
2843
2844         local OST0_UUID=$(ostuuid_from_index 0)
2845         local OST0_QUOTA_USED=$(getquota -u $TSTUSR $OST0_UUID curspace)
2846         [ $OST0_QUOTA_USED -ne 0 ] &&
2847                 ($SHOW_QUOTA_USER; \
2848                 quota_error u $TSTUSR "quota isn't released")
2849         $SHOW_QUOTA_USER
2850 }
2851
2852 test_23() {
2853         [ "$ost1_FSTYPE" == zfs ] &&
2854                 skip "Overwrite in place is not guaranteed to be " \
2855                 "space neutral on ZFS"
2856
2857         local OST0_MIN=$((6 * 1024)) # 6MB, extra space for meta blocks.
2858         check_whether_skip && return 0
2859         log "run for 4MB test file"
2860         test_23_sub 4
2861
2862         OST0_MIN=$((60 * 1024)) # 60MB, extra space for meta blocks.
2863         check_whether_skip && return 0
2864         log "run for 40MB test file"
2865         test_23_sub 40
2866 }
2867 run_test 23 "Quota should be honored with directIO (b16125)"
2868
2869 test_24() {
2870         local blimit=5 # 5M
2871         local TESTFILE="$DIR/$tdir/$tfile"
2872
2873         setup_quota_test || error "setup quota failed with $?"
2874         trap cleanup_quota_test EXIT
2875
2876         set_ost_qtype $QTYPE || error "enable ost quota failed"
2877
2878         # bind file to a single OST
2879         $LFS setstripe -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
2880         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2881
2882         echo "Set user quota (limit: ${blimit}M)"
2883         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2884                 error "set quota failed"
2885
2886         # overrun quota by root user
2887         runas -u 0 -g 0 $DD of=$TESTFILE count=$((blimit + 1)) ||
2888                 error "write failure, expect success"
2889         cancel_lru_locks osc
2890         sync_all_data || true
2891
2892         $SHOW_QUOTA_USER | grep '*' || error "no matching *"
2893
2894         cleanup_quota_test
2895 }
2896 run_test 24 "lfs draws an asterix when limit is reached (b16646)"
2897
2898 test_27a() { # b19612
2899         $LFS quota $TSTUSR $DIR &&
2900                 error "lfs succeeded with no type, but should have failed"
2901         $LFS setquota $TSTUSR $DIR &&
2902                 error "lfs succeeded with no type, but should have failed"
2903         return 0
2904 }
2905 run_test 27a "lfs quota/setquota should handle wrong arguments (b19612)"
2906
2907 test_27b() { # b20200
2908         $LFS setquota -u $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2909                 error "lfs setquota failed with uid argument"
2910         $LFS setquota -g $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2911                 error "lfs stequota failed with gid argument"
2912         if is_project_quota_supported; then
2913                 $LFS setquota -p $TSTPRJID -b 1000 -B 1000 -i 1000 -I \
2914                         1000 $DIR || error \
2915                                 "lfs stequota failed with projid argument"
2916         fi
2917         $SHOW_QUOTA_USERID || error "lfs quota failed with uid argument"
2918         $SHOW_QUOTA_GROUPID || error "lfs quota failed with gid argument"
2919         if is_project_quota_supported; then
2920                 $SHOW_QUOTA_PROJID ||
2921                         error "lfs quota failed with projid argument"
2922         fi
2923         resetquota -u $TSTID
2924         resetquota -g $TSTID
2925         resetquota -p $TSTPRJID
2926         return 0
2927 }
2928 run_test 27b "lfs quota/setquota should handle user/group/project ID (b20200)"
2929
2930 test_27c() {
2931         local limit
2932
2933         $LFS setquota -u $TSTID -b 30M -B 3T $DIR ||
2934                 error "lfs setquota failed"
2935
2936         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
2937         [ $limit != "30M" ] && error "softlimit $limit isn't human-readable"
2938         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
2939         [ $limit != "3T" ] && error "hardlimit $limit isn't human-readable"
2940
2941         $LFS setquota -u $TSTID -b 1500M -B 18500G $DIR ||
2942                 error "lfs setquota for $TSTID failed"
2943
2944         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
2945         [ $limit != "1.465G" ] && error "wrong softlimit $limit"
2946         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
2947         [ $limit != "18.07T" ] && error "wrong hardlimit $limit"
2948
2949         $LFS quota -u $TSTID -v -h $DIR | grep -q "Total allocated" ||
2950                 error "total allocated inode/block limit not printed"
2951
2952         resetquota -u $TSTUSR
2953 }
2954 run_test 27c "lfs quota should support human-readable output"
2955
2956 test_27d() {
2957         local softlimit=1.5
2958         local hardlimit=2.3
2959         local limit
2960
2961         $LFS setquota -u $TSTID -b ${softlimit}p -B ${hardlimit}P $DIR ||
2962                 error "set fraction block limit failed"
2963         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $3}')
2964         [ $limit == ${softlimit}P ] || error "get fraction softlimit failed"
2965         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $4}')
2966         [ $limit == ${hardlimit}P ] || error "get fraction hardlimit failed"
2967
2968         resetquota -u $TSTUSR
2969 }
2970 run_test 27d "lfs setquota should support fraction block limit"
2971
2972 test_30() {
2973         local LIMIT=4 # 4MB
2974         local TESTFILE="$DIR/$tdir/$tfile"
2975         local GRACE=10
2976
2977         setup_quota_test || error "setup quota failed with $?"
2978         trap cleanup_quota_test EXIT
2979
2980         set_ost_qtype "u" || error "enable ost quota failed"
2981
2982         $LFS setstripe $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
2983         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2984
2985         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
2986                 $MAX_IQ_TIME $DIR || error "set grace time failed"
2987         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
2988                 error "set quota failed"
2989         $RUNAS $DD of=$TESTFILE count=$((LIMIT * 2)) || true
2990         cancel_lru_locks osc
2991         sleep $GRACE
2992         $LFS setquota -u $TSTUSR -B 0 $DIR || error "clear quota failed"
2993         # over-quota flag has not yet settled since we do not trigger async
2994         # events based on grace time period expiration
2995         $SHOW_QUOTA_USER
2996         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 || true
2997         cancel_lru_locks osc
2998         # now over-quota flag should be settled and further writes should fail
2999         $SHOW_QUOTA_USER
3000         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 &&
3001                 error "grace times were reset"
3002         # cleanup
3003         cleanup_quota_test
3004         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
3005                 $MAX_IQ_TIME $DIR || error "restore grace time failed"
3006 }
3007 run_test 30 "Hard limit updates should not reset grace times"
3008
3009 # basic usage tracking for user & group
3010 test_33() {
3011         local INODES=10 # 10 files
3012         local BLK_CNT=2 # of 2M each
3013         local TOTAL_BLKS=$((INODES * BLK_CNT * 1024))
3014
3015         setup_quota_test || error "setup quota failed with $?"
3016         trap cleanup_quota_test EXIT
3017
3018         # make sure the system is clean
3019         local USED=$(getquota -u $TSTID global curspace)
3020         [ $USED -ne 0 ] &&
3021                 error "Used space ($USED) for user $TSTID isn't 0."
3022         USED=$(getquota -g $TSTID global curspace)
3023         [ $USED -ne 0 ] &&
3024                 error "Used space ($USED) for group $TSTID isn't 0."
3025         if is_project_quota_supported; then
3026                 USED=$(getquota -p $TSTPRJID global curspace)
3027                 [ $USED -ne 0 ] && error \
3028                         "Used space ($USED) for project $TSTPRJID isn't 0."
3029         fi
3030
3031         echo "Write files..."
3032         for i in $(seq 0 $INODES); do
3033                 $RUNAS $DD of=$DIR/$tdir/$tfile-$i count=$BLK_CNT 2>/dev/null ||
3034                         error "write failed"
3035                         is_project_quota_supported &&
3036                                 change_project -p $TSTPRJID $DIR/$tdir/$tfile-$i
3037                 echo "Iteration $i/$INODES completed"
3038         done
3039         cancel_lru_locks osc
3040
3041         echo "Wait for setattr on objects finished..."
3042         wait_delete_completed
3043
3044         sync; sync_all_data || true
3045
3046         echo "Verify disk usage after write"
3047         USED=$(getquota -u $TSTID global curspace)
3048         [ $USED -lt $TOTAL_BLKS ] &&
3049                 error "Used space for user $TSTID:$USED, expected:$TOTAL_BLKS"
3050         USED=$(getquota -g $TSTID global curspace)
3051         [ $USED -lt $TOTAL_BLKS ] &&
3052                 error "Used space for group $TSTID:$USED, expected:$TOTAL_BLKS"
3053         if is_project_quota_supported; then
3054                 USED=$(getquota -p $TSTPRJID global curspace)
3055                 [ $USED -lt $TOTAL_BLKS ] && error \
3056                         "Used space for project $TSTPRJID:$USED, expected:$TOTAL_BLKS"
3057         fi
3058
3059         echo "Verify inode usage after write"
3060         USED=$(getquota -u $TSTID global curinodes)
3061         [ $USED -lt $INODES ] &&
3062                 error "Used inode for user $TSTID is $USED, expected $INODES"
3063         USED=$(getquota -g $TSTID global curinodes)
3064         [ $USED -lt $INODES ] &&
3065                 error "Used inode for group $TSTID is $USED, expected $INODES"
3066         if is_project_quota_supported; then
3067                 USED=$(getquota -p $TSTPRJID global curinodes)
3068                 [ $USED -lt $INODES ] && error \
3069                         "Used inode for project $TSTPRJID is $USED, expected $INODES"
3070         fi
3071
3072         cleanup_quota_test
3073
3074         echo "Verify disk usage after delete"
3075         USED=$(getquota -u $TSTID global curspace)
3076         [ $USED -eq 0 ] || error "Used space for user $TSTID isn't 0. $USED"
3077         USED=$(getquota -u $TSTID global curinodes)
3078         [ $USED -eq 0 ] || error "Used inodes for user $TSTID isn't 0. $USED"
3079         USED=$(getquota -g $TSTID global curspace)
3080         [ $USED -eq 0 ] || error "Used space for group $TSTID isn't 0. $USED"
3081         USED=$(getquota -g $TSTID global curinodes)
3082         [ $USED -eq 0 ] || error "Used inodes for group $TSTID isn't 0. $USED"
3083         if is_project_quota_supported; then
3084                 USED=$(getquota -p $TSTPRJID global curspace)
3085                 [ $USED -eq 0 ] ||
3086                         error "Used space for project $TSTPRJID isn't 0. $USED"
3087                 USED=$(getquota -p $TSTPRJID global curinodes)
3088                 [ $USED -eq 0 ] ||
3089                         error "Used inodes for project $TSTPRJID isn't 0. $USED"
3090         fi
3091 }
3092 run_test 33 "Basic usage tracking for user & group & project"
3093
3094 # usage transfer test for user & group & project
3095 test_34() {
3096         local BLK_CNT=2 # 2MB
3097         local project_supported="no"
3098
3099         is_project_quota_supported && project_supported="yes"
3100         setup_quota_test || error "setup quota failed with $?"
3101         trap cleanup_quota_test EXIT
3102
3103         # make sure the system is clean
3104         local USED=$(getquota -u $TSTID global curspace)
3105         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID isn't 0."
3106         USED=$(getquota -g $TSTID global curspace)
3107         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTID isn't 0."
3108
3109         local USED=$(getquota -u $TSTID2 global curspace)
3110         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID2 isn't 0."
3111         if [ $project_supported == "yes" ]; then
3112                 USED=$(getquota -p $TSTPRJID global curspace)
3113                 [ $USED -ne 0 ] && error \
3114                         "Used space ($USED) for Project $TSTPRJID isn't 0."
3115         fi
3116
3117         echo "Write file..."
3118         $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
3119                 error "write failed"
3120         cancel_lru_locks osc
3121         sync; sync_all_data || true
3122
3123         echo "chown the file to user $TSTID"
3124         chown $TSTID $DIR/$tdir/$tfile || error "chown failed"
3125
3126         echo "Wait for setattr on objects finished..."
3127         wait_delete_completed
3128
3129         BLK_CNT=$((BLK_CNT * 1024))
3130
3131         echo "Verify disk usage for user $TSTID"
3132         USED=$(getquota -u $TSTID global curspace)
3133         [ $USED -lt $BLK_CNT ] &&
3134                 error "Used space for user $TSTID is ${USED}, expected $BLK_CNT"
3135         USED=$(getquota -u $TSTID global curinodes)
3136         [ $USED -ne 1 ] &&
3137                 error "Used inodes for user $TSTID is $USED, expected 1"
3138
3139         echo "chgrp the file to group $TSTID"
3140         chgrp $TSTID $DIR/$tdir/$tfile || error "chgrp failed"
3141
3142         echo "Wait for setattr on objects finished..."
3143         wait_delete_completed
3144
3145         echo "Verify disk usage for group $TSTID"
3146         USED=$(getquota -g $TSTID global curspace)
3147         [ $USED -ge $BLK_CNT ] ||
3148                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
3149         USED=$(getquota -g $TSTID global curinodes)
3150         [ $USED -eq 1 ] ||
3151                 error "Used inodes for group $TSTID is $USED, expected 1"
3152
3153         # chown won't change the ost object group. LU-4345 */
3154         echo "chown the file to user $TSTID2"
3155         chown $TSTID2 $DIR/$tdir/$tfile || error "chown to $TSTID2 failed"
3156
3157         echo "Wait for setattr on objects finished..."
3158         wait_delete_completed
3159
3160         echo "change_project project id to $TSTPRJID"
3161         [ $project_supported == "yes" ] &&
3162                 change_project -p $TSTPRJID $DIR/$tdir/$tfile
3163         echo "Wait for setattr on objects finished..."
3164         wait_delete_completed
3165
3166         echo "Verify disk usage for user $TSTID2/$TSTID and group $TSTID"
3167         USED=$(getquota -u $TSTID2 global curspace)
3168         [ $USED -lt $BLK_CNT ] &&
3169                 error "Used space for user $TSTID2 is $USED, expected $BLK_CNT"
3170         USED=$(getquota -u $TSTID global curspace)
3171         [ $USED -ne 0 ] &&
3172                 error "Used space for user $TSTID is $USED, expected 0"
3173         USED=$(getquota -g $TSTID global curspace)
3174         [ $USED -lt $BLK_CNT ] &&
3175                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
3176         if [ $project_supported == "yes" ]; then
3177                 USED=$(getquota -p $TSTPRJID global curspace)
3178                 [ $USED -lt $BLK_CNT ] && error \
3179                         "Used space for group $TSTPRJID is $USED, expected $BLK_CNT"
3180         fi
3181
3182         cleanup_quota_test
3183 }
3184 run_test 34 "Usage transfer for user & group & project"
3185
3186 # usage is still accessible across restart
3187 test_35() {
3188         local BLK_CNT=2 # 2 MB
3189
3190         setup_quota_test || error "setup quota failed with $?"
3191         trap cleanup_quota_test EXIT
3192
3193         echo "Write file..."
3194         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
3195                 error "write failed"
3196         is_project_quota_supported &&
3197                 change_project -p $TSTPRJID $DIR/$tdir/$tfile
3198         cancel_lru_locks osc
3199
3200         echo "Wait for setattr on objects finished..."
3201         wait_delete_completed
3202
3203         sync; sync_all_data || true
3204
3205         echo "Save disk usage before restart"
3206         local ORIG_USR_SPACE=$(getquota -u $TSTID global curspace)
3207         [ $ORIG_USR_SPACE -eq 0 ] &&
3208                 error "Used space for user $TSTID is 0, expected ${BLK_CNT}M"
3209         local ORIG_USR_INODES=$(getquota -u $TSTID global curinodes)
3210         [ $ORIG_USR_INODES -eq 0 ] &&
3211                 error "Used inodes for user $TSTID is 0, expected 1"
3212         echo "User $TSTID: ${ORIG_USR_SPACE}KB $ORIG_USR_INODES inodes"
3213         local ORIG_GRP_SPACE=$(getquota -g $TSTID global curspace)
3214         [ $ORIG_GRP_SPACE -eq 0 ] &&
3215                 error "Used space for group $TSTID is 0, expected ${BLK_CNT}M"
3216         local ORIG_GRP_INODES=$(getquota -g $TSTID global curinodes)
3217         [ $ORIG_GRP_INODES -eq 0 ] &&
3218                 error "Used inodes for group $TSTID is 0, expected 1"
3219         echo "Group $TSTID: ${ORIG_GRP_SPACE}KB $ORIG_GRP_INODES inodes"
3220
3221         if is_project_quota_supported; then
3222                 local ORIG_PRJ_SPACE=$(getquota -p $TSTPRJID global curspace)
3223                 [ $ORIG_PRJ_SPACE -eq 0 ] && error \
3224                         "Used space for project $TSTPRJID is 0, expected ${BLK_CNT}M"
3225                 local ORIG_PRJ_INODES=$(getquota -p $TSTPRJID global curinodes)
3226                 [ $ORIG_PRJ_INODES -eq 0 ] && error \
3227                         "Used inodes for project $TSTPRJID is 0, expected 1"
3228                 echo "Project $TSTPRJID: ${ORIG_PRJ_SPACE}KB $ORIG_PRJ_INODES inodes"
3229         fi
3230
3231         log "Restart..."
3232         stopall
3233         setupall
3234         quota_init
3235
3236         echo "Verify disk usage after restart"
3237         local USED=$(getquota -u $TSTID global curspace)
3238         [ $USED -eq $ORIG_USR_SPACE ] ||
3239                 error "Used space for user $TSTID changed from " \
3240                         "$ORIG_USR_SPACE to $USED"
3241         USED=$(getquota -u $TSTID global curinodes)
3242         [ $USED -eq $ORIG_USR_INODES ] ||
3243                 error "Used inodes for user $TSTID changed from " \
3244                         "$ORIG_USR_INODES to $USED"
3245         USED=$(getquota -g $TSTID global curspace)
3246         [ $USED -eq $ORIG_GRP_SPACE ] ||
3247                 error "Used space for group $TSTID changed from " \
3248                         "$ORIG_GRP_SPACE to $USED"
3249         USED=$(getquota -g $TSTID global curinodes)
3250         [ $USED -eq $ORIG_GRP_INODES ] ||
3251                 error "Used inodes for group $TSTID changed from " \
3252                         "$ORIG_GRP_INODES to $USED"
3253         if [ $project_supported == "yes" ]; then
3254                 USED=$(getquota -p $TSTPRJID global curinodes)
3255                 [ $USED -eq $ORIG_PRJ_INODES ] ||
3256                         error "Used inodes for project $TSTPRJID " \
3257                                 "changed from $ORIG_PRJ_INODES to $USED"
3258                 USED=$(getquota -p $TSTPRJID global curspace)
3259                 [ $USED -eq $ORIG_PRJ_SPACE ] ||
3260                         error "Used space for project $TSTPRJID "\
3261                                 "changed from $ORIG_PRJ_SPACE to $USED"
3262         fi
3263
3264         # check if the vfs_dq_init() is called before writing
3265         echo "Append to the same file..."
3266         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT seek=1 2>/dev/null ||
3267                 error "write failed"
3268         cancel_lru_locks osc
3269         sync; sync_all_data || true
3270
3271         echo "Verify space usage is increased"
3272         USED=$(getquota -u $TSTID global curspace)
3273         [ $USED -gt $ORIG_USR_SPACE ] ||
3274                 error "Used space for user $TSTID isn't increased" \
3275                         "orig:$ORIG_USR_SPACE, now:$USED"
3276         USED=$(getquota -g $TSTID global curspace)
3277         [ $USED -gt $ORIG_GRP_SPACE ] ||
3278                 error "Used space for group $TSTID isn't increased" \
3279                         "orig:$ORIG_GRP_SPACE, now:$USED"
3280         if [ $project_supported == "yes" ]; then
3281                 USED=$(getquota -p $TSTPRJID global curspace)
3282                 [ $USED -gt $ORIG_PRJ_SPACE ] ||
3283                         error "Used space for project $TSTPRJID isn't " \
3284                                 "increased orig:$ORIG_PRJ_SPACE, now:$USED"
3285         fi
3286
3287         cleanup_quota_test
3288 }
3289 run_test 35 "Usage is still accessible across reboot"
3290
3291 # chown/chgrp to the file created with MDS_OPEN_DELAY_CREATE
3292 # LU-5006
3293 test_37() {
3294         [ "$MDS1_VERSION" -lt $(version_code 2.6.93) ] &&
3295                 skip "Old server doesn't have LU-5006 fix."
3296
3297         setup_quota_test || error "setup quota failed with $?"
3298         trap cleanup_quota_test EXIT
3299
3300         # make sure the system is clean
3301         local USED=$(getquota -u $TSTID global curspace)
3302         [ $USED -ne 0 ] &&
3303                 error "Used space ($USED) for user $TSTID isn't 0."
3304
3305         # create file with MDS_OPEN_DELAY_CREATE flag
3306         $LFS setstripe -c 1 -i 0 $DIR/$tdir/$tfile ||
3307                 error "Create file failed"
3308         # write to file
3309         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 conv=notrunc \
3310                 oflag=sync || error "Write file failed"
3311         # chown to the file
3312         chown $TSTID $DIR/$tdir/$tfile || error "Chown to file failed"
3313
3314         # wait for setattr on objects finished..."
3315         wait_delete_completed
3316
3317         USED=$(getquota -u $TSTID global curspace)
3318         [ $USED -ne 0 ] || quota_error u $TSTUSR "Used space is 0"
3319
3320         cleanup_quota_test
3321 }
3322 run_test 37 "Quota accounted properly for file created by 'lfs setstripe'"
3323
3324 # LU-8801
3325 test_38() {
3326         [ "$MDS1_VERSION" -lt $(version_code 2.8.60) ] &&
3327                 skip "Old server doesn't have LU-8801 fix."
3328
3329         [ "$UID" != 0 ] && skip_env "must run as root" && return
3330
3331         setup_quota_test || error "setup quota failed with $?"
3332         trap cleanup_quota_test EXIT
3333
3334         # make sure the system is clean
3335         local USED=$(getquota -u $TSTID global curspace)
3336         [ $USED -ne 0 ] &&
3337                 error "Used space ($USED) for user $TSTID isn't 0."
3338         USED=$(getquota -u $TSTID2 global curspace)
3339         [ $USED -ne 0 ] &&
3340                 error "Used space ($USED) for user $TSTID2 isn't 0."
3341
3342         local TESTFILE="$DIR/$tdir/$tfile"
3343         local file_cnt=10000
3344
3345         # Generate id entries in accounting file
3346         echo "Create $file_cnt files..."
3347         for i in `seq $file_cnt`; do
3348                 touch $TESTFILE-$i
3349                 chown $((file_cnt - i)):$((file_cnt - i)) $TESTFILE-$i ||
3350                         error "failed to chown $TESTFILE-$i"
3351         done
3352         cancel_lru_locks osc
3353         sync; sync_all_data || true
3354
3355         local procf="osd-$mds1_FSTYPE.$FSNAME-MDT0000"
3356         procf=${procf}.quota_slave.acct_user
3357         local accnt_cnt
3358
3359         acct_cnt=$(do_facet mds1 $LCTL get_param $procf | grep "id:" | \
3360                    awk '{if ($3 < 10000) {print $3}}' | wc -l)
3361         echo "Found $acct_cnt id entries"
3362
3363         [ $file_cnt -eq $acct_cnt ] || {
3364                 do_facet mds1 $LCTL get_param $procf
3365                 error "skipped id entries"
3366         }
3367
3368         cleanup_quota_test
3369 }
3370 run_test 38 "Quota accounting iterator doesn't skip id entries"
3371
3372 test_39() {
3373         local TESTFILE="$DIR/$tdir/project"
3374         ! is_project_quota_supported &&
3375                 skip "Project quota is not supported"
3376
3377         setup_quota_test || error "setup quota failed with $?"
3378
3379         touch $TESTFILE
3380         projectid=$(lfs project $TESTFILE | awk '{print $1}')
3381         [ $projectid -ne 0 ] &&
3382                 error "Project id should be 0 not $projectid"
3383         change_project -p 1024 $TESTFILE
3384         projectid=$(lfs project $TESTFILE | awk '{print $1}')
3385         [ $projectid -ne 1024 ] &&
3386                 error "Project id should be 1024 not $projectid"
3387
3388         stopall || error "failed to stopall (1)"
3389         mount
3390         setupall
3391         projectid=$(lfs project $TESTFILE | awk '{print $1}')
3392         [ $projectid -ne 1024 ] &&
3393                 error "Project id should be 1024 not $projectid"
3394
3395         cleanup_quota_test
3396 }
3397 run_test 39 "Project ID interface works correctly"
3398
3399 test_40a() {
3400         ! is_project_quota_supported &&
3401                 skip "Project quota is not supported"
3402         local dir1="$DIR/$tdir/dir1"
3403         local dir2="$DIR/$tdir/dir2"
3404
3405         setup_quota_test || error "setup quota failed with $?"
3406
3407         mkdir -p $dir1 $dir2
3408         change_project -sp 1 $dir1 && touch $dir1/1
3409         change_project -sp 2 $dir2
3410
3411         ln $dir1/1 $dir2/1_link &&
3412                 error "Hard link across different project quota should fail"
3413         rm -rf $dir1 $dir2
3414
3415         cleanup_quota_test
3416 }
3417 run_test 40a "Hard link across different project ID"
3418
3419 test_40b() {
3420         ! is_project_quota_supported &&
3421                 skip "Project quota is not supported"
3422         local dir1="$DIR/$tdir/dir1"
3423         local dir2="$DIR/$tdir/dir2"
3424
3425         setup_quota_test || error "setup quota failed with $?"
3426         mkdir -p $dir1 $dir2
3427         change_project -sp 1 $dir1 && touch $dir1/1
3428         change_project -sp 2 $dir2
3429
3430         mv $dir1/1 $dir2/2 || error "mv failed $?"
3431         local projid=$(lfs project $dir2/2 | awk '{print $1}')
3432         if [ "$projid" != "2" ]; then
3433                 error "project id expected 2 not $projid"
3434         fi
3435         rm -rf $dir1 $dir2
3436         cleanup_quota_test
3437 }
3438 run_test 40b "Mv across different project ID"
3439
3440 test_40c() {
3441         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
3442                 ! is_project_quota_supported &&
3443                         skip "Project quota is not supported"
3444
3445         setup_quota_test || error "setup quota failed with $?"
3446         local dir="$DIR/$tdir/dir"
3447
3448         mkdir -p $dir && change_project -sp 1 $dir
3449         $LFS mkdir -i 1 $dir/remote_dir || error "create remote dir failed"
3450         local projid=$(lfs project -d $dir/remote_dir | awk '{print $1}')
3451         [ "$projid" != "1" ] && error "projid id expected 1 not $projid"
3452         touch $dir/remote_dir/file
3453         #verify inherit works file for remote dir.
3454         local projid=$(lfs project -d $dir/remote_dir/file | awk '{print $1}')
3455         [ "$projid" != "1" ] &&
3456                 error "file under remote dir expected 1 not $projid"
3457
3458         #Agent inode should be ignored for project quota
3459         USED=$(getquota -p 1 global curinodes)
3460         [ "$USED" != "3" ] &&
3461                 error "file count expected 3 got $USED"
3462
3463         rm -rf $dir
3464         cleanup_quota_test
3465         return 0
3466 }
3467 run_test 40c "Remote child Dir inherit project quota properly"
3468
3469 test_50() {
3470         ! is_project_quota_supported &&
3471                 skip "Project quota is not supported"
3472
3473         setup_quota_test || error "setup quota failed with $?"
3474         local dir1="$DIR/$tdir/dir1"
3475         local dir2="$DIR/$tdir/dir2"
3476
3477         mkdir -p $dir1 && change_project -sp 1 $dir1
3478         mkdir -p $dir2 && change_project -sp 2 $dir2
3479         for num in $(seq 1 10); do
3480                 touch $dir1/file_$num $dir2/file_$num
3481                 ln -s $dir1/file_$num $dir1/file_$num"_link"
3482                 ln -s $dir2/file_$num $dir2/file_$num"_link"
3483         done
3484
3485         count=$($LFS find --projid 1 $DIR | wc -l)
3486         [ "$count" != 21 ] && error "expected 21 but got $count"
3487
3488         # 1(projid 0 dir) + 1(projid 2 dir) + 20(projid 2 files)
3489         count=$($LFS find ! --projid 1 $DIR/$tdir | wc -l)
3490         [ "$count" != 22 ] && error "expected 22 but got $count"
3491
3492         rm -rf $dir1 $dir2
3493         cleanup_quota_test
3494 }
3495 run_test 50 "Test if lfs find --projid works"
3496
3497 test_51() {
3498         ! is_project_quota_supported &&
3499                 skip "Project quota is not supported"
3500         setup_quota_test || error "setup quota failed with $?"
3501         local dir="$DIR/$tdir/dir"
3502
3503         mkdir $dir && change_project -sp 1 $dir
3504         local used=$(getquota -p 1 global curinodes)
3505         [ $used != "1" ] && error "expected 1 got $used"
3506
3507         touch $dir/1
3508         touch $dir/2
3509         cp $dir/2 $dir/3
3510         used=$(getquota -p 1 global curinodes)
3511         [ $used != "4" ] && error "expected 4 got $used"
3512
3513         $DD if=/dev/zero of=$DIR/$tdir/6 bs=1M count=1
3514         #try cp to dir
3515         cp $DIR/$tdir/6 $dir/6
3516         used=$(getquota -p 1 global curinodes)
3517         [ $used != "5" ] && error "expected 5 got $used"
3518
3519         #try mv to dir
3520         mv $DIR/$tdir/6 $dir/7
3521         used=$(getquota -p 1 global curinodes)
3522         [ $used != "6" ] && error "expected 6 got $used"
3523
3524         rm -rf $dir
3525         cleanup_quota_test
3526 }
3527 run_test 51 "Test project accounting with mv/cp"
3528
3529 test_52() {
3530         ! is_project_quota_supported &&
3531                 skip "Project quota is not supported"
3532         setup_quota_test || error "setup quota failed with $?"
3533         local dir="$DIR/$tdir/dir"
3534         mkdir $dir && change_project -sp 1 $dir
3535
3536         touch $DIR/$tdir/file
3537         #Try renaming a file into the project.  This should fail.
3538         for num in $(seq 1 2000); do
3539                 mrename $DIR/$tdir/file $dir/file >&/dev/null &&
3540                         error "rename should fail"
3541         done
3542         rm -rf $dir
3543         cleanup_quota_test
3544 }
3545 run_test 52 "Rename across different project ID"
3546
3547 test_53() {
3548         ! is_project_quota_supported &&
3549                 skip "Project quota is not supported"
3550         setup_quota_test || error "setup quota failed with $?"
3551         local dir="$DIR/$tdir/dir"
3552         mkdir $dir && change_project -s $dir
3553         lfs project -d $dir | grep P || error "inherit attribute should be set"
3554
3555         change_project -C $dir
3556         lfs project -d $dir | grep P &&
3557                 error "inherit attribute should be cleared"
3558
3559         rm -rf $dir
3560         cleanup_quota_test
3561 }
3562 run_test 53 "Project inherit attribute could be cleared"
3563
3564 test_54() {
3565         ! is_project_quota_supported &&
3566                 skip "Project quota is not supported"
3567         setup_quota_test || error "setup quota failed with $?"
3568         trap cleanup_quota_test EXIT
3569         local testfile="$DIR/$tdir/$tfile-0"
3570
3571         #set project ID/inherit attribute
3572         change_project -sp $TSTPRJID $DIR/$tdir
3573         $RUNAS createmany -m ${testfile} 100 ||
3574                 error "create many files failed"
3575
3576         local proj_count=$(lfs project -r $DIR/$tdir | wc -l)
3577         # one more count for directory itself */
3578         ((proj_count++))
3579
3580         #check project
3581         local proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
3582         [ $proj_count1 -eq 0 ] || error "c1: expected 0 got $proj_count1"
3583
3584         proj_count1=$(lfs project -rcp $((TSTPRJID+1)) $DIR/$tdir | wc -l)
3585         [ $proj_count1 -eq $proj_count ] ||
3586                         error "c2: expected $proj_count got $proj_count1"
3587
3588         #clear project but with kept projid
3589         change_project -rCk $DIR/$tdir
3590         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
3591         [ $proj_count1 -eq 1 ] ||
3592                         error "c3: expected 1 got $proj_count1"
3593
3594         #verify projid untouched.
3595         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
3596         ((proj_count1++))
3597         [ $proj_count1 -eq $proj_count ] ||
3598                         error "c4: expected $proj_count got $proj_count1"
3599
3600         # test -0 option
3601         lfs project $DIR/$tdir -cr -0 | xargs -0 lfs project -s
3602         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
3603         [ $proj_count1 -eq 0 ] || error "c5: expected 0 got $proj_count1"
3604
3605         #this time clear all
3606         change_project -rC $DIR/$tdir
3607         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
3608         [ $proj_count1 -eq 0 ] ||
3609                         error "c6: expected 0 got $proj_count1"
3610         #cleanup
3611         unlinkmany ${testfile} 100 ||
3612                 error "unlink many files failed"
3613
3614         cleanup_quota_test
3615 }
3616 run_test 54 "basic lfs project interface test"
3617
3618 test_55() {
3619         [ "$MDS1_VERSION" -lt $(version_code 2.10.58) ] &&
3620                 skip "Not supported before 2.10.58."
3621         setup_quota_test || error "setup quota failed with $?"
3622
3623         set_ost_qtype $QTYPE || error "enable ost quota failed"
3624         quota_init
3625
3626         #add second group to TSTUSR
3627         usermod -G $TSTUSR,$TSTUSR2 $TSTUSR
3628
3629         #prepare test file
3630         $RUNAS dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1024 count=100000 ||
3631         error "failed to dd"
3632
3633         cancel_lru_locks osc
3634         sync; sync_all_data || true
3635
3636         $LFS setquota -g $TSTUSR2 -b 0 -B 50M $DIR ||
3637         error "failed to setquota on group $TSTUSR2"
3638
3639         $LFS quota -v -g $TSTUSR2 $DIR
3640
3641         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile &&
3642         error "chgrp should failed with -EDQUOT"
3643
3644         USED=$(getquota -g $TSTUSR2 global curspace)
3645         echo "$USED"
3646
3647         $LFS setquota -g $TSTUSR2 -b 0 -B 300M $DIR ||
3648         error "failed to setquota on group $TSTUSR2"
3649
3650         $LFS quota -v -g $TSTUSR2 $DIR
3651
3652         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile ||
3653         error "chgrp should succeed"
3654
3655         $LFS quota -v -g $TSTUSR2 $DIR
3656
3657         cleanup_quota_test
3658 }
3659 run_test 55 "Chgrp should be affected by group quota"
3660
3661 test_56() {
3662         setup_quota_test || error "setup quota failed with $?"
3663
3664         set_ost_qtype $QTYPE || error "enable ost quota failed"
3665         quota_init
3666
3667         $LFS setquota -t -u -b 10 -i 10 $DIR ||
3668                 erro "failed to set grace time for usr quota"
3669         grace_time=$($LFS quota -t -u $DIR | grep "Block grace time:" |
3670                      awk '{print $4 $8}')
3671         if [ "x$grace_time" != "x10s;10s" ]; then
3672                 $LFS quota -t -u $DIR
3673                 error "expected grace time: 10s;10s, got:$grace_time"
3674         fi
3675
3676         cleanup_quota_test
3677 }
3678 run_test 56 "lfs quota -t should work well"
3679
3680 test_57() {
3681         setup_quota_test || error "setup quota failed with $?"
3682
3683         local dir="$DIR/$tdir/dir"
3684         mkdir -p $dir
3685         mkfifo $dir/pipe
3686         #try to change pipe file should not hang and return failure
3687         wait_update_facet client "$LFS project -sp 1 $dir/pipe 2>&1 |
3688                 awk -F ':' '{ print \\\$2 }'" \
3689                         " unable to get xattr for fifo '$dir/pipe'" || return 1
3690         #command can process further if it hit some errors
3691         touch $dir/aaa $dir/bbb
3692         mkdir $dir/subdir -p
3693         touch $dir/subdir/aaa $dir/subdir/bbb
3694         #create one invalid link file
3695         ln -s $dir/not_exist_file $dir/ccc
3696         local cnt=$(lfs project -r $dir 2>/dev/null | wc -l)
3697         [ $cnt -eq 5 ] || error "expected 5 got $cnt"
3698
3699         cleanup_quota_test
3700 }
3701 run_test 57 "lfs project could tolerate errors"
3702
3703 test_59() {
3704         [ "$mds1_FSTYPE" != ldiskfs ] &&
3705                 skip "ldiskfs only test"
3706         disable_project_quota
3707         setup_quota_test || error "setup quota failed with $?"
3708         quota_init
3709
3710         local testfile="$DIR/$tdir/$tfile-0"
3711         #make sure it did not crash kernel
3712         touch $testfile && lfs project -sp 1 $testfile
3713
3714         enable_project_quota
3715         cleanup_quota_test
3716 }
3717 run_test 59 "lfs project dosen't crash kernel with project disabled"
3718
3719 test_60() {
3720         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
3721                 skip "Needs MDS version 2.11.53 or later."
3722         setup_quota_test || error "setup quota failed with $?"
3723         trap cleanup_quota_test EXIT
3724
3725         local testfile=$DIR/$tdir/$tfile
3726         local limit=100
3727
3728         set_mdt_qtype "ug" || error "enable mdt quota failed"
3729
3730         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $limit $DIR ||
3731                 error "set quota failed"
3732         quota_show_check a g $TSTUSR
3733
3734         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $DIR/$tdir failed"
3735         chmod g+s $DIR/$tdir || error "chmod g+s failed"
3736         $RUNAS createmany -m ${testfile} $((limit-1)) ||
3737                 error "create many files failed"
3738
3739         $RUNAS touch $DIR/$tdir/foo && error "regular user should fail"
3740
3741         # root user can overrun quota
3742         runas -u 0 -g 0 touch $DIR/$tdir/foo ||
3743                 error "root user should succeed"
3744
3745         cleanup_quota_test
3746 }
3747 run_test 60 "Test quota for root with setgid"
3748
3749 # test default quota
3750 test_default_quota() {
3751         [ "$MDS1_VERSION" -lt $(version_code 2.11.51) ] &&
3752                 skip "Not supported before 2.11.51."
3753
3754         local qtype=$1
3755         local qpool=$2
3756         local qid=$TSTUSR
3757         local qprjid=$TSTPRJID
3758         local qdtype="-U"
3759         local qs="-b"
3760         local qh="-B"
3761         local LIMIT=20480 #20M disk space
3762         local TESTFILE="$DIR/$tdir/$tfile-0"
3763
3764         [ $qtype == "-p" ] && ! is_project_quota_supported &&
3765                 echo "Project quota is not supported" && return 0
3766
3767         [ $qtype == "-u" ] && qdtype="-U"
3768         [ $qtype == "-g" ] && qdtype="-G"
3769         [ $qtype == "-p" ] && {
3770                 qdtype="-P"
3771                 qid=$qprjid
3772         }
3773
3774         [ $qpool == "meta" ] && {
3775                 LIMIT=10240 #10K inodes
3776                 qs="-i"
3777                 qh="-I"
3778         }
3779
3780         setup_quota_test || error "setup quota failed with $?"
3781         trap cleanup_quota_test EXIT
3782
3783         quota_init
3784
3785         # enable mdt/ost quota
3786         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
3787         set_ost_qtype $QTYPE || error "enable ost quota failed"
3788
3789         log "set to use default quota"
3790         $LFS setquota $qtype $qid -d $DIR ||
3791                 error "set $qid to use default quota failed"
3792
3793         log "set default quota"
3794         $LFS setquota $qdtype $qs ${LIMIT} $qh ${LIMIT} $DIR ||
3795                 error "set $qid default quota failed"
3796
3797         log "get default quota"
3798         $LFS quota $qdtype $DIR || error "get default quota failed"
3799
3800         if [ $qpool == "data" ]; then
3801                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
3802                                                         awk '{print $2}')
3803                 [ $SLIMIT -eq $LIMIT ] ||
3804                         error "the returned default quota is wrong"
3805         else
3806                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
3807                                                         awk '{print $5}')
3808                 [ $SLIMIT -eq $LIMIT ] ||
3809                         error "the returned default quota is wrong"
3810         fi
3811
3812         # make sure the system is clean
3813         local USED=$(getquota $qtype $qid global curspace)
3814         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
3815
3816         $LFS setstripe $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
3817         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
3818
3819         [ $qtype == "-p" ] && change_project -sp $TSTPRJID $DIR/$tdir
3820
3821         log "Test not out of quota"
3822         if [ $qpool == "data" ]; then
3823                 $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 >> 10)) oflag=sync ||
3824                         quota_error $qtype $qid "write failed, expect succeed"
3825         else
3826                 $RUNAS createmany -m $TESTFILE $((LIMIT/2)) ||
3827                         quota_error $qtype $qid "create failed, expect succeed"
3828
3829                 unlinkmany $TESTFILE $((LIMIT/2))
3830         fi
3831
3832         log "Test out of quota"
3833         # flush cache, ensure noquota flag is set on client
3834         cancel_lru_locks osc
3835         cancel_lru_locks mdc
3836         sync; sync_all_data || true
3837         if [ $qpool == "data" ]; then
3838                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
3839                         quota_error $qtype $qid "write succeed, expect EDQUOT"
3840         else
3841                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
3842                         quota_error $qtype $qid "create succeed, expect EDQUOT"
3843
3844                 unlinkmany $TESTFILE $((LIMIT*2))
3845         fi
3846
3847         log "Increase default quota"
3848         # increase default quota
3849         $LFS setquota $qdtype $qs $((LIMIT*3)) $qh $((LIMIT*3)) $DIR ||
3850                 error "set default quota failed"
3851
3852         cancel_lru_locks osc
3853         cancel_lru_locks mdc
3854         sync; sync_all_data || true
3855         if [ $qpool == "data" ]; then
3856                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
3857                         quota_error $qtype $qid "write failed, expect succeed"
3858         else
3859                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
3860                         quota_error $qtype $qid "create failed, expect succeed"
3861
3862                 unlinkmany $TESTFILE $((LIMIT*2))
3863         fi
3864
3865         log "Set quota to override default quota"
3866         $LFS setquota $qtype $qid $qs ${LIMIT} $qh ${LIMIT} $DIR ||
3867                 error "set $qid quota failed"
3868
3869         cancel_lru_locks osc
3870         cancel_lru_locks mdc
3871         sync; sync_all_data || true
3872         if [ $qpool == "data" ]; then
3873                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
3874                         quota_error $qtype $qid "write succeed, expect EQUOT"
3875         else
3876                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
3877                         quota_error $qtype $qid "create succeed, expect EQUOT"
3878
3879                 unlinkmany $TESTFILE $((LIMIT*2))
3880         fi
3881
3882         log "Set to use default quota again"
3883         $LFS setquota $qtype $qid -d $DIR ||
3884                 error "set $qid to use default quota failed"
3885
3886         cancel_lru_locks osc
3887         cancel_lru_locks mdc
3888         sync; sync_all_data || true
3889         if [ $qpool == "data" ]; then
3890                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
3891                         quota_error $qtype $qid "write failed, expect succeed"
3892         else
3893                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
3894                         quota_error $qtype $qid "create failed, expect succeed"
3895
3896                 unlinkmany $TESTFILE $((LIMIT*2))
3897         fi
3898
3899         log "Cleanup"
3900         rm -f $TESTFILE
3901         wait_delete_completed || error "wait_delete_completed failed"
3902         sync_all_data || true
3903         $LFS setquota $qdtype -b 0 -B 0 -i 0 -I 0 $DIR ||
3904                 error "reset default quota failed"
3905         $LFS setquota $qtype $qid -b 0 -B 0 -i 0 -I 0 $DIR ||
3906                 error "reset quota failed"
3907
3908         cleanup_quota_test
3909 }
3910
3911 test_61() {
3912         test_default_quota "-u" "data"
3913         test_default_quota "-u" "meta"
3914         test_default_quota "-g" "data"
3915         test_default_quota "-g" "meta"
3916         test_default_quota "-p" "data"
3917         test_default_quota "-p" "meta"
3918 }
3919 run_test 61 "default quota tests"
3920
3921 test_62() {
3922         ! is_project_quota_supported &&
3923                 skip "Project quota is not supported"
3924          [[ "$(chattr -h 2>&1)" =~ "project" ]] ||
3925                 skip "chattr did not support project quota"
3926         setup_quota_test || error "setup quota failed with $?"
3927         local testdir=$DIR/$tdir/
3928
3929         $RUNAS mkdir -p $testdir || error "failed to mkdir"
3930         change_project -s $testdir
3931         [[ $($LFS project -d $testdir) =~ "P" ]] ||
3932                 error "inherit attribute should be set"
3933         # chattr used FS_IOC_SETFLAGS ioctl
3934         $RUNAS chattr -P $testdir &&
3935                 error "regular user clear inherit should fail"
3936         [[ $($LFS project -d $testdir) =~ "P" ]] ||
3937                 error "inherit attribute should still be set"
3938         chattr -P $testdir || error "root failed to clear inherit"
3939         [[ $($LFS project -d $testdir) =~ "P" ]] &&
3940                 error "inherit attribute should be cleared"
3941         cleanup_quota_test
3942 }
3943 run_test 62 "Project inherit should be only changed by root"
3944
3945 test_dom() {
3946         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ] &&
3947                 skip "Not supported before 2.11.55"
3948
3949         local qtype=$1
3950         local qid=$TSTUSR
3951         local dd_failed=false
3952         local tdir_dom=${tdir}_dom
3953         local LIMIT=20480 #20M
3954
3955         [ $qtype == "p" ] && ! is_project_quota_supported &&
3956                 echo "Project quota is not supported" && return 0
3957
3958         [ $qtype == "p" ] && qid=$TSTPRJID
3959
3960         setup_quota_test || error "setup quota failed with $?"
3961         trap cleanup_quota_test EXIT
3962
3963         quota_init
3964
3965         # enable mdt/ost quota
3966         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
3967         set_ost_qtype $QTYPE || error "enable ost quota failed"
3968
3969         # make sure the system is clean
3970         local USED=$(getquota -$qtype $qid global curspace)
3971         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
3972
3973         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $tdir failed"
3974
3975         mkdir $DIR/$tdir_dom || error "mkdir $tdir_dom failed"
3976         $LFS setstripe -E 1M -L mdt $DIR/$tdir_dom ||
3977                 error "setstripe $tdir_dom failed"
3978         chown $TSTUSR.$TSTUSR $DIR/$tdir_dom || error "chown $tdir_dom failed"
3979
3980         [ $qtype == "p" ] && {
3981                 change_project -sp $TSTPRJID $DIR/$tdir
3982                 change_project -sp $TSTPRJID $DIR/$tdir_dom
3983         }
3984
3985         $LFS setquota -$qtype $qid -b $LIMIT -B $LIMIT $DIR ||
3986                 error "set $qid quota failed"
3987
3988         for ((i = 0; i < $((LIMIT/2048)); i++)); do
3989                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3990                                                                 dd_failed=true
3991         done
3992
3993         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
3994
3995         for ((i = $((LIMIT/2048)); i < $((LIMIT/1024 + 10)); i++)); do
3996                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3997                                                                 dd_failed=true
3998         done
3999
4000         $dd_failed || quota_error $qtype $qid "write succeed, expect EDQUOT"
4001
4002         rm -f $DIR/$tdir_dom/*
4003
4004         # flush cache, ensure noquota flag is set on client
4005         cancel_lru_locks osc
4006         cancel_lru_locks mdc
4007         sync; sync_all_data || true
4008
4009         dd_failed=false
4010
4011         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048)) oflag=sync ||
4012                 quota_error $qtype $qid "write failed, expect succeed"
4013
4014         for ((i = 0; i < $((LIMIT/2048 + 10)); i++)); do
4015                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4016                                                                 dd_failed=true
4017         done
4018
4019         $dd_failed || quota_error $qtype $TSTID "write succeed, expect EDQUOT"
4020
4021         rm -f $DIR/$tdir/*
4022         rm -f $DIR/$tdir_dom/*
4023
4024         # flush cache, ensure noquota flag is set on client
4025         cancel_lru_locks osc
4026         cancel_lru_locks mdc
4027         sync; sync_all_data || true
4028
4029         dd_failed=false
4030
4031         for ((i = 0; i < $((LIMIT/2048)); i++)); do
4032                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
4033                                                                 dd_failed=true
4034         done
4035
4036         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
4037
4038         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048 + 10)) oflag=sync &&
4039                 quota_error $qtype $qid "write succeed, expect EDQUOT"
4040
4041         rm -f $DIR/$tdir/*
4042         rm -fr $DIR/$tdir_dom
4043
4044         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
4045                 error "reset usr quota failed"
4046
4047         cleanup_quota_test
4048 }
4049
4050 test_63() {
4051         test_dom "u"
4052         test_dom "g"
4053         test_dom "p"
4054 }
4055 run_test 63 "quota on DoM tests"
4056
4057 test_64() {
4058         ! is_project_quota_supported &&
4059                 skip "Project quota is not supported"
4060         setup_quota_test || error "setup quota failed with $?"
4061         local dir1="$DIR/$tdir/"
4062
4063         touch $dir1/file
4064         ln -s $dir1/file $dir1/file_link
4065
4066         $LFS project -sp $TSTPRJID $dir1/file_link >&/dev/null &&
4067                 error "set symlink file's project should fail"
4068
4069         $LFS project $TSTPRJID $dir1/file_link >&/dev/null &&
4070                 error "get symlink file's project should fail"
4071
4072         cleanup_quota_test
4073 }
4074 run_test 64 "lfs project on symlink files should fail"
4075
4076 test_65() {
4077         local SIZE=10 #10M
4078         local TESTFILE="$DIR/$tdir/$tfile-0"
4079
4080         setup_quota_test || error "setup quota failed with $?"
4081         set_ost_qtype $QTYPE || error "enable ost quota failed"
4082         quota_init
4083
4084         echo "Write..."
4085         $RUNAS $DD of=$TESTFILE count=$SIZE ||
4086                 error "failed to write"
4087         # flush cache, ensure noquota flag is set on client
4088         cancel_lru_locks osc
4089         sync; sync_all_data || true
4090
4091         local quota_u=$($LFS quota -u $TSTUSR $DIR)
4092         local quota_g=$($LFS quota -g $TSTUSR $DIR)
4093         local quota_all=$($RUNAS $LFS quota $DIR)
4094
4095         [ "$(echo "$quota_all" | head -n3)" != "$quota_u" ] &&
4096                 error "usr quota not match"
4097         [ "$(echo "$quota_all" | tail -n3)" != "$quota_g" ] &&
4098                 error "grp quota not match"
4099
4100         rm -f $TESTFILE
4101         # cleanup
4102         cleanup_quota_test
4103 }
4104 run_test 65 "Check lfs quota result"
4105
4106 test_66() {
4107         ! is_project_quota_supported &&
4108                 skip "Project quota is not supported"
4109         [ "$MDS1_VERSION" -lt $(version_code 2.12.4) ] &&
4110                 skip "Not supported before 2.12.4"
4111         setup_quota_test || error "setup quota failed with $?"
4112         stack_trap cleanup_quota_test EXIT
4113         local old=$(do_facet mds1 $LCTL get_param -n \
4114                     mdt.*.enable_chprojid_gid | head -1)
4115         local testdir=$DIR/$tdir/foo
4116
4117         do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=0
4118         stack_trap "do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=0" \
4119                 EXIT
4120
4121         test_mkdir -i 0 -c 1 $testdir || error "failed to mkdir"
4122         chown -R $TSTID:$TSTID $testdir
4123         change_project -sp $TSTPRJID $testdir
4124         $RUNAS mkdir $testdir/foo || error "failed to mkdir foo"
4125
4126         $RUNAS lfs project -p 0 $testdir/foo &&
4127                 error "nonroot user should fail to set projid"
4128
4129         $RUNAS lfs project -C $testdir/foo &&
4130                 error "nonroot user should fail to clear projid"
4131
4132         change_project -C $testdir/foo || error "failed to clear project"
4133
4134         do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=-1
4135         $RUNAS lfs project -p $TSTPRJID $testdir/foo || error \
4136         "failed to set projid with normal user when enable_chprojid_gid=-1"
4137
4138         $RUNAS lfs project -rC $testdir/ || error \
4139 "failed to clear project state with normal user when enable_chprojid_gid=-1"
4140
4141         touch $testdir/bar || error "failed touch $testdir/bar"
4142         $RUNAS lfs project -p $TSTPRJID $testdir/bar && error \
4143         "normal user should not be able to set projid on root owned file"
4144
4145         change_project -p $TSTPRJID $testdir/bar || error \
4146                 "root should be able to change its own file's projid"
4147
4148         cleanup_quota_test
4149 }
4150 run_test 66 "nonroot user can not change project state in default"
4151
4152 test_67_write() {
4153         local file="$1"
4154         local qtype="$2"
4155         local size=$3
4156         local _runas=""
4157         local short_qtype=${qtype:0:1}
4158
4159         echo "file "$file
4160         echo "0 $0 1 $1 2 $2 3 $3 4 $4"
4161         case "$4" in
4162                 quota_usr)  _runas=$RUNAS;;
4163                 quota_2usr) _runas=$RUNAS2;;
4164                 *)          error "unknown quota parameter $4";;
4165         esac
4166
4167         log "Write..."
4168         date
4169         $_runas $DD of=$file count=$size ||
4170                 quota_error $short_qtype $TSTUSR \
4171                         "$qtype write failure, but expect success"
4172         date
4173         cancel_lru_locks osc
4174         date
4175         sync; sync_all_data || true
4176         date
4177 }
4178
4179 getgranted() {
4180         local pool=$1
4181         local ptype=$2
4182         local userid=$3
4183         local qtype=$4
4184         local param=qmt.$FSNAME-QMT0000.$ptype-$pool.glb-$qtype
4185
4186         do_facet mds1 $LCTL get_param $param |
4187                 grep -A2 $userid | awk -F'[, ]*' 'NR==2{print $9}'
4188 }
4189
4190 test_67() {
4191         local limit=20  # 20M
4192         local testfile="$DIR/$tdir/$tfile-0"
4193         local testfile2="$DIR/$tdir/$tfile-1"
4194         local testfile3="$DIR/$tdir/$tfile-2"
4195         local qpool="qpool1"
4196         local used
4197         local granted
4198         local granted_mb
4199
4200         mds_supports_qp
4201         [ "$ost1_FSTYPE" == zfs ] &&
4202                 skip "ZFS grants some block space together with inode"
4203
4204         setup_quota_test || error "setup quota failed with $?"
4205         trap cleanup_quota_test EXIT
4206
4207         # enable ost quota
4208         set_ost_qtype $QTYPE || error "enable ost quota failed"
4209
4210         # test for user
4211         log "User quota (block hardlimit:$limit MB)"
4212         $LFS setquota -u $TSTUSR -b 0 -B ${limit}M -i 0 -I 0 $DIR ||
4213                 error "set user quota failed"
4214
4215         # make sure the system is clean
4216         used=$(getquota -u $TSTUSR global curspace)
4217         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
4218
4219         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4220         echo "granted 0x0 before write $granted"
4221
4222         # trigger reintegration
4223         local procf="osd-$(facet_fstype ost1).$FSNAME-OST*."
4224         procf=${procf}quota_slave.force_reint
4225         do_facet ost1 $LCTL set_param $procf=1 ||
4226                 error "force reintegration failed"
4227         wait_ost_reint "u" || error "reintegration failed"
4228         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4229         [ $granted -ne 0 ] &&
4230                 error "Granted($granted) for $TSTUSR in $qpool isn't 0."
4231
4232         $LFS setstripe $testfile -c 1 -i 0 || error "setstripe $testfile failed"
4233         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4234
4235         # write 10 MB to testfile
4236         test_67_write "$testfile" "user" 10 "quota_usr"
4237
4238         # create qpool and add OST1
4239         pool_add $qpool || error "pool_add failed"
4240         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
4241         # as quota_usr hasn't limits, lqe may absent. But it should be
4242         # created after the 1st direct qmt_get.
4243         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
4244
4245         # check granted - should be 0, as testfile is located only on OST0
4246         granted=$(getgranted "0x0" "dt" $TSTID "usr")
4247         echo "global granted $granted"
4248         granted=$(getgranted $qpool "dt" $TSTID "usr")
4249         echo "$qpool granted $granted"
4250         [ $granted -ne 0 ] &&
4251                 error "Granted($granted) for $TSTUSR in $qpool isn't 0."
4252
4253         # add OST0 to qpool and check granted space
4254         pool_add_targets $qpool 0 1 ||
4255                 error "pool_add_targets failed"
4256         granted_mb=$(($(getgranted $qpool "dt" $TSTID "usr")/1024))
4257         echo "Granted $granted_mb MB"
4258         #should be 10M + qunit for each OST
4259         [ $granted_mb -ge 10 -a $granted_mb -lt $limit ] ||
4260                 error "Granted($granted_mb) for $TSTUSR in $qpool is wrong."
4261
4262         $LFS setstripe $testfile2 -c 1 -i 1 ||
4263                 error "setstripe $testfile2 failed"
4264         chown $TSTUSR2.$TSTUSR2 $testfile2 || error "chown $testfile2 failed"
4265         # Write from another user and check that qpool1
4266         # shows correct granted, despite quota_2usr hasn't limits in qpool1.
4267         test_67_write "$testfile2" "user" 10 "quota_2usr"
4268         used=$(getquota -u $TSTUSR2 global curspace $qpool)
4269         granted=$(getgranted $qpool "dt" $TSTID2 "usr")
4270         [ $granted -ne 0 ] &&
4271                 error "Granted($granted) for $TSTUSR2 in $qpool isn't 0."
4272
4273         # Granted space for quota_2usr in qpool1 should appear only
4274         # when global lqe for this user becomes enforced.
4275         $LFS setquota -u $TSTUSR2 -B ${limit}M $DIR ||
4276                 error "set user quota failed"
4277         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4278         echo "granted_mb $granted_mb"
4279         [ $granted_mb -ge 10 -a $granted_mb -lt $limit ] ||
4280                 error "Granted($granted) for $TSTUSR in $qpool is wrong."
4281
4282         $LFS setstripe $testfile3 -c 1 -i 0 ||
4283                 error "setstripe $testfile3 failed"
4284         chown $TSTUSR2.$TSTUSR2 $testfile3 || error "chown $testfile3 failed"
4285         test_67_write "$testfile3" "user" 10 "quota_2usr"
4286         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4287         echo "$testfile3 granted_mb $granted_mb"
4288         [ $granted_mb -eq $limit ] ||
4289                 error "Granted($granted_mb) for $TSTUSR2 is not equal to 20M"
4290
4291         # remove OST1 from the qpool1 and check granted space
4292         # should be 0 for TSTUSR and 10M for TSTUSR2
4293         pool_remove_target $qpool 0
4294         granted_mb=$(($(getgranted $qpool "dt" $TSTID "usr")/1024))
4295         [ $granted_mb -eq 0 ] ||
4296                 error "Granted($granted_mb) for $TSTUSR in $qpool != 0."
4297         granted_mb=$(($(getgranted $qpool "dt" $TSTID2 "usr")/1024))
4298         [ $granted_mb -eq 10 ] ||
4299                 error "Granted($granted_mb) for $TSTUSR2 is not equal to 10M"
4300
4301         rm -f $testfile
4302         wait_delete_completed || error "wait_delete_completed failed"
4303         sync_all_data || true
4304         used=$(getquota -u $TSTUSR global curspace)
4305         [ $used -ne 0 ] && quota_error u $TSTUSR \
4306                 "user quota isn't released after deletion"
4307         resetquota -u $TSTUSR
4308
4309         cleanup_quota_test
4310 }
4311 run_test 67 "quota pools recalculation"
4312
4313 get_slave_nr() {
4314         local pool=$1
4315         local qtype=$2
4316         local nr
4317
4318         do_facet mds1 $LCTL get_param -n qmt.$FSNAME-QMT0000.dt-$pool.info |
4319                 awk '/usr/ {getline; print $2}'
4320 }
4321
4322 test_68()
4323 {
4324         local qpool="qpool1"
4325
4326         mds_supports_qp
4327         setup_quota_test || error "setup quota failed with $?"
4328         stack_trap cleanup_quota_test EXIT
4329
4330         # enable ost quota
4331         set_ost_qtype $QTYPE || error "enable ost quota failed"
4332
4333         # check slave number for glbal pool
4334         local nr=$(get_slave_nr "0x0" "usr")
4335         echo "nr result $nr"
4336         [[ $nr != $((OSTCOUNT + MDSCOUNT)) ]] &&
4337                 error "Slave_nr $nr for global pool != ($OSTCOUNT + $MDSCOUNT)"
4338
4339         # create qpool and add OST1
4340         pool_add $qpool || error "pool_add failed"
4341         nr=$(get_slave_nr $qpool "usr")
4342         [[ $nr != 0 ]] && error "Slave number $nr for $qpool != 0"
4343
4344         # add OST1 to qpool
4345         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
4346         nr=$(get_slave_nr $qpool "usr")
4347         [[ $nr != 1 ]] && error "Slave number $nr for $qpool != 1"
4348
4349         # add OST0 to qpool
4350         pool_add_targets $qpool 0 1 || error "pool_add_targets failed"
4351         nr=$(get_slave_nr $qpool "usr")
4352         [[ $nr != 2 ]] && error "Slave number $nr for $qpool != 2"
4353
4354         # remove OST0
4355         pool_remove_target $qpool 0
4356         nr=$(get_slave_nr $qpool "usr")
4357         [[ $nr != 1 ]] && error "Slave number $nr for $qpool != 1"
4358
4359         # remove OST1
4360         pool_remove_target $qpool 1
4361         nr=$(get_slave_nr $qpool "usr")
4362         [[ $nr != 0 ]] && error "Slave number $nr for $qpool != 0"
4363
4364         # Check again that all is fine with global pool
4365         nr=$(get_slave_nr "0x0" "usr")
4366         [[ $nr != $((OSTCOUNT + MDSCOUNT)) ]] &&
4367                 error "Slave_nr $nr for global pool != ($OSTCOUNT + $MDSCOUNT)"
4368
4369         cleanup_quota_test
4370 }
4371 run_test 68 "slave number in quota pool changed after each add/remove OST"
4372
4373 test_69()
4374 {
4375         local global_limit=200  # 200M
4376         local limit=10  # 10M
4377         local testfile="$DIR/$tdir/$tfile-0"
4378         local dom0="$DIR/$tdir/dom0"
4379         local qpool="qpool1"
4380
4381         mds_supports_qp
4382         setup_quota_test || error "setup quota failed with $?"
4383         stack_trap cleanup_quota_test EXIT
4384
4385         # enable ost quota
4386         set_ost_qtype $QTYPE || error "enable ost quota failed"
4387         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
4388
4389         # Save DOM only at MDT0
4390         $LFS setdirstripe -c 1 -i 0 $dom0 || error "cannot create $dom0"
4391         $LFS setstripe -E 1M $dom0 -L mdt || error "setstripe to $dom0 failed"
4392         chmod 0777 $dom0
4393         $LFS setstripe -c 1 -i 0 "$DIR/$tdir/"
4394
4395         # create qpool and add OST0
4396         pool_add $qpool || error "pool_add failed"
4397         pool_add_targets $qpool 0 0 || error "pool_add_targets failed"
4398
4399         log "User quota (block hardlimit:$global_limit MB)"
4400         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
4401                 error "set user quota failed"
4402
4403         log "User quota (block hardlimit:$limit MB)"
4404         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
4405                 error "set user quota failed"
4406
4407         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 oflag=sync ||
4408                 quota_error u $TSTUSR "write failed"
4409
4410         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 seek=512 \
4411                 oflag=sync || quota_error u $TSTUSR "write failed"
4412
4413         $RUNAS $DD of=$testfile count=$limit || true
4414
4415         # flush cache, ensure noquota flag is set on client
4416         cancel_lru_locks osc
4417         sync; sync_all_data || true
4418
4419         # MDT0 shouldn't get EDQUOT with glimpse.
4420         $RUNAS $DD of=$testfile count=$limit seek=$limit &&
4421                 quota_error u $TSTUSR \
4422                         "user write success, but expect EDQUOT"
4423
4424         # Now all members of qpool1 should get EDQUOT. Expect success
4425         # when write to DOM on MDT0, as it belongs to global pool.
4426         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 \
4427                 oflag=sync || quota_error u $TSTUSR "write failed"
4428
4429         $RUNAS dd if=/dev/zero of="$dom0/f1" bs=1K count=512 seek=512 \
4430                 oflag=sync || quota_error u $TSTUSR "write failed"
4431
4432         cleanup_quota_test
4433 }
4434 run_test 69 "EDQUOT at one of pools shouldn't affect DOM"
4435
4436 test_70()
4437 {
4438         local qpool="qpool1"
4439         local limit=20
4440         local err=0
4441         local bhard
4442
4443         [[ CLIENT_VERSION -lt $(version_code $VERSION_WITH_QP) ]] &&
4444                 skip "Needs a client >= $VERSION_WITH_QP"
4445
4446         setup_quota_test || error "setup quota failed with $?"
4447         stack_trap cleanup_quota_test EXIT
4448
4449         # MDS returns EFAULT for unsupported quotactl command
4450         [[ $MDS1_VERSION -lt $(version_code $VERSION_WITH_QP) ]] && err=14
4451
4452         # create qpool and add OST0
4453         pool_add $qpool || error "pool_add failed"
4454         pool_add_targets $qpool 0 0 || error "pool_add_targets failed"
4455
4456         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR
4457         rc=$?
4458         [ $rc -eq $err ] || error "setquota res $rc != $err"
4459
4460         # If MDS supports QP, check that limit was set properly.
4461         if [[ $MDS1_VERSION -ge $(version_code $VERSION_WITH_QP) ]]; then
4462                 bhard=$(getquota -u $TSTUSR global bhardlimit $qpool)
4463                 echo "hard limit $bhard limit $limit"
4464                 [ $bhard -ne $((limit*1024)) ] &&
4465                         error "bhard:$bhard for $qpool!=$((limit*1024))"
4466         fi
4467
4468         $LFS quota -u $TSTUSR --pool $qpool $DIR
4469         rc=$?
4470         [ $rc -eq $err ] || error "quota res $rc != $err"
4471
4472         cleanup_quota_test
4473 }
4474 run_test 70 "check lfs setquota/quota with a pool option"
4475
4476 test_71a()
4477 {
4478         local limit=10  # 10M
4479         local global_limit=100  # 100M
4480         local testfile="$DIR/$tdir/$tfile-0"
4481         local qpool="qpool1"
4482         local qpool2="qpool2"
4483
4484         [ "$ost1_FSTYPE" == zfs ] &&
4485                 skip "ZFS grants some block space together with inode"
4486         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs"
4487         mds_supports_qp
4488         setup_quota_test || error "setup quota failed with $?"
4489         stack_trap cleanup_quota_test EXIT
4490
4491         # enable ost quota
4492         set_ost_qtype $QTYPE || error "enable ost quota failed"
4493
4494         # test for user
4495         log "User quota (block hardlimit:$global_limit MB)"
4496         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
4497                 error "set user quota failed"
4498
4499         pool_add $qpool || error "pool_add failed"
4500         pool_add_targets $qpool 0 1 ||
4501                 error "pool_add_targets failed"
4502
4503         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
4504                 error "set user quota failed"
4505
4506         pool_add $qpool2 || error "pool_add failed"
4507         pool_add_targets $qpool2 1 1 ||
4508                 error "pool_add_targets failed"
4509
4510         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool2 $DIR ||
4511                 error "set user quota failed"
4512
4513         # make sure the system is clean
4514         local used=$(getquota -u $TSTUSR global curspace)
4515
4516         echo "used $used"
4517         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
4518
4519         # create 1st component 1-10M
4520         $LFS setstripe -E 10M -S 1M -c 1 -i 0 $testfile
4521         #create 2nd component 10-30M
4522         $LFS setstripe --component-add -E 30M -c 1 -i 1 $testfile
4523         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4524
4525         # сheck normal use and out of quota with PFL
4526         # 1st element is in qppol1(OST0), 2nd in qpool2(OST2).
4527         test_1_check_write $testfile "user" $((limit*2))
4528         rm -f $testfile
4529         wait_delete_completed || error "wait_delete_completed failed"
4530         sync_all_data || true
4531         used=$(getquota -u $TSTUSR global curspace)
4532         [ $used -ne 0 ] && quota_error u $TSTUSR \
4533                 "user quota isn't released after deletion"
4534
4535         # create 1st component 1-10M
4536         $LFS setstripe -E 10M -S 1M -c 1 -i 0 $testfile
4537         # create 2nd component 10-30M
4538         $LFS setstripe --component-add -E 30M -c 1 -i 1 $testfile
4539         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4540
4541         # write to the 2nd component
4542         $RUNAS $DD of=$testfile count=$limit seek=10 ||
4543                 quota_error u $TSTUSR \
4544                         "write failure, but expect success"
4545         # this time maybe cache write,  ignore it's failure
4546         $RUNAS $DD of=$testfile count=$((2*limit)) seek=10 || true
4547         cancel_lru_locks osc
4548         sync; sync_all_data || true
4549         # write over limit in qpool2(2nd component 10-30M)
4550         $RUNAS $DD of=$testfile count=1 seek=$((10 + 2*limit)) &&
4551                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
4552         # write to the 1st component - OST0 is empty
4553         $RUNAS $DD of=$testfile count=$limit seek=0 ||
4554                 quota_error u $TSTUSR "write failed"
4555
4556         cleanup_quota_test
4557 }
4558 run_test 71a "Check PFL with quota pools"
4559
4560 test_71b()
4561 {
4562         local global_limit=1000 # 1G
4563         local limit1=160 # 160M
4564         local limit2=10 # 10M
4565         local testfile="$DIR/$tdir/$tfile-0"
4566         local qpool="qpool1"
4567         local qpool2="qpool2"
4568
4569         [ "$ost1_FSTYPE" == zfs ] &&
4570                 skip "ZFS grants some block space together with inode"
4571         [[ $OSTCOUNT -lt 2 ]] && skip "need >= 2 OSTs" && return
4572         mds_supports_qp
4573         setup_quota_test || error "setup quota failed with $?"
4574         stack_trap cleanup_quota_test EXIT
4575
4576         # enable ost quota
4577         set_ost_qtype $QTYPE || error "enable ost quota failed"
4578
4579         # test for user
4580         log "User quota (block hardlimit:$global_limit MB)"
4581         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
4582                 error "set user quota failed"
4583
4584         pool_add $qpool || error "pool_add failed"
4585         pool_add_targets $qpool 0 1 ||
4586                 error "pool_add_targets failed"
4587
4588         $LFS setquota -u $TSTUSR -B ${limit1}M --pool $qpool $DIR ||
4589                 error "set user quota failed"
4590
4591         pool_add $qpool2 || error "pool_add failed"
4592         pool_add_targets $qpool2 1 1 ||
4593                 error "pool_add_targets failed"
4594
4595         $LFS setquota -u $TSTUSR -B ${limit2}M --pool $qpool2 $DIR ||
4596                 error "set user quota failed"
4597
4598         # make sure the system is clean
4599         local used=$(getquota -u $TSTUSR global curspace)
4600
4601         echo "used $used"
4602         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
4603
4604         # First component is on OST0, 2nd on OST1
4605         $LFS setstripe -E 128M -i 0 -z 64M -E -1 -i 1 -z 64M $testfile
4606         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4607
4608         # fill the 1st component on OST0
4609         $RUNAS $DD of=$testfile count=128 ||
4610                 quota_error u $TSTUSR "write failed"
4611         # write to the 2nd cmpnt on OST1
4612         $RUNAS $DD of=$testfile count=$((limit2/2)) seek=128 ||
4613                 quota_error u $TSTUSR "write failed"
4614         # this time maybe cache write,  ignore it's failure
4615         $RUNAS $DD of=$testfile count=$((limit2/2)) seek=$((128 + limit2/2)) ||
4616                 true
4617         cancel_lru_locks osc
4618         sync; sync_all_data || true
4619         # write over limit in qpool2
4620         $RUNAS $DD of=$testfile count=2 seek=$((128 + limit2)) &&
4621                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
4622
4623         cleanup_quota_test
4624 }
4625 run_test 71b "Check SEL with quota pools"
4626
4627 test_72()
4628 {
4629         local limit=10  # 10M
4630         local global_limit=50  # 50M
4631         local testfile="$DIR/$tdir/$tfile-0"
4632         local qpool="qpool1"
4633
4634         mds_supports_qp
4635         setup_quota_test || error "setup quota failed with $?"
4636         stack_trap cleanup_quota_test EXIT
4637
4638         # enable ost quota
4639         set_ost_qtype $QTYPE || error "enable ost quota failed"
4640
4641         # test for user
4642         log "User quota (block hardlimit:$global_limit MB)"
4643         $LFS setquota -u $TSTUSR -b 0 -B ${global_limit}M -i 0 -I 0 $DIR ||
4644                 error "set user quota failed"
4645
4646         pool_add $qpool || error "pool_add failed"
4647         pool_add_targets $qpool 1 1 || error "pool_add_targets failed"
4648
4649         $LFS setquota -u $TSTUSR -B ${limit}M --pool $qpool $DIR ||
4650                 error "set user quota failed"
4651
4652         # make sure the system is clean
4653         local used=$(getquota -u $TSTUSR global curspace)
4654         echo "used $used"
4655         [ $used -ne 0 ] && error "Used space($used) for user $TSTUSR isn't 0."
4656
4657         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
4658
4659         $LFS setstripe $testfile -c 1 -i 1 || error "setstripe $testfile failed"
4660         chown $TSTUSR.$TSTUSR $testfile || error "chown $testfile failed"
4661         test_1_check_write $testfile "user" $limit
4662         used=$(getquota -u $TSTUSR global bhardlimit $qpool)
4663         echo "used $used"
4664         [ $used -ge $limit ] || error "used($used) is less than limit($limit)"
4665         # check that lfs quota -uv --pool prints only OST that
4666         # was added in a pool
4667         lfs quota -v -u quota_usr --pool $qpool $DIR | grep -v "OST0001" |
4668                 grep "OST\|MDT" && error "$qpool consists wrong targets"
4669
4670         cleanup_quota_test
4671 }
4672 run_test 72 "lfs quota --pool prints only pool's OSTs"
4673
4674 quota_fini()
4675 {
4676         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=-quota"
4677         if $PQ_CLEANUP; then
4678                 disable_project_quota
4679         fi
4680 }
4681 reset_quota_settings
4682 quota_fini
4683
4684 cd $ORIG_PWD
4685 complete $SECONDS
4686 check_and_cleanup_lustre
4687 export QUOTA_AUTO=$QUOTA_AUTO_OLD
4688 exit_status