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