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