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