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