Whamcloud - gitweb
bde71556be68247c2e4f3d97b82ee4d7f7053e4d
[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
43 require_dsh_mds || exit 0
44 require_dsh_ost || exit 0
45
46 # Does e2fsprogs support quota feature?
47 if [ "$mds1_FSTYPE" == ldiskfs ] &&
48         do_facet $SINGLEMDS "! $DEBUGFS -c -R supported_features |
49                 grep -q 'quota'"; then
50         skip_env "e2fsprogs doesn't support quota"
51 fi
52
53 QUOTALOG=${TESTSUITELOG:-$TMP/$(basename $0 .sh).log}
54
55 [ "$QUOTALOG" ] && rm -f $QUOTALOG || true
56
57 DIR=${DIR:-$MOUNT}
58 DIR2=${DIR2:-$MOUNT2}
59
60 QUOTA_AUTO_OLD=$QUOTA_AUTO
61 export QUOTA_AUTO=0
62
63 check_and_setup_lustre
64
65 ENABLE_PROJECT_QUOTAS=${ENABLE_PROJECT_QUOTAS:-true}
66
67 SHOW_QUOTA_USER="$LFS quota -v -u $TSTUSR $DIR"
68 SHOW_QUOTA_USERID="$LFS quota -v -u $TSTID $DIR"
69 SHOW_QUOTA_GROUP="$LFS quota -v -g $TSTUSR $DIR"
70 SHOW_QUOTA_GROUPID="$LFS quota -v -g $TSTID $DIR"
71 SHOW_QUOTA_PROJID="eval is_project_quota_supported && $LFS quota -v -p $TSTPRJID $DIR"
72 SHOW_QUOTA_INFO_USER="$LFS quota -t -u $DIR"
73 SHOW_QUOTA_INFO_GROUP="$LFS quota -t -g $DIR"
74 SHOW_QUOTA_INFO_PROJID="eval is_project_quota_supported && $LFS quota -t -p $DIR"
75
76 lustre_fail() {
77         local fail_node=$1
78         local fail_loc=$2
79         local fail_val=${3:-0}
80         local NODES=
81
82         case $fail_node in
83         mds_ost|mdt_ost) NODES="$(comma_list $(mdts_nodes) $(osts_nodes))";;
84         mds|mdt) NODES="$(comma_list $(mdts_nodes))";;
85         ost) NODES="$(comma_list $(osts_nodes))";;
86         esac
87
88         do_nodes $NODES "lctl set_param fail_val=$fail_val fail_loc=$fail_loc"
89 }
90
91 change_project()
92 {
93         echo "lfs project $*"
94         lfs project $* || error "lfs project $* failed"
95 }
96
97 RUNAS="runas -u $TSTID -g $TSTID"
98 RUNAS2="runas -u $TSTID2 -g $TSTID2"
99 DD="dd if=/dev/zero bs=1M"
100
101 FAIL_ON_ERROR=false
102
103 # clear quota limits for a user or a group
104 # usage: resetquota -u username
105 #        resetquota -g groupname
106 #        resetquota -p projid
107
108 resetquota() {
109         [ "$#" != 2 ] && error "resetquota: wrong number of arguments: $#"
110         [ "$1" != "-u" -a "$1" != "-g" -a "$1" != "-p" ] &&
111                 error "resetquota: wrong specifier $1 passed"
112
113         if [ $1 == "-p" ]; then
114                 is_project_quota_supported || return 0
115         fi
116
117         $LFS setquota "$1" "$2" -b 0 -B 0 -i 0 -I 0 $MOUNT ||
118                 error "clear quota for [type:$1 name:$2] failed"
119         # give a chance to slave to release space
120         sleep 1
121 }
122
123 quota_scan() {
124         local local_ugp=$1
125         local local_id=$2
126
127         if [ "$local_ugp" == "a" -o "$local_ugp" == "u" ]; then
128                 $LFS quota -v -u $local_id $DIR
129                 log "Files for user ($local_id):"
130                 ($LFS find --user $local_id $DIR | head -n 4 |
131                         xargs stat 2>/dev/null)
132         fi
133
134         if [ "$local_ugp" == "a" -o "$local_ugp" == "g" ]; then
135                 $LFS quota -v -g $local_id $DIR
136                 log "Files for group ($local_id):"
137                 ($LFS find --group $local_id $DIR | head -n 4 |
138                         xargs stat 2>/dev/null)
139         fi
140
141         if [ "$local_ugp" == "a" -o "$local_ugp" == "p" ]; then
142                 $LFS quota -v -p $TSTPRJID $DIR
143                 log "Files for project ($TSTPRJID):"
144                 ($LFS find --projid $TSTPRJID $DIR | head -n 4 |
145                         xargs stat 2>/dev/null)
146         fi
147 }
148
149 quota_error() {
150         quota_scan $1 $2
151         shift 2
152         error "$*"
153 }
154
155 quota_log() {
156         quota_scan $1 $2
157         shift 2
158         log "$*"
159 }
160
161 # get quota for a user or a group
162 # usage: getquota -u|-g|-p <username>|<groupname>|<projid> global|<obd_uuid> \
163 #                 bhardlimit|bsoftlimit|bgrace|ihardlimit|isoftlimit|igrace
164 getquota() {
165         local spec
166         local uuid
167
168         sync_all_data > /dev/null 2>&1 || true
169
170         [ "$#" != 4 ] && error "getquota: wrong number of arguments: $#"
171         [ "$1" != "-u" -a "$1" != "-g" -a "$1" != "-p" ] &&
172                 error "getquota: wrong u/g/p specifier $1 passed"
173
174         uuid="$3"
175
176         case "$4" in
177                 curspace)   spec=1;;
178                 bsoftlimit) spec=2;;
179                 bhardlimit) spec=3;;
180                 bgrace)     spec=4;;
181                 curinodes)  spec=5;;
182                 isoftlimit) spec=6;;
183                 ihardlimit) spec=7;;
184                 igrace)     spec=8;;
185                 *)          error "unknown quota parameter $4";;
186         esac
187
188         [ "$uuid" = "global" ] && uuid=$DIR
189
190         $LFS quota -v "$1" "$2" $DIR |
191                 awk 'BEGIN { num='$spec' } { if ($1 == "'$uuid'") \
192                 { if (NF == 1) { getline } else { num++ } ; print $num;} }' \
193                 | tr -d "*"
194 }
195
196 # set mdt quota type
197 # usage: set_mdt_qtype ugp|u|g|p|none
198 set_mdt_qtype() {
199         local qtype=$1
200         local varsvc
201         local mdts=$(get_facets MDS)
202         local cmd
203         [[ "$qtype" =~ "p" ]] && ! is_project_quota_supported &&
204                 qtype=$(tr -d 'p' <<<$qtype)
205
206         if [[ $PERM_CMD == *"set_param -P"* ]]; then
207                 do_facet mgs $PERM_CMD \
208                         osd-*.$FSNAME-MDT*.quota_slave.enable=$qtype
209         else
210                 do_facet mgs $PERM_CMD $FSNAME.quota.mdt=$qtype
211         fi
212         # we have to make sure each MDT received config changes
213         for mdt in ${mdts//,/ }; do
214                 varsvc=${mdt}_svc
215                 cmd="$LCTL get_param -n "
216                 cmd=${cmd}osd-$(facet_fstype $mdt).${!varsvc}
217                 cmd=${cmd}.quota_slave.enabled
218
219                 if $(facet_up $mdt); then
220                         wait_update_facet $mdt "$cmd" "$qtype" || return 1
221                 fi
222         done
223         return 0
224 }
225
226 # set ost quota type
227 # usage: set_ost_qtype ugp|u|g|p|none
228 set_ost_qtype() {
229         local qtype=$1
230         local varsvc
231         local osts=$(get_facets OST)
232         local cmd
233         [[ "$qtype" =~ "p" ]] && ! is_project_quota_supported &&
234                 qtype=$(tr -d 'p' <<<$qtype)
235
236         if [[ $PERM_CMD == *"set_param -P"* ]]; then
237                 do_facet mgs $PERM_CMD \
238                         osd-*.$FSNAME-OST*.quota_slave.enable=$qtype
239         else
240                 do_facet mgs $PERM_CMD $FSNAME.quota.ost=$qtype
241         fi
242         # we have to make sure each OST received config changes
243         for ost in ${osts//,/ }; do
244                 varsvc=${ost}_svc
245                 cmd="$LCTL get_param -n "
246                 cmd=${cmd}osd-$(facet_fstype $ost).${!varsvc}
247                 cmd=${cmd}.quota_slave.enabled
248
249                 if $(facet_up $ost); then
250                         wait_update_facet $ost "$cmd" "$qtype" || return 1
251                 fi
252         done
253         return 0
254 }
255
256 wait_reintegration() {
257         local ntype=$1
258         local qtype=$2
259         local max=$3
260         local result="glb[1],slv[1],reint[0]"
261         local varsvc
262         local cmd
263         local tgts
264
265         if [ $ntype == "mdt" ]; then
266                 tgts=$(get_facets MDS)
267         else
268                 tgts=$(get_facets OST)
269         fi
270
271         for tgt in ${tgts//,/ }; do
272                 varsvc=${tgt}_svc
273                 cmd="$LCTL get_param -n "
274                 cmd=${cmd}osd-$(facet_fstype $tgt).${!varsvc}
275                 cmd=${cmd}.quota_slave.info
276
277                 if $(facet_up $tgt); then
278                         wait_update_facet $tgt "$cmd |
279                                 grep "$qtype" | awk '{ print \\\$3 }'" \
280                                         "$result" $max || return 1
281                 fi
282         done
283         return 0
284 }
285
286 wait_mdt_reint() {
287         local qtype=$1
288         local max=${2:-90}
289
290         if [[ "$qtype" =~ "u" ]]; then
291                 wait_reintegration "mdt" "user" $max || return 1
292         fi
293
294         if [[ "$qtype" =~ "g" ]]; then
295                 wait_reintegration "mdt" "group" $max || return 1
296         fi
297
298         if [[ "$qtype" =~ "p" ]]; then
299                 ! is_project_quota_supported && return 0
300                 wait_reintegration "mdt" "project" $max || return 1
301         fi
302         return 0
303 }
304
305 wait_ost_reint() {
306         local qtype=$1
307         local max=${2:-90}
308
309         if [[ "$qtype" =~ "u" ]]; then
310                 wait_reintegration "ost" "user" $max || return 1
311         fi
312
313         if [[ "$qtype" =~ "g" ]]; then
314                 wait_reintegration "ost" "group" $max || return 1
315         fi
316
317         if [[ "$qtype" =~ "p" ]]; then
318                 ! is_project_quota_supported && return 0
319                 wait_reintegration "ost" "project" $max || return 1
320         fi
321         return 0
322 }
323
324 wait_grace_time() {
325         local qtype=$1
326         local flavour=$2
327         local extrasleep=${3:-5}
328         local qarg
329
330         case $qtype in
331                 u|g) qarg=$TSTUSR ;;
332                 p) qarg=$TSTPRJID ;;
333                 *) error "get_grace_time: Invalid quota type: $qtype"
334         esac
335
336         case $flavour in
337                 block)
338                         time=$(lfs quota -$qtype $qarg $DIR|
339                                    awk 'NR == 3{ print $5 }'| sed 's/s$//')
340                         ;;
341                 file)
342                         time=$(lfs quota -$qtype $qarg $DIR|
343                                    awk 'NR == 3{ print $9 }'| sed 's/s$//')
344                         ;;
345                 *)
346                         error "Unknown quota type: $flavour"
347                         ;;
348         esac
349
350         echo "Sleep through grace ..."
351         [ "$time" == "-" ] &&
352             error "Grace timeout was not set or quota not exceeded"
353         if [ "$time" == "none" ]; then
354             echo "...Grace timeout already expired"
355         else
356                 let time+=$extrasleep
357                 echo "...sleep $time seconds"
358                 sleep $time
359         fi
360 }
361
362 setup_quota_test() {
363         wait_delete_completed
364         echo "Creating test directory"
365         mkdir $DIR/$tdir || return 1
366         chmod 0777 $DIR/$tdir || return 2
367         # always clear fail_loc in case of fail_loc isn't cleared
368         # properly when previous test failed
369         lustre_fail mds_ost 0
370 }
371
372 cleanup_quota_test() {
373         trap 0
374         echo "Delete files..."
375         rm -rf $DIR/$tdir
376         echo "Wait for unlink objects finished..."
377         wait_delete_completed
378         sync_all_data || true
379         reset_quota_settings
380 }
381
382 quota_show_check() {
383         local bf=$1
384         local ugp=$2
385         local qid=$3
386         local usage
387
388         $LFS quota -v -$ugp $qid $DIR
389
390         if [ "$bf" == "a" -o "$bf" == "b" ]; then
391                 usage=$(getquota -$ugp $qid global curspace)
392                 if [ -z $usage ]; then
393                         quota_error $ugp $qid \
394                                 "Query block quota failed ($ugp:$qid)."
395                 else
396                         [ $usage -ne 0 ] && quota_log $ugp $qid \
397                                 "Block quota isn't 0 ($ugp:$qid:$usage)."
398                 fi
399         fi
400
401         if [ "$bf" == "a" -o "$bf" == "f" ]; then
402                 usage=$(getquota -$ugp $qid global curinodes)
403                 if [ -z $usage ]; then
404                         quota_error $ugp $qid \
405                                 "Query file quota failed ($ugp:$qid)."
406                 else
407                         [ $usage -ne 0 ] && quota_log $ugp $qid \
408                                 "File quota isn't 0 ($ugp:$qid:$usage)."
409                 fi
410         fi
411 }
412
413 project_quota_enabled () {
414         local rc=0
415         local zfeat="feature@project_quota"
416
417         for facet in $(seq -f mds%g $MDSCOUNT) $(seq -f ost%g $OSTCOUNT); do
418                 local facet_fstype=${facet:0:3}1_FSTYPE
419                 local devname
420
421                 if [ "${!facet_fstype}" = "zfs" ]; then
422                         devname=$(zpool_name ${facet})
423                         do_facet ${facet} $ZPOOL get -H "$zfeat" $devname |
424                                 grep -wq active || rc=1
425                 else
426                         [ ${facet:0:3} == "mds" ] &&
427                                 devname=$(mdsdevname ${facet:3}) ||
428                                 devname=$(ostdevname ${facet:3})
429                         do_facet ${facet} $DEBUGFS -R features $devname |
430                                 grep -q project || rc=1
431                 fi
432         done
433         [ $rc -eq 0 ] && PQ_CLEANUP=false || PQ_CLEANUP=true
434         return $rc
435 }
436
437 project_quota_enabled || enable_project_quota
438
439 reset_quota_settings() {
440         resetquota -u $TSTUSR
441         resetquota -u $TSTID
442         resetquota -g $TSTUSR
443         resetquota -g $TSTID
444         resetquota -u $TSTUSR2
445         resetquota -u $TSTID2
446         resetquota -g $TSTUSR2
447         resetquota -g $TSTID2
448         resetquota -p $TSTPRJID
449 }
450
451 # enable quota debug
452 quota_init() {
453         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=+quota"
454 }
455 quota_init
456 reset_quota_settings
457
458 check_runas_id_ret $TSTUSR $TSTUSR $RUNAS ||
459         error "Please create user $TSTUSR($TSTID) and group $TSTUSR($TSTID)"
460 check_runas_id_ret $TSTUSR2 $TSTUSR2 $RUNAS2 ||
461         error "Please create user $TSTUSR2($TSTID2) and group $TSTUSR2($TSTID2)"
462
463 test_quota_performance() {
464         local TESTFILE="$DIR/$tdir/$tfile-0"
465         local size=$1 # in MB
466         local stime=$(date +%s)
467         $RUNAS $DD of=$TESTFILE count=$size conv=fsync ||
468                 quota_error u $TSTUSR "write failure"
469         local etime=$(date +%s)
470         delta=$((etime - stime))
471         if [ $delta -gt 0 ]; then
472                 rate=$((size * 1024 / delta))
473                 if [ "$mds1_FSTYPE" = zfs ]; then
474                         # LU-2872 - see LU-2887 for fix
475                         [ $rate -gt 64 ] ||
476                                 error "SLOW IO for $TSTUSR (user): $rate KB/sec"
477                 else
478                         [ $rate -gt 1024 ] ||
479                                 error "SLOW IO for $TSTUSR (user): $rate KB/sec"
480                 fi
481         fi
482         rm -f $TESTFILE
483 }
484
485 # test basic quota performance b=21696
486 test_0() {
487         local MB=100 # 100M
488         [ "$SLOW" = "no" ] && MB=10
489
490         local free_space=$(lfs_df | grep "summary" | awk '{print $4}')
491         [ $free_space -le $((MB * 1024)) ] &&
492                 skip "not enough space ${free_space} KB, " \
493                         "required $((MB * 1024)) KB"
494         setup_quota_test || error "setup quota failed with $?"
495         trap cleanup_quota_test EXIT
496
497         set_ost_qtype "none" || error "disable ost quota failed"
498         test_quota_performance $MB
499
500         set_ost_qtype $QTYPE || error "enable ost quota failed"
501         $LFS setquota -u $TSTUSR -b 0 -B 10G -i 0 -I 0 $DIR ||
502                 error "set quota failed"
503         test_quota_performance $MB
504
505         cleanup_quota_test
506 }
507 run_test 0 "Test basic quota performance"
508
509 # test block hardlimit
510 test_1() {
511         local LIMIT=10  # 10M
512         local TESTFILE="$DIR/$tdir/$tfile-0"
513
514         setup_quota_test || error "setup quota failed with $?"
515         trap cleanup_quota_test EXIT
516
517         # enable ost quota
518         set_ost_qtype $QTYPE || error "enable ost quota failed"
519
520         # test for user
521         log "User quota (block hardlimit:$LIMIT MB)"
522         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
523                 error "set user quota failed"
524
525         # make sure the system is clean
526         local USED=$(getquota -u $TSTUSR global curspace)
527         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
528
529         $LFS setstripe $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
530         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
531
532         log "Write..."
533         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) ||
534                 quota_error u $TSTUSR "user write failure, but expect success"
535         log "Write out of block quota ..."
536         # this time maybe cache write,  ignore it's failure
537         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
538         # flush cache, ensure noquota flag is set on client
539         cancel_lru_locks osc
540         sync; sync_all_data || true
541         $RUNAS $DD of=$TESTFILE count=1 seek=$LIMIT &&
542                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
543
544         rm -f $TESTFILE
545         wait_delete_completed || error "wait_delete_completed failed"
546         sync_all_data || true
547         USED=$(getquota -u $TSTUSR global curspace)
548         [ $USED -ne 0 ] && quota_error u $TSTUSR \
549                 "user quota isn't released after deletion"
550         resetquota -u $TSTUSR
551
552         # test for group
553         log "--------------------------------------"
554         log "Group quota (block hardlimit:$LIMIT MB)"
555         $LFS setquota -g $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
556                 error "set group quota failed"
557
558         TESTFILE="$DIR/$tdir/$tfile-1"
559         # make sure the system is clean
560         USED=$(getquota -g $TSTUSR global curspace)
561         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTUSR isn't 0"
562
563         $LFS setstripe $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
564         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
565
566         log "Write ..."
567         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) ||
568                 quota_error g $TSTUSR "Group write failure, but expect success"
569         log "Write out of block quota ..."
570         # this time maybe cache write, ignore it's failure
571         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
572         cancel_lru_locks osc
573         sync; sync_all_data || true
574         $RUNAS $DD of=$TESTFILE count=10 seek=$LIMIT &&
575                 quota_error g $TSTUSR "Group write success, but expect EDQUOT"
576         rm -f $TESTFILE
577         wait_delete_completed || error "wait_delete_completed failed"
578         sync_all_data || true
579         USED=$(getquota -g $TSTUSR global curspace)
580         [ $USED -ne 0 ] && quota_error g $TSTUSR \
581                                 "Group quota isn't released after deletion"
582         resetquota -g $TSTUSR
583
584         if ! is_project_quota_supported; then
585                 echo "Project quota is not supported"
586                 cleanup_quota_test
587                 return 0
588         fi
589
590         TESTFILE="$DIR/$tdir/$tfile-2"
591         # make sure the system is clean
592         USED=$(getquota -p $TSTPRJID global curspace)
593         [ $USED -ne 0 ] &&
594                 error "used space($USED) for project $TSTPRJID isn't 0"
595
596         # test for Project
597         log "--------------------------------------"
598         log "Project quota (block hardlimit:$LIMIT mb)"
599         $LFS setquota -p $TSTPRJID -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
600                 error "set project quota failed"
601
602         $LFS setstripe $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
603         chown $TSTUSR:$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
604         change_project -p $TSTPRJID $TESTFILE
605
606         log "write ..."
607         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) || quota_error p $TSTPRJID \
608                 "project write failure, but expect success"
609         log "write out of block quota ..."
610         # this time maybe cache write, ignore it's failure
611         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
612         cancel_lru_locks osc
613         sync; sync_all_data || true
614         $RUNAS $DD of=$TESTFILE count=10 seek=$LIMIT && quota_error p \
615                 $TSTPRJID "project write success, but expect EDQUOT"
616
617         # cleanup
618         cleanup_quota_test
619
620         USED=$(getquota -p $TSTPRJID global curspace)
621         [ $USED -eq 0 ] || quota_error p $TSTPRJID \
622                 "project quota isn't released after deletion"
623 }
624 run_test 1 "Block hard limit (normal use and out of quota)"
625
626 # test inode hardlimit
627 test_2() {
628         local LIMIT=$((1024 * 1024)) # 1M inodes
629         local TESTFILE="$DIR/$tdir/$tfile-0"
630
631         [ "$SLOW" = "no" ] && LIMIT=1024 # 1k inodes
632
633         local FREE_INODES=$(mdt_free_inodes 0)
634         echo "$FREE_INODES free inodes on master MDT"
635         [ $FREE_INODES -lt $LIMIT ] &&
636                 skip "not enough free inodes $FREE_INODES required $LIMIT"
637
638         setup_quota_test || error "setup quota failed with $?"
639         trap cleanup_quota_test EXIT
640
641         # enable mdt quota
642         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
643
644         # test for user
645         log "User quota (inode hardlimit:$LIMIT files)"
646         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
647                 error "set user quota failed"
648
649         # make sure the system is clean
650         local USED=$(getquota -u $TSTUSR global curinodes)
651         [ $USED -ne 0 ] && error "Used inodes($USED) for user $TSTUSR isn't 0."
652
653         log "Create $LIMIT files ..."
654         $RUNAS createmany -m ${TESTFILE} $LIMIT ||
655                 quota_error u $TSTUSR "user create failure, but expect success"
656         log "Create out of file quota ..."
657         $RUNAS touch ${TESTFILE}_xxx &&
658                 quota_error u $TSTUSR "user create success, but expect EDQUOT"
659
660         # cleanup
661         unlinkmany ${TESTFILE} $LIMIT || error "unlinkmany $TESTFILE failed"
662         rm -f ${TESTFILE}_xxx
663         wait_delete_completed
664
665         USED=$(getquota -u $TSTUSR global curinodes)
666         [ $USED -ne 0 ] && quota_error u $TSTUSR \
667                 "user quota isn't released after deletion"
668         resetquota -u $TSTUSR
669
670         # test for group
671         log "--------------------------------------"
672         log "Group quota (inode hardlimit:$LIMIT files)"
673         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
674                 error "set group quota failed"
675
676         TESTFILE=$DIR/$tdir/$tfile-1
677         # make sure the system is clean
678         USED=$(getquota -g $TSTUSR global curinodes)
679         [ $USED -ne 0 ] && error "Used inodes($USED) for group $TSTUSR isn't 0."
680
681         log "Create $LIMIT files ..."
682         $RUNAS createmany -m ${TESTFILE} $LIMIT ||
683                 quota_error g $TSTUSR "group create failure, but expect success"
684         log "Create out of file quota ..."
685         $RUNAS touch ${TESTFILE}_xxx &&
686                 quota_error g $TSTUSR "group create success, but expect EDQUOT"
687
688         # cleanup
689         unlinkmany ${TESTFILE} $LIMIT || error "unlinkmany $TESTFILE failed"
690         rm -f ${TESTFILE}_xxx
691         wait_delete_completed
692
693         USED=$(getquota -g $TSTUSR global curinodes)
694         [ $USED -ne 0 ] && quota_error g $TSTUSR \
695                 "user quota isn't released after deletion"
696
697         resetquota -g $TSTUSR
698         ! is_project_quota_supported && cleanup_quota_test &&
699                 echo "Skip project quota is not supported" && return 0
700
701         # test for project
702         log "--------------------------------------"
703         log "Project quota (inode hardlimit:$LIMIT files)"
704         $LFS setquota -p $TSTPRJID -b 0 -B 0 -i 0 -I $LIMIT $DIR ||
705                 error "set project quota failed"
706
707         TESTFILE=$DIR/$tdir/$tfile-1
708         # make sure the system is clean
709         USED=$(getquota -p $TSTPRJID global curinodes)
710         [ $USED -ne 0 ] &&
711                 error "Used inodes($USED) for project $TSTPRJID isn't 0"
712
713         change_project -sp $TSTPRJID $DIR/$tdir
714         log "Create $LIMIT files ..."
715         $RUNAS createmany -m ${TESTFILE} $((LIMIT-1)) || quota_error p \
716                 $TSTPRJID "project create fail, but expect success"
717         log "Create out of file quota ..."
718         $RUNAS touch ${TESTFILE}_xxx && quota_error p $TSTPRJID \
719                 "project create success, but expect EDQUOT"
720         change_project -C $DIR/$tdir
721
722         cleanup_quota_test
723         USED=$(getquota -p $TSTPRJID global curinodes)
724         [ $USED -eq 0 ] || quota_error p $TSTPRJID \
725                 "project quota isn't released after deletion"
726
727 }
728 run_test 2 "File hard limit (normal use and out of quota)"
729
730 test_block_soft() {
731         local TESTFILE=$1
732         local GRACE=$2
733         local LIMIT=$3
734         local OFFSET=0
735         local qtype=$4
736
737         setup_quota_test
738         trap cleanup_quota_test EXIT
739
740         $LFS setstripe $TESTFILE -c 1 -i 0
741         chown $TSTUSR.$TSTUSR $TESTFILE
742         [ "$qtype" == "p" ] && is_project_quota_supported &&
743                 change_project -p $TSTPRJID $TESTFILE
744
745         echo "Write up to soft limit"
746         $RUNAS $DD of=$TESTFILE count=$LIMIT ||
747                 quota_error a $TSTUSR "write failure, but expect success"
748         OFFSET=$((LIMIT * 1024))
749         cancel_lru_locks osc
750
751         echo "Write to exceed soft limit"
752         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET ||
753                 quota_error a $TSTUSR "write failure, but expect success"
754         OFFSET=$((OFFSET + 1024)) # make sure we don't write to same block
755         cancel_lru_locks osc
756
757         $SHOW_QUOTA_USER
758         $SHOW_QUOTA_GROUP
759         $SHOW_QUOTA_PROJID
760         $SHOW_QUOTA_INFO_USER
761         $SHOW_QUOTA_INFO_GROUP
762         $SHOW_QUOTA_INFO_PROJID
763
764         echo "Write before timer goes off"
765         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET ||
766                 quota_error a $TSTUSR "write failure, but expect success"
767         OFFSET=$((OFFSET + 1024))
768         cancel_lru_locks osc
769
770         wait_grace_time $qtype "block"
771
772         $SHOW_QUOTA_USER
773         $SHOW_QUOTA_GROUP
774         $SHOW_QUOTA_PROJID
775         $SHOW_QUOTA_INFO_USER
776         $SHOW_QUOTA_INFO_GROUP
777         $SHOW_QUOTA_INFO_PROJID
778
779         echo "Write after timer goes off"
780         # maybe cache write, ignore.
781         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET || true
782         OFFSET=$((OFFSET + 1024))
783         cancel_lru_locks osc
784         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET &&
785                 quota_error a $TSTUSR "write success, but expect EDQUOT"
786
787         $SHOW_QUOTA_USER
788         $SHOW_QUOTA_GROUP
789         $SHOW_QUOTA_PROJID
790         $SHOW_QUOTA_INFO_USER
791         $SHOW_QUOTA_INFO_GROUP
792         $SHOW_QUOTA_INFO_PROJID
793
794         echo "Unlink file to stop timer"
795         rm -f $TESTFILE
796         wait_delete_completed
797         sync_all_data || true
798
799         $SHOW_QUOTA_USER
800         $SHOW_QUOTA_GROUP
801         $SHOW_QUOTA_PROJID
802         $SHOW_QUOTA_INFO_USER
803         $SHOW_QUOTA_INFO_GROUP
804         $SHOW_QUOTA_INFO_PROJID
805
806         $LFS setstripe $TESTFILE -c 1 -i 0
807         chown $TSTUSR.$TSTUSR $TESTFILE
808         [ "$qtype" == "p" ] && change_project -p $TSTPRJID $TESTFILE
809
810         echo "Write ..."
811         $RUNAS $DD of=$TESTFILE count=$LIMIT ||
812                 quota_error a $TSTUSR "write failure, but expect success"
813         # cleanup
814         cleanup_quota_test
815 }
816
817 # block soft limit
818 test_3() {
819         local GRACE=20 # 20s
820         if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
821             GRACE=60
822         fi
823         local TESTFILE=$DIR/$tdir/$tfile-0
824
825         # get minimum soft qunit size
826         local LIMIT=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
827                 qmt.$FSNAME-QMT0000.dt-0x0.soft_least_qunit) / 1024 ))
828
829         set_ost_qtype $QTYPE || error "enable ost quota failed"
830
831         echo "User quota (soft limit:$LIMIT MB  grace:$GRACE seconds)"
832         # make sure the system is clean
833         local USED=$(getquota -u $TSTUSR global curspace)
834         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
835
836         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
837                 $MAX_IQ_TIME $DIR || error "set user grace time failed"
838         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
839                 error "set user quota failed"
840
841         test_block_soft $TESTFILE $GRACE $LIMIT "u"
842
843         echo "Group quota (soft limit:$LIMIT MB  grace:$GRACE seconds)"
844         TESTFILE=$DIR/$tdir/$tfile-1
845         # make sure the system is clean
846         USED=$(getquota -g $TSTUSR global curspace)
847         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
848
849         $LFS setquota -t -g --block-grace $GRACE --inode-grace \
850                 $MAX_IQ_TIME $DIR || error "set group grace time failed"
851         $LFS setquota -g $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
852                 error "set group quota failed"
853
854         test_block_soft $TESTFILE $GRACE $LIMIT "g"
855
856         if is_project_quota_supported; then
857                 echo "Project quota (soft limit:$LIMIT MB  grace:$GRACE sec)"
858                 TESTFILE=$DIR/$tdir/$tfile-2
859                 # make sure the system is clean
860                 USED=$(getquota -p $TSTPRJID global curspace)
861                 [ $USED -ne 0 ] && error \
862                         "Used space($USED) for project $TSTPRJID isn't 0."
863
864                 $LFS setquota -t -p --block-grace $GRACE --inode-grace \
865                         $MAX_IQ_TIME $DIR ||
866                                 error "set project grace time failed"
867                 $LFS setquota -p $TSTPRJID -b ${LIMIT}M -B 0 -i 0 -I 0 \
868                         $DIR || error "set project quota failed"
869
870                 test_block_soft $TESTFILE $GRACE $LIMIT "p"
871                 resetquota -p $TSTPRJID
872                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
873                         $MAX_IQ_TIME $DIR ||
874                                 error "restore project grace time failed"
875         fi
876
877         # cleanup
878         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
879                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
880         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
881                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
882 }
883 run_test 3 "Block soft limit (start timer, timer goes off, stop timer)"
884
885 test_file_soft() {
886         local TESTFILE=$1
887         local LIMIT=$2
888         local grace=$3
889         local qtype=$4
890
891         setup_quota_test
892         trap cleanup_quota_test EXIT
893         is_project_quota_supported && change_project -sp $TSTPRJID $DIR/$tdir
894
895         echo "Create files to exceed soft limit"
896         $RUNAS createmany -m ${TESTFILE}_ $((LIMIT + 1)) ||
897                 quota_error a $TSTUSR "create failure, but expect success"
898         local trigger_time=$(date +%s)
899
900         sync_all_data || true
901
902         local cur_time=$(date +%s)
903         [ $(($cur_time - $trigger_time)) -ge $grace ] &&
904                 error "Passed grace time $grace, $trigger_time, $cur_time"
905
906         echo "Create file before timer goes off"
907         $RUNAS touch ${TESTFILE}_before ||
908                 quota_error a $TSTUSR "failed create before timer expired," \
909                         "but expect success. $trigger_time, $cur_time"
910         sync_all_data || true
911
912         wait_grace_time $qtype "file"
913
914         $SHOW_QUOTA_USER
915         $SHOW_QUOTA_GROUP
916         $SHOW_QUOTA_PROJID
917         $SHOW_QUOTA_INFO_USER
918         $SHOW_QUOTA_INFO_GROUP
919         $SHOW_QUOTA_INFO_PROJID
920
921         echo "Create file after timer goes off"
922         # There is a window that space is accounted in the quota usage but
923         # hasn't been decreased from the pending write, if we acquire quota
924         # in this window, we'll acquire more than we needed.
925         $RUNAS touch ${TESTFILE}_after_1 ${TESTFILE}_after_2 || true
926         sync_all_data || true
927         $RUNAS touch ${TESTFILE}_after_3 &&
928                 quota_error a $TSTUSR "create after timer expired," \
929                         "but expect EDQUOT"
930         sync_all_data || true
931
932         $SHOW_QUOTA_USER
933         $SHOW_QUOTA_GROUP
934         $SHOW_QUOTA_PROJID
935         $SHOW_QUOTA_INFO_USER
936         $SHOW_QUOTA_INFO_GROUP
937         $SHOW_QUOTA_INFO_PROJID
938
939         echo "Unlink files to stop timer"
940         find $(dirname $TESTFILE) -name "$(basename ${TESTFILE})*" | xargs rm -f
941         wait_delete_completed
942
943         echo "Create file"
944         $RUNAS touch ${TESTFILE}_xxx ||
945                 quota_error a $TSTUSR "touch after timer stop failure," \
946                         "but expect success"
947         sync_all_data || true
948
949         # cleanup
950         cleanup_quota_test
951 }
952
953 # file soft limit
954 test_4a() {
955         local LIMIT=$(do_facet $SINGLEMDS $LCTL get_param -n \
956                 qmt.$FSNAME-QMT0000.md-0x0.soft_least_qunit)
957         local TESTFILE=$DIR/$tdir/$tfile-0
958         local GRACE=12
959
960         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
961
962         echo "User quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
963         # make sure the system is clean
964         local USED=$(getquota -u $TSTUSR global curinodes)
965         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
966
967         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
968                 $GRACE $DIR || error "set user grace time failed"
969         $LFS setquota -u $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
970                 error "set user quota failed"
971
972         [ "$mds1_FSTYPE" = zfs ] && GRACE=20
973
974         test_file_soft $TESTFILE $LIMIT $GRACE "u"
975
976         echo "Group quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
977         # make sure the system is clean
978         USED=$(getquota -g $TSTUSR global curinodes)
979         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
980
981         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
982                 $GRACE $DIR || error "set group grace time failed"
983         $LFS setquota -g $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
984                 error "set group quota failed"
985         TESTFILE=$DIR/$tdir/$tfile-1
986
987         test_file_soft $TESTFILE $LIMIT $GRACE "g"
988
989         if is_project_quota_supported; then
990                 echo "Project quota (soft limit:$LIMIT files grace:$GRACE sec)"
991                 # make sure the system is clean
992                 USED=$(getquota -p $TSTPRJID global curinodes)
993                 [ $USED -ne 0 ] && error \
994                         "Used space($USED) for project $TSTPRJID isn't 0."
995
996                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
997                         $GRACE $DIR || error "set project grace time failed"
998                 $LFS setquota -p $TSTPRJID -b 0 -B 0 -i $LIMIT -I 0 $DIR ||
999                         error "set project quota failed"
1000
1001                 TESTFILE=$DIR/$tdir/$tfile-1
1002                 # one less than limit, because of parent directory included.
1003                 test_file_soft $TESTFILE $((LIMIT-1)) $GRACE "p"
1004                 resetquota -p $TSTPRJID
1005                 $LFS setquota -t -p --block-grace $MAX_DQ_TIME --inode-grace \
1006                         $MAX_IQ_TIME $DIR ||
1007                                 error "restore project grace time failed"
1008         fi
1009
1010         # cleanup
1011         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1012                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1013         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1014                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1015 }
1016 run_test 4a "File soft limit (start timer, timer goes off, stop timer)"
1017
1018 test_4b() {
1019         local GR_STR1="1w3d"
1020         local GR_STR2="1000s"
1021         local GR_STR3="5s"
1022         local GR_STR4="1w2d3h4m5s"
1023         local GR_STR5="5c"
1024         local GR_STR6="18446744073709551615"
1025         local GR_STR7="-1"
1026
1027         wait_delete_completed
1028
1029         # test of valid grace strings handling
1030         echo "Valid grace strings test"
1031         $LFS setquota -t -u --block-grace $GR_STR1 --inode-grace \
1032                 $GR_STR2 $DIR || error "set user grace time failed"
1033         $LFS quota -u -t $DIR | grep "Block grace time: $GR_STR1"
1034         $LFS setquota -t -g --block-grace $GR_STR3 --inode-grace \
1035                 $GR_STR4 $DIR || error "set group grace time quota failed"
1036         $LFS quota -g -t $DIR | grep "Inode grace time: $GR_STR4"
1037
1038         # test of invalid grace strings handling
1039         echo "  Invalid grace strings test"
1040         ! $LFS setquota -t -u --block-grace $GR_STR4 --inode-grace $GR_STR5 $DIR
1041         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace $GR_STR6 $DIR
1042         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace \
1043                 $GR_STR7 $DIR
1044
1045         # cleanup
1046         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1047                 $MAX_IQ_TIME $DIR || error "restore user grace time failed"
1048         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
1049                 $MAX_IQ_TIME $DIR || error "restore group grace time failed"
1050 }
1051 run_test 4b "Grace time strings handling"
1052
1053 # chown & chgrp (chown & chgrp successfully even out of block/file quota)
1054 test_5() {
1055         local BLIMIT=10 # 10M
1056         local ILIMIT=10 # 10 inodes
1057
1058         setup_quota_test || error "setup quota failed with $?"
1059         trap cleanup_quota_test EXIT
1060
1061         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1062         set_ost_qtype $QTYPE || error "enable ost quota failed"
1063
1064         echo "Set quota limit (0 ${BLIMIT}M 0 $ILIMIT) for $TSTUSR.$TSTUSR"
1065         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
1066                 error "set user quota failed"
1067         $LFS setquota -g $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR ||
1068         if is_project_quota_supported; then
1069                 error "set group quota failed"
1070                 $LFS setquota -p $TSTPRJID -b 0 -B ${BLIMIT}M -i 0 \
1071                         -I $ILIMIT $DIR || error "set project quota failed"
1072         fi
1073
1074         # make sure the system is clean
1075         local USED=$(getquota -u $TSTUSR global curinodes)
1076         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
1077         USED=$(getquota -g $TSTUSR global curinodes)
1078         [ $USED -ne 0 ] && error "Used inode($USED) for group $TSTUSR isn't 0."
1079         USED=$(getquota -u $TSTUSR global curspace)
1080         [ $USED -ne 0 ] && error "Used block($USED) for user $TSTUSR isn't 0."
1081         USED=$(getquota -g $TSTUSR global curspace)
1082         [ $USED -ne 0 ] && error "Used block($USED) for group $TSTUSR isn't 0."
1083         if is_project_quota_supported; then
1084                 USED=$(getquota -p $TSTPRJID global curinodes)
1085                 [ $USED -ne 0 ] &&
1086                         error "Used inode($USED) for project $TSTPRJID isn't 0."
1087                 USED=$(getquota -p $TSTPRJID global curspace)
1088                 [ $USED -ne 0 ] &&
1089                         error "Used block($USED) for project $TSTPRJID isn't 0."
1090         fi
1091
1092         echo "Create more than $ILIMIT files and more than $BLIMIT MB ..."
1093         createmany -m $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
1094                 error "create failure, expect success"
1095         if is_project_quota_supported; then
1096                 touch $DIR/$tdir/$tfile-0_1
1097                 change_project -p $TSTPRJID $DIR/$tdir/$tfile-0_1
1098         fi
1099         $DD of=$DIR/$tdir/$tfile-0_1 count=$((BLIMIT+1)) ||
1100                 error "write failure, expect success"
1101
1102         echo "Chown files to $TSTUSR.$TSTUSR ..."
1103         for i in $(seq 0 $ILIMIT); do
1104                 chown $TSTUSR.$TSTUSR $DIR/$tdir/$tfile-0_$i ||
1105                         quota_error a $TSTUSR "chown failure, expect success"
1106         done
1107
1108         # cleanup
1109         unlinkmany $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
1110                 error "unlinkmany $DIR/$tdir/$tfile-0_ failed"
1111         cleanup_quota_test
1112 }
1113 run_test 5 "Chown & chgrp successfully even out of block/file quota"
1114
1115 # test dropping acquire request on master
1116 test_6() {
1117         local LIMIT=3 # 3M
1118
1119         # Clear dmesg so watchdog is not triggered by previous
1120         # test output
1121         do_facet ost1 dmesg -c > /dev/null
1122
1123         setup_quota_test || error "setup quota failed with $?"
1124         trap cleanup_quota_test EXIT
1125
1126         # make sure the system is clean
1127         local USED=$(getquota -u $TSTUSR global curspace)
1128         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1129
1130         # make sure no granted quota on ost
1131         set_ost_qtype $QTYPE || error "enable ost quota failed"
1132         resetquota -u $TSTUSR
1133
1134         # create file for $TSTUSR
1135         local TESTFILE=$DIR/$tdir/$tfile-$TSTUSR
1136         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1137         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1138
1139         # create file for $TSTUSR2
1140         local TESTFILE2=$DIR/$tdir/$tfile-$TSTUSR2
1141         $LFS setstripe $TESTFILE2 -c 1 -i 0 || error "setstripe $TESTFILE2 failed"
1142         chown $TSTUSR2.$TSTUSR2 $TESTFILE2 || error "chown $TESTFILE2 failed"
1143
1144         # cache per-ID lock for $TSTUSR on slave
1145         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1146                 error "set quota failed"
1147         $RUNAS $DD of=$TESTFILE count=1 ||
1148                 error "write $TESTFILE failure, expect success"
1149         $RUNAS2 $DD of=$TESTFILE2 count=1 ||
1150                 error "write $TESTFILE2 failure, expect success"
1151
1152         if at_is_enabled; then
1153                 at_max_saved=$(at_max_get ost1)
1154                 at_max_set $TIMEOUT ost1
1155
1156                 # write to enforced ID ($TSTUSR) to exceed limit to make sure
1157                 # DQACQ is sent, which makes at_max to take effect
1158                 $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync \
1159                                                                 conv=notrunc
1160                 rm -f $TESTFILE
1161                 wait_delete_completed
1162         fi
1163
1164         sync; sync
1165         sync_all_data || true
1166
1167         #define QUOTA_DQACQ 601
1168         #define OBD_FAIL_PTLRPC_DROP_REQ_OPC 0x513
1169         lustre_fail mds 0x513 601
1170
1171         do_facet ost1 $LCTL set_param \
1172                         osd-*.$FSNAME-OST*.quota_slave.timeout=$((TIMEOUT / 2))
1173
1174         # write to un-enforced ID ($TSTUSR2) should succeed
1175         $RUNAS2 $DD of=$TESTFILE2 count=$LIMIT seek=1 oflag=sync conv=notrunc ||
1176                 error "write failure, expect success"
1177
1178         # write to enforced ID ($TSTUSR) in background, exceeding limit
1179         # to make sure DQACQ is sent
1180         $RUNAS $DD of=$TESTFILE count=$LIMIT seek=1 oflag=sync conv=notrunc &
1181         DDPID=$!
1182
1183         # watchdog timer uses a factor of 2
1184         echo "Sleep for $((TIMEOUT * 2 + 1)) seconds ..."
1185         sleep $((TIMEOUT * 2 + 1))
1186
1187         [ $at_max_saved -ne 0 ] && at_max_set $at_max_saved ost1
1188
1189         # write should be blocked and never finished
1190         if ! ps -p $DDPID  > /dev/null 2>&1; then
1191                 lustre_fail mds 0 0
1192                 error "write finished incorrectly!"
1193         fi
1194
1195         lustre_fail mds 0 0
1196
1197         # no watchdog is triggered
1198         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1199         watchdog=$(awk '/[Ss]ervice thread pid/ && /was inactive/ \
1200                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
1201         [ -z "$watchdog" ] || error "$watchdog"
1202
1203         rm -f $TMP/lustre-log-${TESTNAME}.log
1204
1205         # write should continue then fail with EDQUOT
1206         local count=0
1207         local c_size
1208         while [ true ]; do
1209                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1210                 if [ $count -ge 240 ]; then
1211                         quota_error u $TSTUSR "dd not finished in $count secs"
1212                 fi
1213                 count=$((count + 1))
1214                 if [ $((count % 30)) -eq 0 ]; then
1215                         c_size=$(stat -c %s $TESTFILE)
1216                         echo "Waiting $count secs. $c_size"
1217                         $SHOW_QUOTA_USER
1218                 fi
1219                 sleep 1
1220         done
1221
1222         cleanup_quota_test
1223 }
1224 run_test 6 "Test dropping acquire request on master"
1225
1226 # quota reintegration (global index)
1227 test_7a() {
1228         local TESTFILE=$DIR/$tdir/$tfile
1229         local LIMIT=20 # 20M
1230
1231         [ "$SLOW" = "no" ] && LIMIT=5
1232
1233         setup_quota_test || error "setup quota failed with $?"
1234         trap cleanup_quota_test EXIT
1235
1236         # make sure the system is clean
1237         local USED=$(getquota -u $TSTUSR global curspace)
1238         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1239
1240         # make sure no granted quota on ost1
1241         set_ost_qtype $QTYPE || error "enable ost quota failed"
1242         resetquota -u $TSTUSR
1243         set_ost_qtype "none" || error "disable ost quota failed"
1244
1245         local OSTUUID=$(ostuuid_from_index 0)
1246         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1247         [ $USED -ne 0 ] &&
1248                 error "limit($USED) on $OSTUUID for user $TSTUSR isn't 0"
1249
1250         # create test file
1251         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1252         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1253
1254         echo "Stop ost1..."
1255         stop ost1
1256
1257         echo "Enable quota & set quota limit for $TSTUSR"
1258         set_ost_qtype $QTYPE || error "enable ost quota failed"
1259         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1260                 error "set quota failed"
1261
1262         echo "Start ost1..."
1263         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1264         quota_init
1265
1266         wait_ost_reint $QTYPE || error "reintegration failed"
1267
1268         # hardlimit should have been fetched by slave during global
1269         # reintegration, write will exceed quota
1270         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1271                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1272
1273         rm -f $TESTFILE
1274         wait_delete_completed
1275         sync_all_data || true
1276         sleep 3
1277
1278         echo "Stop ost1..."
1279         stop ost1
1280
1281         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
1282                 error "clear quota failed"
1283
1284         echo "Start ost1..."
1285         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1286         quota_init
1287
1288         wait_ost_reint $QTYPE || error "reintegration failed"
1289
1290         # hardlimit should be cleared on slave during reintegration
1291         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync ||
1292                 quota_error u $TSTUSR "write error, but expect success"
1293
1294         cleanup_quota_test
1295 }
1296 run_test 7a "Quota reintegration (global index)"
1297
1298 # quota reintegration (slave index)
1299 test_7b() {
1300         local LIMIT="100G"
1301         local TESTFILE=$DIR/$tdir/$tfile
1302
1303         setup_quota_test || error "setup quota failed with $?"
1304         trap cleanup_quota_test EXIT
1305
1306         # make sure the system is clean
1307         local USED=$(getquota -u $TSTUSR global curspace)
1308         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1309
1310         # make sure no granted quota on ost1
1311         set_ost_qtype $QTYPE || error "enable ost quota failed"
1312         resetquota -u $TSTUSR
1313         set_ost_qtype "none" || error "disable ost quota failed"
1314
1315         local OSTUUID=$(ostuuid_from_index 0)
1316         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1317         [ $USED -ne 0 ] &&
1318                 error "limit($USED) on $OSTUUID for user $TSTUSR isn't 0"
1319
1320         # create test file
1321         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1322         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1323
1324         # consume some space to make sure the granted space will not
1325         # be released during reconciliation
1326         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1327                 error "consume space failure, expect success"
1328
1329         # define OBD_FAIL_QUOTA_EDQUOT 0xa02
1330         lustre_fail mds 0xa02
1331
1332         set_ost_qtype $QTYPE || error "enable ost quota failed"
1333         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1334                 error "set quota failed"
1335
1336         # ignore the write error
1337         $RUNAS $DD of=$TESTFILE count=1 seek=1 oflag=sync conv=notrunc
1338
1339         local old_used=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1340
1341         lustre_fail mds 0
1342
1343         echo "Restart ost to trigger reintegration..."
1344         stop ost1
1345         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "start ost1 failed"
1346         quota_init
1347
1348         wait_ost_reint $QTYPE || error "reintegration failed"
1349
1350         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1351         [ $USED -gt $old_used ] || error "limit on $OSTUUID $USED <= $old_used"
1352
1353         cleanup_quota_test
1354         $SHOW_QUOTA_USER
1355 }
1356 run_test 7b "Quota reintegration (slave index)"
1357
1358 # quota reintegration (restart mds during reintegration)
1359 test_7c() {
1360         local LIMIT=20 # 20M
1361         local TESTFILE=$DIR/$tdir/$tfile
1362
1363         [ "$SLOW" = "no" ] && LIMIT=5
1364
1365         setup_quota_test || error "setup quota failed with $?"
1366         trap cleanup_quota_test EXIT
1367
1368         # make sure the system is clean
1369         local USED=$(getquota -u $TSTUSR global curspace)
1370         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1371
1372         set_ost_qtype "none" || error "disable ost quota failed"
1373         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR ||
1374                 error "set quota failed"
1375
1376         # define OBD_FAIL_QUOTA_DELAY_REINT 0xa03
1377         lustre_fail ost 0xa03
1378
1379         # enable ost quota
1380         set_ost_qtype $QTYPE || error "enable ost quota failed"
1381         # trigger reintegration
1382         local procf="osd-$ost1_FSTYPE.$FSNAME-OST*."
1383         procf=${procf}quota_slave.force_reint
1384         do_facet ost1 $LCTL set_param $procf=1 ||
1385                 error "force reintegration failed"
1386
1387         echo "Stop mds..."
1388         stop mds1
1389
1390         lustre_fail ost 0
1391
1392         echo "Start mds..."
1393         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
1394         quota_init
1395
1396         # wait longer than usual to make sure the reintegration
1397         # is triggered by quota wb thread.
1398         wait_ost_reint $QTYPE 200 || error "reintegration failed"
1399
1400         # hardlimit should have been fetched by slave during global
1401         # reintegration, write will exceed quota
1402         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1403                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1404
1405         cleanup_quota_test
1406 }
1407 run_test 7c "Quota reintegration (restart mds during reintegration)"
1408
1409 # Quota reintegration (Transfer index in multiple bulks)
1410 test_7d(){
1411         local TESTFILE=$DIR/$tdir/$tfile
1412         local TESTFILE1="$DIR/$tdir/$tfile"-1
1413         local limit=20 #20M
1414
1415         setup_quota_test || error "setup quota failed with $?"
1416         trap cleanup_quota_test EXIT
1417
1418         set_ost_qtype "none" || error "disable ost quota failed"
1419         $LFS setquota -u $TSTUSR -B ${limit}M $DIR ||
1420                 error "set quota for $TSTUSR failed"
1421         $LFS setquota -u $TSTUSR2 -B ${limit}M $DIR ||
1422                 error "set quota for $TSTUSR2 failed"
1423
1424         #define OBD_FAIL_OBD_IDX_READ_BREAK 0x608
1425         lustre_fail mds 0x608 0
1426
1427         # enable quota to tirgger reintegration
1428         set_ost_qtype "u" || error "enable ost quota failed"
1429         wait_ost_reint "u" || error "reintegration failed"
1430
1431         lustre_fail mds 0
1432
1433         # hardlimit should have been fetched by slave during global
1434         # reintegration, write will exceed quota
1435         $RUNAS $DD of=$TESTFILE count=$((limit + 1)) oflag=sync &&
1436                 quota_error u $TSTUSR "$TSTUSR write success, expect EDQUOT"
1437
1438         $RUNAS2 $DD of=$TESTFILE1 count=$((limit + 1)) oflag=sync &&
1439                 quota_error u $TSTUSR2 "$TSTUSR2 write success, expect EDQUOT"
1440
1441         cleanup_quota_test
1442 }
1443 run_test 7d "Quota reintegration (Transfer index in multiple bulks)"
1444
1445 # quota reintegration (inode limits)
1446 test_7e() {
1447         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
1448
1449         # LU-2435: skip this quota test if underlying zfs version has not
1450         # supported native dnode accounting
1451         [ "$mds1_FSTYPE" == zfs ] && {
1452                 local F="feature@userobj_accounting"
1453                 local pool=$(zpool_name mds1)
1454                 local feature=$(do_facet mds1 $ZPOOL get -H $F $pool)
1455
1456                 [[ "$feature" != *" active "* ]] &&
1457                         skip "requires zpool with active userobj_accounting"
1458         }
1459
1460         local ilimit=$((1024 * 2)) # 2k inodes
1461         local TESTFILE=$DIR/${tdir}-1/$tfile
1462
1463         setup_quota_test || error "setup quota failed with $?"
1464         trap cleanup_quota_test EXIT
1465
1466         # make sure the system is clean
1467         local USED=$(getquota -u $TSTUSR global curinodes)
1468         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
1469
1470         # make sure no granted quota on mdt1
1471         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1472         resetquota -u $TSTUSR
1473         set_mdt_qtype "none" || error "disable mdt quota failed"
1474
1475         local MDTUUID=$(mdtuuid_from_index $((MDSCOUNT - 1)))
1476         USED=$(getquota -u $TSTUSR $MDTUUID ihardlimit)
1477         [ $USED -ne 0 ] && error "limit($USED) on $MDTUUID for user" \
1478                 "$TSTUSR isn't 0."
1479
1480         echo "Stop mds${MDSCOUNT}..."
1481         stop mds${MDSCOUNT}
1482
1483         echo "Enable quota & set quota limit for $TSTUSR"
1484         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1485         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
1486                 error "set quota failed"
1487
1488         echo "Start mds${MDSCOUNT}..."
1489         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
1490         quota_init
1491
1492         wait_mdt_reint $QTYPE || error "reintegration failed"
1493
1494         echo "create remote dir"
1495         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/${tdir}-1 ||
1496                 error "create remote dir failed"
1497         chmod 0777 $DIR/${tdir}-1
1498
1499         # hardlimit should have been fetched by slave during global
1500         # reintegration, create will exceed quota
1501         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) &&
1502                 quota_error u $TSTUSR "create succeeded, expect EDQUOT"
1503
1504         $RUNAS unlinkmany $TESTFILE $ilimit || error "unlink files failed"
1505         wait_delete_completed
1506         sync_all_data || true
1507
1508         echo "Stop mds${MDSCOUNT}..."
1509         stop mds${MDSCOUNT}
1510
1511         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
1512                 error "clear quota failed"
1513
1514         echo "Start mds${MDSCOUNT}..."
1515         start mds${MDSCOUNT} $(mdsdevname $MDSCOUNT) $MDS_MOUNT_OPTS
1516         quota_init
1517
1518         wait_mdt_reint $QTYPE || error "reintegration failed"
1519
1520         # hardlimit should be cleared on slave during reintegration
1521         $RUNAS createmany -m $TESTFILE $((ilimit + 1)) ||
1522                 quota_error u $TSTUSR "create failed, expect success"
1523
1524         $RUNAS unlinkmany $TESTFILE $((ilimit + 1)) || error "unlink failed"
1525         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
1526
1527         cleanup_quota_test
1528 }
1529 run_test 7e "Quota reintegration (inode limits)"
1530
1531 # run dbench with quota enabled
1532 test_8() {
1533         local BLK_LIMIT="100g" #100G
1534         local FILE_LIMIT=1000000
1535
1536         setup_quota_test || error "setup quota failed with $?"
1537         trap cleanup_quota_test EXIT
1538
1539         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
1540         set_ost_qtype $QTYPE || error "enable ost quota failed"
1541
1542         echo "Set enough high limit for user: $TSTUSR"
1543         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1544                 error "set user quota failed"
1545         echo "Set enough high limit for group: $TSTUSR"
1546         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1547                 error "set group quota failed"
1548         if is_project_quota_supported; then
1549                 change_project -sp $TSTPRJID $DIR/$tdir
1550                 echo "Set enough high limit for project: $TSTPRJID"
1551                 $LFS setquota -p $TSTPRJID -b 0 \
1552                         -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1553                         error "set project quota failed"
1554         fi
1555
1556         local duration=""
1557         [ "$SLOW" = "no" ] && duration=" -t 120"
1558         $RUNAS bash rundbench -D $DIR/$tdir 3 $duration ||
1559                 quota_error a $TSTUSR "dbench failed!"
1560
1561         is_project_quota_supported && change_project -C $DIR/$tdir
1562         cleanup_quota_test
1563 }
1564 run_test 8 "Run dbench with quota enabled"
1565
1566 # this check is just for test_9
1567 OST0_MIN=4900000 #4.67G
1568
1569 check_whether_skip () {
1570         local OST0_SIZE=$($LFS df $DIR | awk '/\[OST:0\]/ {print $4}')
1571         log "OST0_SIZE: $OST0_SIZE  required: $OST0_MIN"
1572         if [ $OST0_SIZE -lt $OST0_MIN ]; then
1573                 echo "WARN: OST0 has less than $OST0_MIN free, skip this test."
1574                 return 0
1575         else
1576                 return 1
1577         fi
1578 }
1579
1580 # run for fixing bug10707, it needs a big room. test for 64bit
1581 test_9() {
1582         local filesize=$((1024 * 9 / 2)) # 4.5G
1583
1584         check_whether_skip && return 0
1585
1586         setup_quota_test || error "setup quota failed with $?"
1587         trap cleanup_quota_test EXIT
1588
1589         set_ost_qtype "ug" || error "enable ost quota failed"
1590
1591         local TESTFILE="$DIR/$tdir/$tfile-0"
1592         local BLK_LIMIT=100G #100G
1593         local FILE_LIMIT=1000000
1594
1595         echo "Set block limit $BLK_LIMIT bytes to $TSTUSR.$TSTUSR"
1596
1597         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1598                 "for user: $TSTUSR"
1599         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1600                 error "set user quota failed"
1601
1602         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1603                 "for group: $TSTUSR"
1604         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR ||
1605                 error "set group quota failed"
1606
1607         quota_show_check a u $TSTUSR
1608         quota_show_check a g $TSTUSR
1609
1610         echo "Create test file"
1611         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1612         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1613
1614         log "Write the big file of 4.5G ..."
1615         $RUNAS $DD of=$TESTFILE count=$filesize ||
1616                 quota_error a $TSTUSR "write 4.5G file failure, expect success"
1617
1618         $SHOW_QUOTA_USER
1619         $SHOW_QUOTA_GROUP
1620
1621         cleanup_quota_test
1622
1623         $SHOW_QUOTA_USER
1624         $SHOW_QUOTA_GROUP
1625 }
1626 run_test 9 "Block limit larger than 4GB (b10707)"
1627
1628 test_10() {
1629         local TESTFILE=$DIR/$tdir/$tfile
1630
1631         setup_quota_test || error "setup quota failed with $?"
1632         trap cleanup_quota_test EXIT
1633
1634         # set limit to root user should fail
1635         $LFS setquota -u root -b 100G -B 500G -i 1K -I 1M $DIR &&
1636                 error "set limit for root user successfully, expect failure"
1637         $LFS setquota -g root -b 1T -B 10T -i 5K -I 100M $DIR &&
1638                 error "set limit for root group successfully, expect failure"
1639         $LFS setquota -p 0 -b 1T -B 10T -i 5K -I 100M $DIR &&
1640                 error "set limit for project 0 successfully, expect failure"
1641
1642         # root user can overrun quota
1643         set_ost_qtype "ug" || error "enable ost quota failed"
1644
1645         $LFS setquota -u $TSTUSR -b 0 -B 2M -i 0 -I 0 $DIR ||
1646                 error "set quota failed"
1647         quota_show_check b u $TSTUSR
1648
1649         $LFS setstripe $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
1650         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1651
1652         runas -u 0 -g 0 $DD of=$TESTFILE count=3 oflag=sync ||
1653                 error "write failure, expect success"
1654
1655         cleanup_quota_test
1656 }
1657 run_test 10 "Test quota for root user"
1658
1659 test_11() {
1660         local TESTFILE=$DIR/$tdir/$tfile
1661         setup_quota_test || error "setup quota failed with $?"
1662         trap cleanup_quota_test EXIT
1663
1664         set_mdt_qtype "ug" || error "enable mdt quota failed"
1665         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 1 $DIR ||
1666                 error "set quota failed"
1667
1668         touch "$TESTFILE"-0 || error "touch $TESTFILE-0 failed"
1669         touch "$TESTFILE"-1 || error "touch $TESTFILE-0 failed"
1670
1671         chown $TSTUSR.$TSTUSR "$TESTFILE"-0 || error "chown $TESTFILE-0 failed"
1672         chown $TSTUSR.$TSTUSR "$TESTFILE"-1 || error "chown $TESTFILE-1 failed"
1673
1674         $SHOW_QUOTA_USER
1675         local USED=$(getquota -u $TSTUSR global curinodes)
1676         [ $USED -ge 2 ] || error "Used inodes($USED) is less than 2"
1677
1678         cleanup_quota_test
1679 }
1680 run_test 11 "Chown/chgrp ignores quota"
1681
1682 test_12a() {
1683         [ "$OSTCOUNT" -lt "2" ] && skip "needs >= 2 OSTs"
1684
1685         local blimit=22 # 22M
1686         local blk_cnt=$((blimit - 5))
1687         local TESTFILE0="$DIR/$tdir/$tfile"-0
1688         local TESTFILE1="$DIR/$tdir/$tfile"-1
1689
1690         setup_quota_test || error "setup quota failed with $?"
1691         trap cleanup_quota_test EXIT
1692
1693         set_ost_qtype "u" || error "enable ost quota failed"
1694         quota_show_check b u $TSTUSR
1695
1696         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $DIR ||
1697                 error "set quota failed"
1698
1699         $LFS setstripe $TESTFILE0 -c 1 -i 0 || error "setstripe $TESTFILE0 failed"
1700         $LFS setstripe $TESTFILE1 -c 1 -i 1 || error "setstripe $TESTFILE1 failed"
1701         chown $TSTUSR.$TSTUSR $TESTFILE0 || error "chown $TESTFILE0 failed"
1702         chown $TSTUSR.$TSTUSR $TESTFILE1 || error "chown $TESTFILE1 failed"
1703
1704         echo "Write to ost0..."
1705         $RUNAS $DD of=$TESTFILE0 count=$blk_cnt oflag=sync ||
1706                 quota_error a $TSTUSR "dd failed"
1707
1708         echo "Write to ost1..."
1709         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync &&
1710                 quota_error a $TSTUSR "dd succeed, expect EDQUOT"
1711
1712         echo "Free space from ost0..."
1713         rm -f $TESTFILE0
1714         wait_delete_completed
1715         sync_all_data || true
1716
1717         echo "Write to ost1 after space freed from ost0..."
1718         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync ||
1719                 quota_error a $TSTUSR "rebalancing failed"
1720
1721         cleanup_quota_test
1722 }
1723 run_test 12a "Block quota rebalancing"
1724
1725 test_12b() {
1726         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
1727
1728         local ilimit=$((1024 * 2)) # 2k inodes
1729         local TESTFILE0=$DIR/$tdir/$tfile
1730         local TESTFILE1=$DIR/${tdir}-1/$tfile
1731
1732         setup_quota_test || error "setup quota failed with $?"
1733         trap cleanup_quota_test EXIT
1734
1735         $LFS mkdir -i 1 $DIR/${tdir}-1 || error "create remote dir failed"
1736         chmod 0777 $DIR/${tdir}-1
1737
1738         set_mdt_qtype "u" || error "enable mdt quota failed"
1739         quota_show_check f u $TSTUSR
1740
1741         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $ilimit $DIR ||
1742                 error "set quota failed"
1743
1744         echo "Create $ilimit files on mdt0..."
1745         $RUNAS createmany -m $TESTFILE0 $ilimit ||
1746                 quota_error u $TSTUSR "create failed, but expect success"
1747
1748         echo "Create files on mdt1..."
1749         $RUNAS createmany -m $TESTFILE1 1 &&
1750                 quota_error a $TSTUSR "create succeeded, expect EDQUOT"
1751
1752         echo "Free space from mdt0..."
1753         $RUNAS unlinkmany $TESTFILE0 $ilimit || error "unlink mdt0 files failed"
1754         wait_delete_completed
1755         sync_all_data || true
1756
1757         echo "Create files on mdt1 after space freed from mdt0..."
1758         $RUNAS createmany -m $TESTFILE1 $((ilimit / 2)) ||
1759                 quota_error a $TSTUSR "rebalancing failed"
1760
1761         $RUNAS unlinkmany $TESTFILE1 $((ilimit / 2)) ||
1762                 error "unlink mdt1 files failed"
1763         rmdir $DIR/${tdir}-1 || error "unlink remote dir failed"
1764
1765         cleanup_quota_test
1766 }
1767 run_test 12b "Inode quota rebalancing"
1768
1769 test_13(){
1770         local TESTFILE=$DIR/$tdir/$tfile
1771         # the name of lwp on ost1 name is MDT0000-lwp-OST0000
1772         local procf="ldlm.namespaces.*MDT0000-lwp-OST0000.lru_size"
1773
1774         setup_quota_test || error "setup quota failed with $?"
1775         trap cleanup_quota_test EXIT
1776
1777         set_ost_qtype "u" || error "enable ost quota failed"
1778         quota_show_check b u $TSTUSR
1779
1780         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
1781                 error "set quota failed"
1782         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
1783         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1784
1785         # clear the locks in cache first
1786         do_facet ost1 $LCTL set_param -n $procf=clear
1787         local nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1788         [ $nlock -eq 0 ] || error "$nlock cached locks"
1789
1790         # write to acquire the per-ID lock
1791         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1792                 quota_error a $TSTUSR "dd failed"
1793
1794         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1795         [ $nlock -eq 1 ] || error "lock count($nlock) isn't 1"
1796
1797         # clear quota doesn't trigger per-ID lock cancellation
1798         resetquota -u $TSTUSR
1799         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1800         [ $nlock -eq 1 ] || error "per-ID lock is lost on quota clear"
1801
1802         # clear the per-ID lock
1803         do_facet ost1 $LCTL set_param -n $procf=clear
1804         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1805         [ $nlock -eq 0 ] || error "per-ID lock isn't cleared"
1806
1807         # spare quota should be released
1808         local OSTUUID=$(ostuuid_from_index 0)
1809         local limit=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1810         local space=$(getquota -u $TSTUSR $OSTUUID curspace)
1811         [ $limit -le $space ] ||
1812                 error "spare quota isn't released, limit:$limit, space:$space"
1813
1814         cleanup_quota_test
1815 }
1816 run_test 13 "Cancel per-ID lock in the LRU list"
1817
1818 test_15(){
1819         local LIMIT=$((24 * 1024 * 1024 * 1024 * 1024)) # 24 TB
1820
1821         wait_delete_completed
1822         sync_all_data || true
1823
1824         # test for user
1825         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1826                 error "set user quota failed"
1827         local TOTAL_LIMIT=$(getquota -u $TSTUSR global bhardlimit)
1828         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1829                 error "(user) limit:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1830         resetquota -u $TSTUSR
1831
1832         # test for group
1833         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR ||
1834                 error "set group quota failed"
1835         TOTAL_LIMIT=$(getquota -g $TSTUSR global bhardlimit)
1836         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1837                 error "(group) limits:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1838         resetquota -g $TSTUSR
1839 }
1840 run_test 15 "Set over 4T block quota"
1841
1842 test_17sub() {
1843         local err_code=$1
1844         local BLKS=1    # 1M less than limit
1845         local TESTFILE=$DIR/$tdir/$tfile
1846
1847         setup_quota_test || error "setup quota failed with $?"
1848         trap cleanup_quota_test EXIT
1849
1850         # make sure the system is clean
1851         local USED=$(getquota -u $TSTUSR global curspace)
1852         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1853
1854         set_ost_qtype "ug" || error "enable ost quota failed"
1855         # make sure no granted quota on ost
1856         resetquota -u $TSTUSR
1857         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR ||
1858                 error "set quota failed"
1859
1860         quota_show_check b u $TSTUSR
1861
1862         #define OBD_FAIL_QUOTA_RECOVERABLE_ERR 0xa04
1863         lustre_fail mds 0xa04 $err_code
1864
1865         # write in background
1866         $RUNAS $DD of=$TESTFILE count=$BLKS oflag=direct &
1867         local DDPID=$!
1868
1869         sleep 2
1870         # write should be blocked and never finished
1871         if ! ps -p $DDPID  > /dev/null 2>&1; then
1872                 lustre_fail mds 0 0
1873                 quota_error u $TSTUSR "write finished incorrectly!"
1874         fi
1875
1876         lustre_fail mds 0 0
1877
1878         local count=0
1879         local timeout=30
1880         while [ true ]; do
1881                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1882                 count=$((count+1))
1883                 if [ $count -gt $timeout ]; then
1884                         quota_error u $TSTUSR "dd is not finished!"
1885                 fi
1886                 sleep 1
1887         done
1888
1889         sync; sync_all_data || true
1890
1891         USED=$(getquota -u $TSTUSR global curspace)
1892         [ $USED -ge $((BLKS * 1024)) ] || quota_error u $TSTUSR \
1893                 "Used space(${USED}K) is less than ${BLKS}M"
1894
1895         cleanup_quota_test
1896 }
1897
1898 # DQACQ return recoverable error
1899 test_17() {
1900         echo "DQACQ return -ENOLCK"
1901         #define ENOLCK  37
1902         test_17sub 37 || error "Handle -ENOLCK failed"
1903
1904         echo "DQACQ return -EAGAIN"
1905         #define EAGAIN  11
1906         test_17sub 11 || error "Handle -EAGAIN failed"
1907
1908         echo "DQACQ return -ETIMEDOUT"
1909         #define ETIMEDOUT 110
1910         test_17sub 110 || error "Handle -ETIMEDOUT failed"
1911
1912         echo "DQACQ return -ENOTCONN"
1913         #define ENOTCONN 107
1914         test_17sub 107 || error "Handle -ENOTCONN failed"
1915 }
1916
1917 run_test 17 "DQACQ return recoverable error"
1918
1919 test_18_sub () {
1920         local io_type=$1
1921         local blimit="200m" # 200M
1922         local TESTFILE="$DIR/$tdir/$tfile"
1923
1924         setup_quota_test || error "setup quota failed with $?"
1925         trap cleanup_quota_test EXIT
1926
1927         set_ost_qtype "u" || error "enable ost quota failed"
1928         log "User quota (limit: $blimit)"
1929         $LFS setquota -u $TSTUSR -b 0 -B $blimit -i 0 -I 0 $MOUNT ||
1930                 error "set quota failed"
1931         quota_show_check b u $TSTUSR
1932
1933         $LFS setstripe $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
1934         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
1935
1936         local timeout=$(sysctl -n lustre.timeout)
1937
1938         if [ $io_type = "directio" ]; then
1939                 log "Write 100M (directio) ..."
1940                 $RUNAS $DD of=$TESTFILE count=100 oflag=direct &
1941         else
1942                 log "Write 100M (buffered) ..."
1943                 $RUNAS $DD of=$TESTFILE count=100 &
1944         fi
1945         local DDPID=$!
1946
1947         replay_barrier $SINGLEMDS
1948         log "Fail mds for $((2 * timeout)) seconds"
1949         fail $SINGLEMDS $((2 * timeout))
1950
1951         local count=0
1952         if at_is_enabled; then
1953                 timeout=$(at_max_get mds)
1954         else
1955                 timeout=$(lctl get_param -n timeout)
1956         fi
1957
1958         while [ true ]; do
1959                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1960                 if [ $((++count % (2 * timeout) )) -eq 0 ]; then
1961                         log "it took $count second"
1962                 fi
1963                 sleep 1
1964         done
1965
1966         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
1967         sync
1968         cancel_lru_locks mdc
1969         cancel_lru_locks osc
1970         $SHOW_QUOTA_USER
1971
1972         local testfile_size=$(stat -c %s $TESTFILE)
1973         if [ $testfile_size -ne $((BLK_SZ * 1024 * 100)) ] ; then
1974                 quota_error u $TSTUSR "expect $((BLK_SZ * 1024 * 100))," \
1975                         "got ${testfile_size}. Verifying file failed!"
1976         fi
1977         cleanup_quota_test
1978 }
1979
1980 # test when mds does failover, the ost still could work well
1981 # this test shouldn't trigger watchdog b=14840
1982 test_18() {
1983         # Clear dmesg so watchdog is not triggered by previous
1984         # test output
1985         do_facet ost1 dmesg -c > /dev/null
1986
1987         test_18_sub normal
1988         test_18_sub directio
1989
1990         # check if watchdog is triggered
1991         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1992         local watchdog=$(awk '/[Ss]ervice thread pid/ && /was inactive/ \
1993                         { print; }' $TMP/lustre-log-${TESTNAME}.log)
1994         [ -z "$watchdog" ] || error "$watchdog"
1995         rm -f $TMP/lustre-log-${TESTNAME}.log
1996 }
1997 run_test 18 "MDS failover while writing, no watchdog triggered (b14840)"
1998
1999 test_19() {
2000         local blimit=5 # 5M
2001         local TESTFILE=$DIR/$tdir/$tfile
2002
2003         setup_quota_test || error "setup quota failed with $?"
2004         trap cleanup_quota_test EXIT
2005
2006         set_ost_qtype $QTYPE || error "enable ost quota failed"
2007
2008         # bind file to a single OST
2009         $LFS setstripe -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
2010         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2011
2012         echo "Set user quota (limit: ${blimit}M)"
2013         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2014                 error "set user quota failed"
2015         quota_show_check b u $TSTUSR
2016         echo "Update quota limits"
2017         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2018                 error "set group quota failed"
2019         quota_show_check b u $TSTUSR
2020
2021         # first wirte might be cached
2022         $RUNAS $DD of=$TESTFILE count=$((blimit + 1))
2023         cancel_lru_locks osc
2024         $SHOW_QUOTA_USER
2025         $RUNAS $DD of=$TESTFILE count=$((blimit + 1)) seek=$((blimit + 1)) &&
2026                 quota_error u $TSTUSR "Write success, expect failure"
2027         $SHOW_QUOTA_USER
2028
2029         cleanup_quota_test
2030 }
2031 run_test 19 "Updating admin limits doesn't zero operational limits(b14790)"
2032
2033 test_20() { # b15754
2034         local LSTR=(2g 1t 4k 3m) # limits strings
2035         # limits values
2036         local LVAL=($((2*1024*1024)) $((1*1024*1024*1024)) $((4*1024)) \
2037                     $((3*1024*1024)))
2038
2039         resetquota -u $TSTUSR
2040
2041         $LFS setquota -u $TSTUSR --block-softlimit ${LSTR[0]} \
2042                 $MOUNT || error "could not set quota limits"
2043         $LFS setquota -u $TSTUSR --block-hardlimit ${LSTR[1]} \
2044                                 --inode-softlimit ${LSTR[2]} \
2045                                 --inode-hardlimit ${LSTR[3]} \
2046                                 $MOUNT || error "could not set quota limits"
2047
2048         [ "$(getquota -u $TSTUSR global bsoftlimit)" = "${LVAL[0]}" ] ||
2049                 error "bsoftlimit was not set properly"
2050         [ "$(getquota -u $TSTUSR global bhardlimit)" = "${LVAL[1]}" ] ||
2051                 error "bhardlimit was not set properly"
2052         [ "$(getquota -u $TSTUSR global isoftlimit)" = "${LVAL[2]}" ] ||
2053                 error "isoftlimit was not set properly"
2054         [ "$(getquota -u $TSTUSR global ihardlimit)" = "${LVAL[3]}" ] ||
2055                 error "ihardlimit was not set properly"
2056
2057         resetquota -u $TSTUSR
2058 }
2059 run_test 20 "Test if setquota specifiers work properly (b15754)"
2060
2061 test_21_sub() {
2062         local testfile=$1
2063         local blk_number=$2
2064         local seconds=$3
2065
2066         local time=$(($(date +%s) + seconds))
2067         while [ $(date +%s) -lt $time ]; do
2068                 $RUNAS $DD of=$testfile count=$blk_number > /dev/null 2>&1
2069         done
2070 }
2071
2072 # run for fixing bug16053, setquota shouldn't fail when writing and
2073 # deleting are happening
2074 test_21() {
2075         local TESTFILE="$DIR/$tdir/$tfile"
2076         local BLIMIT=10 # 10G
2077         local ILIMIT=1000000
2078
2079         setup_quota_test || error "setup quota failed with $?"
2080         trap cleanup_quota_test EXIT
2081
2082         set_ost_qtype $QTYPE || error "Enable ost quota failed"
2083
2084         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for user: $TSTUSR"
2085         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}G -i 0 -I $ILIMIT $MOUNT ||
2086                 error "set user quota failed"
2087         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for group: $TSTUSR"
2088         $LFS setquota -g $TSTUSR -b 0 -B $BLIMIT -i 0 -I $ILIMIT $MOUNT ||
2089                 error "set group quota failed"
2090         if is_project_quota_supported; then
2091                 log "Set limit(block:${BLIMIT}G; file:$LIMIT) for " \
2092                         "project: $TSTPRJID"
2093                 $LFS setquota -p $TSTPRJID -b 0 -B $BLIMIT -i 0 -I $ILIMIT \
2094                          $MOUNT || error "set project quota failed"
2095         fi
2096
2097         # repeat writing on a 1M file
2098         test_21_sub ${TESTFILE}_1 1 30 &
2099         local DDPID1=$!
2100         # repeat writing on a 128M file
2101         test_21_sub ${TESTFILE}_2 128 30 &
2102         local DDPID2=$!
2103
2104         local time=$(($(date +%s) + 30))
2105         local i=1
2106         while [ $(date +%s) -lt $time ]; do
2107                 log "Set quota for $i times"
2108                 $LFS setquota -u $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
2109                         -I $((ILIMIT + i)) $MOUNT ||
2110                                 error "Set user quota failed"
2111                 $LFS setquota -g $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
2112                         -I $((ILIMIT + i)) $MOUNT ||
2113                                 error "Set group quota failed"
2114                 if is_project_quota_supported; then
2115                         $LFS setquota -p $TSTPRJID -b 0 -B \
2116                         "$((BLIMIT + i))G"  -i 0 -I $((ILIMIT + i)) $MOUNT ||
2117                                 error "Set project quota failed"
2118                 fi
2119                 i=$((i+1))
2120                 sleep 1
2121         done
2122
2123         local count=0
2124         while [ true ]; do
2125                 if ! ps -p ${DDPID1} > /dev/null 2>&1; then break; fi
2126                 count=$((count+1))
2127                 if [ $count -gt 60 ]; then
2128                         quota_error a $TSTUSR "dd should be finished!"
2129                 fi
2130                 sleep 1
2131         done
2132         echo "(dd_pid=$DDPID1, time=$count)successful"
2133
2134         count=0
2135         while [ true ]; do
2136                 if ! ps -p ${DDPID2} > /dev/null 2>&1; then break; fi
2137                 count=$((count+1))
2138                 if [ $count -gt 60 ]; then
2139                         quota_error a $TSTUSR "dd should be finished!"
2140                 fi
2141                 sleep 1
2142         done
2143         echo "(dd_pid=$DDPID2, time=$count)successful"
2144
2145         cleanup_quota_test
2146 }
2147 run_test 21 "Setquota while writing & deleting (b16053)"
2148
2149 # enable/disable quota enforcement permanently
2150 test_22() {
2151         echo "Set both mdt & ost quota type as ug"
2152         local qtype="ug"
2153         is_project_quota_supported && qtype=$QTYPE
2154         set_mdt_qtype $qtype || error "enable mdt quota failed"
2155         set_ost_qtype $qtype || error "enable ost quota failed"
2156
2157         echo "Restart..."
2158         stopall || error "failed to stopall (1)"
2159         mount
2160         setupall
2161
2162         echo "Verify if quota is enabled"
2163         local qtype1=$(mdt_quota_type)
2164         [ $qtype1 != $qtype ] && error "mdt quota setting is lost"
2165         qtype=$(ost_quota_type)
2166         [ $qtype1 != $qtype ] && error "ost quota setting is lost"
2167
2168         echo "Set both mdt & ost quota type as none"
2169         set_mdt_qtype "none" || error "disable mdt quota failed"
2170         set_ost_qtype "none" || error "disable ost quota failed"
2171
2172         echo "Restart..."
2173         stopall || error "failed to stopall (2)"
2174         mount
2175         setupall
2176         quota_init
2177
2178         echo "Verify if quota is disabled"
2179         qtype=$(mdt_quota_type)
2180         [ $qtype != "none" ] && error "mdt quota setting is lost"
2181         qtype=$(ost_quota_type)
2182         [ $qtype != "none" ] && error "ost quota setting is lost"
2183
2184         return 0
2185 }
2186 run_test 22 "enable/disable quota by 'lctl conf_param/set_param -P'"
2187
2188 test_23_sub() {
2189         local TESTFILE="$DIR/$tdir/$tfile"
2190         local LIMIT=$1
2191
2192         setup_quota_test || error "setup quota failed with $?"
2193         trap cleanup_quota_test EXIT
2194
2195         set_ost_qtype $QTYPE || error "Enable ost quota failed"
2196
2197         # test for user
2198         log "User quota (limit: $LIMIT MB)"
2199         $LFS setquota -u $TSTUSR -b 0 -B "$LIMIT"M -i 0 -I 0 $DIR ||
2200                 error "set quota failed"
2201         quota_show_check b u $TSTUSR
2202
2203         $LFS setstripe $TESTFILE -c 1 -i 0 || error "setstripe $TESTFILE failed"
2204         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2205
2206         log "Step1: trigger EDQUOT with O_DIRECT"
2207         log "Write half of file"
2208         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) oflag=direct ||
2209                 quota_error u $TSTUSR "(1) Write failure, expect success." \
2210                         "limit=$LIMIT"
2211         log "Write out of block quota ..."
2212         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 + 1)) seek=$((LIMIT/2)) \
2213                 oflag=direct conv=notrunc &&
2214                 quota_error u $TSTUSR "(2) Write success, expect EDQUOT." \
2215                         "limit=$LIMIT"
2216         log "Step1: done"
2217
2218         log "Step2: rewrite should succeed"
2219         $RUNAS $DD of=$TESTFILE count=1 oflag=direct conv=notrunc||
2220                 quota_error u $TSTUSR "(3) Write failure, expect success." \
2221                         "limit=$LIMIT"
2222         log "Step2: done"
2223
2224         cleanup_quota_test
2225
2226         local OST0_UUID=$(ostuuid_from_index 0)
2227         local OST0_QUOTA_USED=$(getquota -u $TSTUSR $OST0_UUID curspace)
2228         [ $OST0_QUOTA_USED -ne 0 ] &&
2229                 ($SHOW_QUOTA_USER; \
2230                 quota_error u $TSTUSR "quota isn't released")
2231         $SHOW_QUOTA_USER
2232 }
2233
2234 test_23() {
2235         [ "$ost1_FSTYPE" == zfs ] &&
2236                 skip "Overwrite in place is not guaranteed to be " \
2237                 "space neutral on ZFS"
2238
2239         local OST0_MIN=$((6 * 1024)) # 6MB, extra space for meta blocks.
2240         check_whether_skip && return 0
2241         log "run for 4MB test file"
2242         test_23_sub 4
2243
2244         OST0_MIN=$((60 * 1024)) # 60MB, extra space for meta blocks.
2245         check_whether_skip && return 0
2246         log "run for 40MB test file"
2247         test_23_sub 40
2248 }
2249 run_test 23 "Quota should be honored with directIO (b16125)"
2250
2251 test_24() {
2252         local blimit=5 # 5M
2253         local TESTFILE="$DIR/$tdir/$tfile"
2254
2255         setup_quota_test || error "setup quota failed with $?"
2256         trap cleanup_quota_test EXIT
2257
2258         set_ost_qtype $QTYPE || error "enable ost quota failed"
2259
2260         # bind file to a single OST
2261         $LFS setstripe -c 1 $TESTFILE || error "setstripe $TESTFILE failed"
2262         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2263
2264         echo "Set user quota (limit: ${blimit}M)"
2265         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT ||
2266                 error "set quota failed"
2267
2268         # overrun quota by root user
2269         runas -u 0 -g 0 $DD of=$TESTFILE count=$((blimit + 1)) ||
2270                 error "write failure, expect success"
2271         cancel_lru_locks osc
2272         sync_all_data || true
2273
2274         $SHOW_QUOTA_USER | grep '*' || error "no matching *"
2275
2276         cleanup_quota_test
2277 }
2278 run_test 24 "lfs draws an asterix when limit is reached (b16646)"
2279
2280 test_27a() { # b19612
2281         $LFS quota $TSTUSR $DIR &&
2282                 error "lfs succeeded with no type, but should have failed"
2283         $LFS setquota $TSTUSR $DIR &&
2284                 error "lfs succeeded with no type, but should have failed"
2285         return 0
2286 }
2287 run_test 27a "lfs quota/setquota should handle wrong arguments (b19612)"
2288
2289 test_27b() { # b20200
2290         $LFS setquota -u $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2291                 error "lfs setquota failed with uid argument"
2292         $LFS setquota -g $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
2293                 error "lfs stequota failed with gid argument"
2294         if is_project_quota_supported; then
2295                 $LFS setquota -p $TSTPRJID -b 1000 -B 1000 -i 1000 -I \
2296                         1000 $DIR || error \
2297                                 "lfs stequota failed with projid argument"
2298         fi
2299         $SHOW_QUOTA_USERID || error "lfs quota failed with uid argument"
2300         $SHOW_QUOTA_GROUPID || error "lfs quota failed with gid argument"
2301         if is_project_quota_supported; then
2302                 $SHOW_QUOTA_PROJID ||
2303                         error "lfs quota failed with projid argument"
2304         fi
2305         resetquota -u $TSTID
2306         resetquota -g $TSTID
2307         resetquota -p $TSTPRJID
2308         return 0
2309 }
2310 run_test 27b "lfs quota/setquota should handle user/group/project ID (b20200)"
2311
2312 test_27c() {
2313         local limit
2314
2315         $LFS setquota -u $TSTID -b 30M -B 3T $DIR ||
2316                 error "lfs setquota failed"
2317
2318         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
2319         [ $limit != "30M" ] && error "softlimit $limit isn't human-readable"
2320         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
2321         [ $limit != "3T" ] && error "hardlimit $limit isn't human-readable"
2322
2323         $LFS setquota -u $TSTID -b 1500M -B 18500G $DIR ||
2324                 error "lfs setquota for $TSTID failed"
2325
2326         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $3}')
2327         [ $limit != "1.465G" ] && error "wrong softlimit $limit"
2328         limit=$($LFS quota -u $TSTID -v -h $DIR | grep $DIR | awk '{print $4}')
2329         [ $limit != "18.07T" ] && error "wrong hardlimit $limit"
2330
2331         $LFS quota -u $TSTID -v -h $DIR | grep -q "Total allocated" ||
2332                 error "total allocated inode/block limit not printed"
2333
2334         resetquota -u $TSTUSR
2335 }
2336 run_test 27c "lfs quota should support human-readable output"
2337
2338 test_27d() {
2339         local softlimit=1.5
2340         local hardlimit=2.3
2341         local limit
2342
2343         $LFS setquota -u $TSTID -b ${softlimit}p -B ${hardlimit}P $DIR ||
2344                 error "set fraction block limit failed"
2345         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $3}')
2346         [ $limit == ${softlimit}P ] || error "get fraction softlimit failed"
2347         limit=$($LFS quota -u $TSTID -h $DIR | grep $DIR | awk '{print $4}')
2348         [ $limit == ${hardlimit}P ] || error "get fraction hardlimit failed"
2349
2350         resetquota -u $TSTUSR
2351 }
2352 run_test 27d "lfs setquota should support fraction block limit"
2353
2354 test_30() {
2355         local LIMIT=4 # 4MB
2356         local TESTFILE="$DIR/$tdir/$tfile"
2357         local GRACE=10
2358
2359         setup_quota_test || error "setup quota failed with $?"
2360         trap cleanup_quota_test EXIT
2361
2362         set_ost_qtype "u" || error "enable ost quota failed"
2363
2364         $LFS setstripe $TESTFILE -i 0 -c 1 || error "setstripe $TESTFILE failed"
2365         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
2366
2367         $LFS setquota -t -u --block-grace $GRACE --inode-grace \
2368                 $MAX_IQ_TIME $DIR || error "set grace time failed"
2369         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR ||
2370                 error "set quota failed"
2371         $RUNAS $DD of=$TESTFILE count=$((LIMIT * 2)) || true
2372         cancel_lru_locks osc
2373         sleep $GRACE
2374         $LFS setquota -u $TSTUSR -B 0 $DIR || error "clear quota failed"
2375         # over-quota flag has not yet settled since we do not trigger async
2376         # events based on grace time period expiration
2377         $SHOW_QUOTA_USER
2378         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 || true
2379         cancel_lru_locks osc
2380         # now over-quota flag should be settled and further writes should fail
2381         $SHOW_QUOTA_USER
2382         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=4 &&
2383                 error "grace times were reset"
2384         # cleanup
2385         cleanup_quota_test
2386         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
2387                 $MAX_IQ_TIME $DIR || error "restore grace time failed"
2388 }
2389 run_test 30 "Hard limit updates should not reset grace times"
2390
2391 # basic usage tracking for user & group
2392 test_33() {
2393         local INODES=10 # 10 files
2394         local BLK_CNT=2 # of 2M each
2395         local TOTAL_BLKS=$((INODES * BLK_CNT * 1024))
2396
2397         setup_quota_test || error "setup quota failed with $?"
2398         trap cleanup_quota_test EXIT
2399
2400         # make sure the system is clean
2401         local USED=$(getquota -u $TSTID global curspace)
2402         [ $USED -ne 0 ] &&
2403                 error "Used space ($USED) for user $TSTID isn't 0."
2404         USED=$(getquota -g $TSTID global curspace)
2405         [ $USED -ne 0 ] &&
2406                 error "Used space ($USED) for group $TSTID isn't 0."
2407         if is_project_quota_supported; then
2408                 USED=$(getquota -p $TSTPRJID global curspace)
2409                 [ $USED -ne 0 ] && error \
2410                         "Used space ($USED) for project $TSTPRJID isn't 0."
2411         fi
2412
2413         echo "Write files..."
2414         for i in $(seq 0 $INODES); do
2415                 $RUNAS $DD of=$DIR/$tdir/$tfile-$i count=$BLK_CNT 2>/dev/null ||
2416                         error "write failed"
2417                         is_project_quota_supported &&
2418                                 change_project -p $TSTPRJID $DIR/$tdir/$tfile-$i
2419                 echo "Iteration $i/$INODES completed"
2420         done
2421         cancel_lru_locks osc
2422
2423         echo "Wait for setattr on objects finished..."
2424         wait_delete_completed
2425
2426         sync; sync_all_data || true
2427
2428         echo "Verify disk usage after write"
2429         USED=$(getquota -u $TSTID global curspace)
2430         [ $USED -lt $TOTAL_BLKS ] &&
2431                 error "Used space for user $TSTID:$USED, expected:$TOTAL_BLKS"
2432         USED=$(getquota -g $TSTID global curspace)
2433         [ $USED -lt $TOTAL_BLKS ] &&
2434                 error "Used space for group $TSTID:$USED, expected:$TOTAL_BLKS"
2435         if is_project_quota_supported; then
2436                 USED=$(getquota -p $TSTPRJID global curspace)
2437                 [ $USED -lt $TOTAL_BLKS ] && error \
2438                         "Used space for project $TSTPRJID:$USED, expected:$TOTAL_BLKS"
2439         fi
2440
2441         echo "Verify inode usage after write"
2442         USED=$(getquota -u $TSTID global curinodes)
2443         [ $USED -lt $INODES ] &&
2444                 error "Used inode for user $TSTID is $USED, expected $INODES"
2445         USED=$(getquota -g $TSTID global curinodes)
2446         [ $USED -lt $INODES ] &&
2447                 error "Used inode for group $TSTID is $USED, expected $INODES"
2448         if is_project_quota_supported; then
2449                 USED=$(getquota -p $TSTPRJID global curinodes)
2450                 [ $USED -lt $INODES ] && error \
2451                         "Used inode for project $TSTPRJID is $USED, expected $INODES"
2452         fi
2453
2454         cleanup_quota_test
2455
2456         echo "Verify disk usage after delete"
2457         USED=$(getquota -u $TSTID global curspace)
2458         [ $USED -eq 0 ] || error "Used space for user $TSTID isn't 0. $USED"
2459         USED=$(getquota -u $TSTID global curinodes)
2460         [ $USED -eq 0 ] || error "Used inodes for user $TSTID isn't 0. $USED"
2461         USED=$(getquota -g $TSTID global curspace)
2462         [ $USED -eq 0 ] || error "Used space for group $TSTID isn't 0. $USED"
2463         USED=$(getquota -g $TSTID global curinodes)
2464         [ $USED -eq 0 ] || error "Used inodes for group $TSTID isn't 0. $USED"
2465         if is_project_quota_supported; then
2466                 USED=$(getquota -p $TSTPRJID global curspace)
2467                 [ $USED -eq 0 ] ||
2468                         error "Used space for project $TSTPRJID isn't 0. $USED"
2469                 USED=$(getquota -p $TSTPRJID global curinodes)
2470                 [ $USED -eq 0 ] ||
2471                         error "Used inodes for project $TSTPRJID isn't 0. $USED"
2472         fi
2473 }
2474 run_test 33 "Basic usage tracking for user & group & project"
2475
2476 # usage transfer test for user & group & project
2477 test_34() {
2478         local BLK_CNT=2 # 2MB
2479         local project_supported="no"
2480
2481         is_project_quota_supported && project_supported="yes"
2482         setup_quota_test || error "setup quota failed with $?"
2483         trap cleanup_quota_test EXIT
2484
2485         # make sure the system is clean
2486         local USED=$(getquota -u $TSTID global curspace)
2487         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID isn't 0."
2488         USED=$(getquota -g $TSTID global curspace)
2489         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTID isn't 0."
2490
2491         local USED=$(getquota -u $TSTID2 global curspace)
2492         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID2 isn't 0."
2493         if [ $project_supported == "yes" ]; then
2494                 USED=$(getquota -p $TSTPRJID global curspace)
2495                 [ $USED -ne 0 ] && error \
2496                         "Used space ($USED) for Project $TSTPRJID isn't 0."
2497         fi
2498
2499         echo "Write file..."
2500         $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
2501                 error "write failed"
2502         cancel_lru_locks osc
2503         sync; sync_all_data || true
2504
2505         echo "chown the file to user $TSTID"
2506         chown $TSTID $DIR/$tdir/$tfile || error "chown failed"
2507
2508         echo "Wait for setattr on objects finished..."
2509         wait_delete_completed
2510
2511         BLK_CNT=$((BLK_CNT * 1024))
2512
2513         echo "Verify disk usage for user $TSTID"
2514         USED=$(getquota -u $TSTID global curspace)
2515         [ $USED -lt $BLK_CNT ] &&
2516                 error "Used space for user $TSTID is ${USED}, expected $BLK_CNT"
2517         USED=$(getquota -u $TSTID global curinodes)
2518         [ $USED -ne 1 ] &&
2519                 error "Used inodes for user $TSTID is $USED, expected 1"
2520
2521         echo "chgrp the file to group $TSTID"
2522         chgrp $TSTID $DIR/$tdir/$tfile || error "chgrp failed"
2523
2524         echo "Wait for setattr on objects finished..."
2525         wait_delete_completed
2526
2527         echo "Verify disk usage for group $TSTID"
2528         USED=$(getquota -g $TSTID global curspace)
2529         [ $USED -ge $BLK_CNT ] ||
2530                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
2531         USED=$(getquota -g $TSTID global curinodes)
2532         [ $USED -eq 1 ] ||
2533                 error "Used inodes for group $TSTID is $USED, expected 1"
2534
2535         # chown won't change the ost object group. LU-4345 */
2536         echo "chown the file to user $TSTID2"
2537         chown $TSTID2 $DIR/$tdir/$tfile || error "chown to $TSTID2 failed"
2538
2539         echo "Wait for setattr on objects finished..."
2540         wait_delete_completed
2541
2542         echo "change_project project id to $TSTPRJID"
2543         [ $project_supported == "yes" ] &&
2544                 change_project -p $TSTPRJID $DIR/$tdir/$tfile
2545         echo "Wait for setattr on objects finished..."
2546         wait_delete_completed
2547
2548         echo "Verify disk usage for user $TSTID2/$TSTID and group $TSTID"
2549         USED=$(getquota -u $TSTID2 global curspace)
2550         [ $USED -lt $BLK_CNT ] &&
2551                 error "Used space for user $TSTID2 is $USED, expected $BLK_CNT"
2552         USED=$(getquota -u $TSTID global curspace)
2553         [ $USED -ne 0 ] &&
2554                 error "Used space for user $TSTID is $USED, expected 0"
2555         USED=$(getquota -g $TSTID global curspace)
2556         [ $USED -lt $BLK_CNT ] &&
2557                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
2558         if [ $project_supported == "yes" ]; then
2559                 USED=$(getquota -p $TSTPRJID global curspace)
2560                 [ $USED -lt $BLK_CNT ] && error \
2561                         "Used space for group $TSTPRJID is $USED, expected $BLK_CNT"
2562         fi
2563
2564         cleanup_quota_test
2565 }
2566 run_test 34 "Usage transfer for user & group & project"
2567
2568 # usage is still accessible across restart
2569 test_35() {
2570         local BLK_CNT=2 # 2 MB
2571
2572         setup_quota_test || error "setup quota failed with $?"
2573         trap cleanup_quota_test EXIT
2574
2575         echo "Write file..."
2576         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
2577                 error "write failed"
2578         is_project_quota_supported &&
2579                 change_project -p $TSTPRJID $DIR/$tdir/$tfile
2580         cancel_lru_locks osc
2581
2582         echo "Wait for setattr on objects finished..."
2583         wait_delete_completed
2584
2585         sync; sync_all_data || true
2586
2587         echo "Save disk usage before restart"
2588         local ORIG_USR_SPACE=$(getquota -u $TSTID global curspace)
2589         [ $ORIG_USR_SPACE -eq 0 ] &&
2590                 error "Used space for user $TSTID is 0, expected ${BLK_CNT}M"
2591         local ORIG_USR_INODES=$(getquota -u $TSTID global curinodes)
2592         [ $ORIG_USR_INODES -eq 0 ] &&
2593                 error "Used inodes for user $TSTID is 0, expected 1"
2594         echo "User $TSTID: ${ORIG_USR_SPACE}KB $ORIG_USR_INODES inodes"
2595         local ORIG_GRP_SPACE=$(getquota -g $TSTID global curspace)
2596         [ $ORIG_GRP_SPACE -eq 0 ] &&
2597                 error "Used space for group $TSTID is 0, expected ${BLK_CNT}M"
2598         local ORIG_GRP_INODES=$(getquota -g $TSTID global curinodes)
2599         [ $ORIG_GRP_INODES -eq 0 ] &&
2600                 error "Used inodes for group $TSTID is 0, expected 1"
2601         echo "Group $TSTID: ${ORIG_GRP_SPACE}KB $ORIG_GRP_INODES inodes"
2602
2603         if is_project_quota_supported; then
2604                 local ORIG_PRJ_SPACE=$(getquota -p $TSTPRJID global curspace)
2605                 [ $ORIG_PRJ_SPACE -eq 0 ] && error \
2606                         "Used space for project $TSTPRJID is 0, expected ${BLK_CNT}M"
2607                 local ORIG_PRJ_INODES=$(getquota -p $TSTPRJID global curinodes)
2608                 [ $ORIG_PRJ_INODES -eq 0 ] && error \
2609                         "Used inodes for project $TSTPRJID is 0, expected 1"
2610                 echo "Project $TSTPRJID: ${ORIG_PRJ_SPACE}KB $ORIG_PRJ_INODES inodes"
2611         fi
2612
2613         log "Restart..."
2614         stopall
2615         setupall
2616         quota_init
2617
2618         echo "Verify disk usage after restart"
2619         local USED=$(getquota -u $TSTID global curspace)
2620         [ $USED -eq $ORIG_USR_SPACE ] ||
2621                 error "Used space for user $TSTID changed from " \
2622                         "$ORIG_USR_SPACE to $USED"
2623         USED=$(getquota -u $TSTID global curinodes)
2624         [ $USED -eq $ORIG_USR_INODES ] ||
2625                 error "Used inodes for user $TSTID changed from " \
2626                         "$ORIG_USR_INODES to $USED"
2627         USED=$(getquota -g $TSTID global curspace)
2628         [ $USED -eq $ORIG_GRP_SPACE ] ||
2629                 error "Used space for group $TSTID changed from " \
2630                         "$ORIG_GRP_SPACE to $USED"
2631         USED=$(getquota -g $TSTID global curinodes)
2632         [ $USED -eq $ORIG_GRP_INODES ] ||
2633                 error "Used inodes for group $TSTID changed from " \
2634                         "$ORIG_GRP_INODES to $USED"
2635         if [ $project_supported == "yes" ]; then
2636                 USED=$(getquota -p $TSTPRJID global curinodes)
2637                 [ $USED -eq $ORIG_PRJ_INODES ] ||
2638                         error "Used inodes for project $TSTPRJID " \
2639                                 "changed from $ORIG_PRJ_INODES to $USED"
2640                 USED=$(getquota -p $TSTPRJID global curspace)
2641                 [ $USED -eq $ORIG_PRJ_SPACE ] ||
2642                         error "Used space for project $TSTPRJID "\
2643                                 "changed from $ORIG_PRJ_SPACE to $USED"
2644         fi
2645
2646         # check if the vfs_dq_init() is called before writing
2647         echo "Append to the same file..."
2648         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT seek=1 2>/dev/null ||
2649                 error "write failed"
2650         cancel_lru_locks osc
2651         sync; sync_all_data || true
2652
2653         echo "Verify space usage is increased"
2654         USED=$(getquota -u $TSTID global curspace)
2655         [ $USED -gt $ORIG_USR_SPACE ] ||
2656                 error "Used space for user $TSTID isn't increased" \
2657                         "orig:$ORIG_USR_SPACE, now:$USED"
2658         USED=$(getquota -g $TSTID global curspace)
2659         [ $USED -gt $ORIG_GRP_SPACE ] ||
2660                 error "Used space for group $TSTID isn't increased" \
2661                         "orig:$ORIG_GRP_SPACE, now:$USED"
2662         if [ $project_supported == "yes" ]; then
2663                 USED=$(getquota -p $TSTPRJID global curspace)
2664                 [ $USED -gt $ORIG_PRJ_SPACE ] ||
2665                         error "Used space for project $TSTPRJID isn't " \
2666                                 "increased orig:$ORIG_PRJ_SPACE, now:$USED"
2667         fi
2668
2669         cleanup_quota_test
2670 }
2671 run_test 35 "Usage is still accessible across reboot"
2672
2673 # chown/chgrp to the file created with MDS_OPEN_DELAY_CREATE
2674 # LU-5006
2675 test_37() {
2676         [ "$MDS1_VERSION" -lt $(version_code 2.6.93) ] &&
2677                 skip "Old server doesn't have LU-5006 fix."
2678
2679         setup_quota_test || error "setup quota failed with $?"
2680         trap cleanup_quota_test EXIT
2681
2682         # make sure the system is clean
2683         local USED=$(getquota -u $TSTID global curspace)
2684         [ $USED -ne 0 ] &&
2685                 error "Used space ($USED) for user $TSTID isn't 0."
2686
2687         # create file with MDS_OPEN_DELAY_CREATE flag
2688         $LFS setstripe -c 1 -i 0 $DIR/$tdir/$tfile ||
2689                 error "Create file failed"
2690         # write to file
2691         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 conv=notrunc \
2692                 oflag=sync || error "Write file failed"
2693         # chown to the file
2694         chown $TSTID $DIR/$tdir/$tfile || error "Chown to file failed"
2695
2696         # wait for setattr on objects finished..."
2697         wait_delete_completed
2698
2699         USED=$(getquota -u $TSTID global curspace)
2700         [ $USED -ne 0 ] || quota_error u $TSTUSR "Used space is 0"
2701
2702         cleanup_quota_test
2703 }
2704 run_test 37 "Quota accounted properly for file created by 'lfs setstripe'"
2705
2706 # LU-8801
2707 test_38() {
2708         [ "$MDS1_VERSION" -lt $(version_code 2.8.60) ] &&
2709                 skip "Old server doesn't have LU-8801 fix."
2710
2711         [ "$UID" != 0 ] && skip_env "must run as root" && return
2712
2713         setup_quota_test || error "setup quota failed with $?"
2714         trap cleanup_quota_test EXIT
2715
2716         # make sure the system is clean
2717         local USED=$(getquota -u $TSTID global curspace)
2718         [ $USED -ne 0 ] &&
2719                 error "Used space ($USED) for user $TSTID isn't 0."
2720         USED=$(getquota -u $TSTID2 global curspace)
2721         [ $USED -ne 0 ] &&
2722                 error "Used space ($USED) for user $TSTID2 isn't 0."
2723
2724         local TESTFILE="$DIR/$tdir/$tfile"
2725         local file_cnt=10000
2726
2727         # Generate id entries in accounting file
2728         echo "Create $file_cnt files..."
2729         for i in `seq $file_cnt`; do
2730                 touch $TESTFILE-$i
2731                 chown $((file_cnt - i)):$((file_cnt - i)) $TESTFILE-$i ||
2732                         error "failed to chown $TESTFILE-$i"
2733         done
2734         cancel_lru_locks osc
2735         sync; sync_all_data || true
2736
2737         local procf="osd-$mds1_FSTYPE.$FSNAME-MDT0000"
2738         procf=${procf}.quota_slave.acct_user
2739         local accnt_cnt
2740
2741         acct_cnt=$(do_facet mds1 $LCTL get_param $procf | grep "id:" | \
2742                    awk '{if ($3 < 10000) {print $3}}' | wc -l)
2743         echo "Found $acct_cnt id entries"
2744
2745         [ $file_cnt -eq $acct_cnt ] || {
2746                 do_facet mds1 $LCTL get_param $procf
2747                 error "skipped id entries"
2748         }
2749
2750         cleanup_quota_test
2751 }
2752 run_test 38 "Quota accounting iterator doesn't skip id entries"
2753
2754 test_39() {
2755         local TESTFILE="$DIR/$tdir/project"
2756         ! is_project_quota_supported &&
2757                 skip "Project quota is not supported"
2758
2759         setup_quota_test || error "setup quota failed with $?"
2760
2761         touch $TESTFILE
2762         projectid=$(lfs project $TESTFILE | awk '{print $1}')
2763         [ $projectid -ne 0 ] &&
2764                 error "Project id should be 0 not $projectid"
2765         change_project -p 1024 $TESTFILE
2766         projectid=$(lfs project $TESTFILE | awk '{print $1}')
2767         [ $projectid -ne 1024 ] &&
2768                 error "Project id should be 1024 not $projectid"
2769
2770         stopall || error "failed to stopall (1)"
2771         mount
2772         setupall
2773         projectid=$(lfs project $TESTFILE | awk '{print $1}')
2774         [ $projectid -ne 1024 ] &&
2775                 error "Project id should be 1024 not $projectid"
2776
2777         cleanup_quota_test
2778 }
2779 run_test 39 "Project ID interface works correctly"
2780
2781 test_40a() {
2782         ! is_project_quota_supported &&
2783                 skip "Project quota is not supported"
2784         local dir1="$DIR/$tdir/dir1"
2785         local dir2="$DIR/$tdir/dir2"
2786
2787         setup_quota_test || error "setup quota failed with $?"
2788
2789         mkdir -p $dir1 $dir2
2790         change_project -sp 1 $dir1 && touch $dir1/1
2791         change_project -sp 2 $dir2
2792
2793         ln $dir1/1 $dir2/1_link &&
2794                 error "Hard link across different project quota should fail"
2795         rm -rf $dir1 $dir2
2796
2797         cleanup_quota_test
2798 }
2799 run_test 40a "Hard link across different project ID"
2800
2801 test_40b() {
2802         ! is_project_quota_supported &&
2803                 skip "Project quota is not supported"
2804         local dir1="$DIR/$tdir/dir1"
2805         local dir2="$DIR/$tdir/dir2"
2806
2807         setup_quota_test || error "setup quota failed with $?"
2808         mkdir -p $dir1 $dir2
2809         change_project -sp 1 $dir1 && touch $dir1/1
2810         change_project -sp 2 $dir2
2811
2812         mv $dir1/1 $dir2/2 || error "mv failed $?"
2813         local projid=$(lfs project $dir2/2 | awk '{print $1}')
2814         if [ "$projid" != "2" ]; then
2815                 error "project id expected 2 not $projid"
2816         fi
2817         rm -rf $dir1 $dir2
2818         cleanup_quota_test
2819 }
2820 run_test 40b "Mv across different project ID"
2821
2822 test_40c() {
2823         [ "$MDSCOUNT" -lt "2" ] && skip "needs >= 2 MDTs"
2824                 ! is_project_quota_supported &&
2825                         skip "Project quota is not supported"
2826
2827         setup_quota_test || error "setup quota failed with $?"
2828         local dir="$DIR/$tdir/dir"
2829
2830         mkdir -p $dir && change_project -sp 1 $dir
2831         $LFS mkdir -i 1 $dir/remote_dir || error "create remote dir failed"
2832         local projid=$(lfs project -d $dir/remote_dir | awk '{print $1}')
2833         [ "$projid" != "1" ] && error "projid id expected 1 not $projid"
2834         touch $dir/remote_dir/file
2835         #verify inherit works file for remote dir.
2836         local projid=$(lfs project -d $dir/remote_dir/file | awk '{print $1}')
2837         [ "$projid" != "1" ] &&
2838                 error "file under remote dir expected 1 not $projid"
2839
2840         #Agent inode should be ignored for project quota
2841         USED=$(getquota -p 1 global curinodes)
2842         [ "$USED" != "3" ] &&
2843                 error "file count expected 3 got $USED"
2844
2845         rm -rf $dir
2846         cleanup_quota_test
2847         return 0
2848 }
2849 run_test 40c "Remote child Dir inherit project quota properly"
2850
2851 test_50() {
2852         ! is_project_quota_supported &&
2853                 skip "Project quota is not supported"
2854
2855         setup_quota_test || error "setup quota failed with $?"
2856         local dir1="$DIR/$tdir/dir1"
2857         local dir2="$DIR/$tdir/dir2"
2858
2859         mkdir -p $dir1 && change_project -sp 1 $dir1
2860         mkdir -p $dir2 && change_project -sp 2 $dir2
2861         for num in $(seq 1 10); do
2862                 touch $dir1/file_$num $dir2/file_$num
2863                 ln -s $dir1/file_$num $dir1/file_$num"_link"
2864                 ln -s $dir2/file_$num $dir2/file_$num"_link"
2865         done
2866
2867         count=$($LFS find --projid 1 $DIR | wc -l)
2868         [ "$count" != 21 ] && error "expected 21 but got $count"
2869
2870         # 1(projid 0 dir) + 1(projid 2 dir) + 20(projid 2 files)
2871         count=$($LFS find ! --projid 1 $DIR/$tdir | wc -l)
2872         [ "$count" != 22 ] && error "expected 22 but got $count"
2873
2874         rm -rf $dir1 $dir2
2875         cleanup_quota_test
2876 }
2877 run_test 50 "Test if lfs find --projid works"
2878
2879 test_51() {
2880         ! is_project_quota_supported &&
2881                 skip "Project quota is not supported"
2882         setup_quota_test || error "setup quota failed with $?"
2883         local dir="$DIR/$tdir/dir"
2884
2885         mkdir $dir && change_project -sp 1 $dir
2886         local used=$(getquota -p 1 global curinodes)
2887         [ $used != "1" ] && error "expected 1 got $used"
2888
2889         touch $dir/1
2890         touch $dir/2
2891         cp $dir/2 $dir/3
2892         used=$(getquota -p 1 global curinodes)
2893         [ $used != "4" ] && error "expected 4 got $used"
2894
2895         $DD if=/dev/zero of=$DIR/$tdir/6 bs=1M count=1
2896         #try cp to dir
2897         cp $DIR/$tdir/6 $dir/6
2898         used=$(getquota -p 1 global curinodes)
2899         [ $used != "5" ] && error "expected 5 got $used"
2900
2901         #try mv to dir
2902         mv $DIR/$tdir/6 $dir/7
2903         used=$(getquota -p 1 global curinodes)
2904         [ $used != "6" ] && error "expected 6 got $used"
2905
2906         rm -rf $dir
2907         cleanup_quota_test
2908 }
2909 run_test 51 "Test project accounting with mv/cp"
2910
2911 test_52() {
2912         ! is_project_quota_supported &&
2913                 skip "Project quota is not supported"
2914         setup_quota_test || error "setup quota failed with $?"
2915         local dir="$DIR/$tdir/dir"
2916         mkdir $dir && change_project -sp 1 $dir
2917
2918         touch $DIR/$tdir/file
2919         #Try renaming a file into the project.  This should fail.
2920         for num in $(seq 1 2000); do
2921                 mrename $DIR/$tdir/file $dir/file >&/dev/null &&
2922                         error "rename should fail"
2923         done
2924         rm -rf $dir
2925         cleanup_quota_test
2926 }
2927 run_test 52 "Rename across different project ID"
2928
2929 test_53() {
2930         ! is_project_quota_supported &&
2931                 skip "Project quota is not supported"
2932         setup_quota_test || error "setup quota failed with $?"
2933         local dir="$DIR/$tdir/dir"
2934         mkdir $dir && change_project -s $dir
2935         lfs project -d $dir | grep P || error "inherit attribute should be set"
2936
2937         change_project -C $dir
2938         lfs project -d $dir | grep P &&
2939                 error "inherit attribute should be cleared"
2940
2941         rm -rf $dir
2942         cleanup_quota_test
2943 }
2944 run_test 53 "Project inherit attribute could be cleared"
2945
2946 test_54() {
2947         ! is_project_quota_supported &&
2948                 skip "Project quota is not supported"
2949         setup_quota_test || error "setup quota failed with $?"
2950         trap cleanup_quota_test EXIT
2951         local testfile="$DIR/$tdir/$tfile-0"
2952
2953         #set project ID/inherit attribute
2954         change_project -sp $TSTPRJID $DIR/$tdir
2955         $RUNAS createmany -m ${testfile} 100 ||
2956                 error "create many files failed"
2957
2958         local proj_count=$(lfs project -r $DIR/$tdir | wc -l)
2959         # one more count for directory itself */
2960         ((proj_count++))
2961
2962         #check project
2963         local proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
2964         [ $proj_count1 -eq 0 ] || error "c1: expected 0 got $proj_count1"
2965
2966         proj_count1=$(lfs project -rcp $((TSTPRJID+1)) $DIR/$tdir | wc -l)
2967         [ $proj_count1 -eq $proj_count ] ||
2968                         error "c2: expected $proj_count got $proj_count1"
2969
2970         #clear project but with kept projid
2971         change_project -rCk $DIR/$tdir
2972         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
2973         [ $proj_count1 -eq 1 ] ||
2974                         error "c3: expected 1 got $proj_count1"
2975
2976         #verify projid untouched.
2977         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
2978         ((proj_count1++))
2979         [ $proj_count1 -eq $proj_count ] ||
2980                         error "c4: expected $proj_count got $proj_count1"
2981
2982         # test -0 option
2983         lfs project $DIR/$tdir -cr -0 | xargs -0 lfs project -s
2984         proj_count1=$(lfs project -rcp $TSTPRJID $DIR/$tdir | wc -l)
2985         [ $proj_count1 -eq 0 ] || error "c5: expected 0 got $proj_count1"
2986
2987         #this time clear all
2988         change_project -rC $DIR/$tdir
2989         proj_count1=$(lfs project -r $DIR/$tdir | grep -c $TSTPRJID)
2990         [ $proj_count1 -eq 0 ] ||
2991                         error "c6: expected 0 got $proj_count1"
2992         #cleanup
2993         unlinkmany ${testfile} 100 ||
2994                 error "unlink many files failed"
2995
2996         cleanup_quota_test
2997 }
2998 run_test 54 "basic lfs project interface test"
2999
3000 test_55() {
3001         [ "$MDS1_VERSION" -lt $(version_code 2.10.58) ] &&
3002                 skip "Not supported before 2.10.58."
3003         setup_quota_test || error "setup quota failed with $?"
3004
3005         set_ost_qtype $QTYPE || error "enable ost quota failed"
3006         quota_init
3007
3008         #add second group to TSTUSR
3009         usermod -G $TSTUSR,$TSTUSR2 $TSTUSR
3010
3011         #prepare test file
3012         $RUNAS dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1024 count=100000 ||
3013         error "failed to dd"
3014
3015         cancel_lru_locks osc
3016         sync; sync_all_data || true
3017
3018         $LFS setquota -g $TSTUSR2 -b 0 -B 50M $DIR ||
3019         error "failed to setquota on group $TSTUSR2"
3020
3021         $LFS quota -v -g $TSTUSR2 $DIR
3022
3023         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile &&
3024         error "chgrp should failed with -EDQUOT"
3025
3026         USED=$(getquota -g $TSTUSR2 global curspace)
3027         echo "$USED"
3028
3029         $LFS setquota -g $TSTUSR2 -b 0 -B 300M $DIR ||
3030         error "failed to setquota on group $TSTUSR2"
3031
3032         $LFS quota -v -g $TSTUSR2 $DIR
3033
3034         runas -u $TSTUSR -g $TSTUSR2 chgrp $TSTUSR2 $DIR/$tdir/$tfile ||
3035         error "chgrp should succeed"
3036
3037         $LFS quota -v -g $TSTUSR2 $DIR
3038
3039         cleanup_quota_test
3040 }
3041 run_test 55 "Chgrp should be affected by group quota"
3042
3043 test_56() {
3044         setup_quota_test || error "setup quota failed with $?"
3045
3046         set_ost_qtype $QTYPE || error "enable ost quota failed"
3047         quota_init
3048
3049         $LFS setquota -t -u -b 10 -i 10 $DIR ||
3050                 erro "failed to set grace time for usr quota"
3051         grace_time=$($LFS quota -t -u $DIR | grep "Block grace time:" |
3052                      awk '{print $4 $8}')
3053         if [ "x$grace_time" != "x10s;10s" ]; then
3054                 $LFS quota -t -u $DIR
3055                 error "expected grace time: 10s;10s, got:$grace_time"
3056         fi
3057
3058         cleanup_quota_test
3059 }
3060 run_test 56 "lfs quota -t should work well"
3061
3062 test_57() {
3063         setup_quota_test || error "setup quota failed with $?"
3064
3065         local dir="$DIR/$tdir/dir"
3066         mkdir -p $dir
3067         mkfifo $dir/pipe
3068         #try to change pipe file should not hang and return failure
3069         wait_update_facet client "$LFS project -sp 1 $dir/pipe 2>&1 |
3070                 awk -F ':' '{ print \\\$2 }'" \
3071                         " unable to get xattr for fifo '$dir/pipe'" || return 1
3072         #command can process further if it hit some errors
3073         touch $dir/aaa $dir/bbb
3074         mkdir $dir/subdir -p
3075         touch $dir/subdir/aaa $dir/subdir/bbb
3076         #create one invalid link file
3077         ln -s $dir/not_exist_file $dir/ccc
3078         local cnt=$(lfs project -r $dir 2>/dev/null | wc -l)
3079         [ $cnt -eq 5 ] || error "expected 5 got $cnt"
3080
3081         cleanup_quota_test
3082 }
3083 run_test 57 "lfs project could tolerate errors"
3084
3085 test_59() {
3086         [ "$mds1_FSTYPE" != ldiskfs ] &&
3087                 skip "ldiskfs only test"
3088         disable_project_quota
3089         setup_quota_test || error "setup quota failed with $?"
3090         quota_init
3091
3092         local testfile="$DIR/$tdir/$tfile-0"
3093         #make sure it did not crash kernel
3094         touch $testfile && lfs project -sp 1 $testfile
3095
3096         enable_project_quota
3097         cleanup_quota_test
3098 }
3099 run_test 59 "lfs project dosen't crash kernel with project disabled"
3100
3101 test_60() {
3102         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
3103                 skip "Needs MDS version 2.11.53 or later."
3104         setup_quota_test || error "setup quota failed with $?"
3105         trap cleanup_quota_test EXIT
3106
3107         local testfile=$DIR/$tdir/$tfile
3108         local limit=100
3109
3110         set_mdt_qtype "ug" || error "enable mdt quota failed"
3111
3112         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $limit $DIR ||
3113                 error "set quota failed"
3114         quota_show_check a g $TSTUSR
3115
3116         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $DIR/$tdir failed"
3117         chmod g+s $DIR/$tdir || error "chmod g+s failed"
3118         $RUNAS createmany -m ${testfile} $((limit-1)) ||
3119                 error "create many files failed"
3120
3121         $RUNAS touch $DIR/$tdir/foo && error "regular user should fail"
3122
3123         # root user can overrun quota
3124         runas -u 0 -g 0 touch $DIR/$tdir/foo ||
3125                 error "root user should succeed"
3126
3127         cleanup_quota_test
3128 }
3129 run_test 60 "Test quota for root with setgid"
3130
3131 # test default quota
3132 test_default_quota() {
3133         [ "$MDS1_VERSION" -lt $(version_code 2.11.51) ] &&
3134                 skip "Not supported before 2.11.51."
3135
3136         local qtype=$1
3137         local qpool=$2
3138         local qid=$TSTUSR
3139         local qprjid=$TSTPRJID
3140         local qdtype="-U"
3141         local qs="-b"
3142         local qh="-B"
3143         local LIMIT=20480 #20M disk space
3144         local TESTFILE="$DIR/$tdir/$tfile-0"
3145
3146         [ $qtype == "-p" ] && ! is_project_quota_supported &&
3147                 echo "Project quota is not supported" && return 0
3148
3149         [ $qtype == "-u" ] && qdtype="-U"
3150         [ $qtype == "-g" ] && qdtype="-G"
3151         [ $qtype == "-p" ] && {
3152                 qdtype="-P"
3153                 qid=$qprjid
3154         }
3155
3156         [ $qpool == "meta" ] && {
3157                 LIMIT=10240 #10K inodes
3158                 qs="-i"
3159                 qh="-I"
3160         }
3161
3162         setup_quota_test || error "setup quota failed with $?"
3163         trap cleanup_quota_test EXIT
3164
3165         quota_init
3166
3167         # enable mdt/ost quota
3168         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
3169         set_ost_qtype $QTYPE || error "enable ost quota failed"
3170
3171         log "set to use default quota"
3172         $LFS setquota $qtype $qid -d $DIR ||
3173                 error "set $qid to use default quota failed"
3174
3175         log "set default quota"
3176         $LFS setquota $qdtype $qs ${LIMIT} $qh ${LIMIT} $DIR ||
3177                 error "set $qid default quota failed"
3178
3179         log "get default quota"
3180         $LFS quota $qdtype $DIR || error "get default quota failed"
3181
3182         if [ $qpool == "data" ]; then
3183                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
3184                                                         awk '{print $2}')
3185                 [ $SLIMIT -eq $LIMIT ] ||
3186                         error "the returned default quota is wrong"
3187         else
3188                 local SLIMIT=$($LFS quota $qdtype $DIR | grep "$MOUNT" | \
3189                                                         awk '{print $5}')
3190                 [ $SLIMIT -eq $LIMIT ] ||
3191                         error "the returned default quota is wrong"
3192         fi
3193
3194         # make sure the system is clean
3195         local USED=$(getquota $qtype $qid global curspace)
3196         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
3197
3198         $LFS setstripe $TESTFILE -c 1 || error "setstripe $TESTFILE failed"
3199         chown $TSTUSR.$TSTUSR $TESTFILE || error "chown $TESTFILE failed"
3200
3201         [ $qtype == "-p" ] && change_project -sp $TSTPRJID $DIR/$tdir
3202
3203         log "Test not out of quota"
3204         if [ $qpool == "data" ]; then
3205                 $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 >> 10)) oflag=sync ||
3206                         quota_error $qtype $qid "write failed, expect succeed"
3207         else
3208                 $RUNAS createmany -m $TESTFILE $((LIMIT/2)) ||
3209                         quota_error $qtype $qid "create failed, expect succeed"
3210
3211                 unlinkmany $TESTFILE $((LIMIT/2))
3212         fi
3213
3214         log "Test out of quota"
3215         # flush cache, ensure noquota flag is set on client
3216         cancel_lru_locks osc
3217         cancel_lru_locks mdc
3218         sync; sync_all_data || true
3219         if [ $qpool == "data" ]; then
3220                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
3221                         quota_error $qtype $qid "write succeed, expect EDQUOT"
3222         else
3223                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
3224                         quota_error $qtype $qid "create succeed, expect EDQUOT"
3225
3226                 unlinkmany $TESTFILE $((LIMIT*2))
3227         fi
3228
3229         log "Increase default quota"
3230         # increase default quota
3231         $LFS setquota $qdtype $qs $((LIMIT*3)) $qh $((LIMIT*3)) $DIR ||
3232                 error "set default quota failed"
3233
3234         cancel_lru_locks osc
3235         cancel_lru_locks mdc
3236         sync; sync_all_data || true
3237         if [ $qpool == "data" ]; then
3238                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
3239                         quota_error $qtype $qid "write failed, expect succeed"
3240         else
3241                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
3242                         quota_error $qtype $qid "create failed, expect succeed"
3243
3244                 unlinkmany $TESTFILE $((LIMIT*2))
3245         fi
3246
3247         log "Set quota to override default quota"
3248         $LFS setquota $qtype $qid $qs ${LIMIT} $qh ${LIMIT} $DIR ||
3249                 error "set $qid quota failed"
3250
3251         cancel_lru_locks osc
3252         cancel_lru_locks mdc
3253         sync; sync_all_data || true
3254         if [ $qpool == "data" ]; then
3255                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync &&
3256                         quota_error $qtype $qid "write succeed, expect EQUOT"
3257         else
3258                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) &&
3259                         quota_error $qtype $qid "create succeed, expect EQUOT"
3260
3261                 unlinkmany $TESTFILE $((LIMIT*2))
3262         fi
3263
3264         log "Set to use default quota again"
3265         $LFS setquota $qtype $qid -d $DIR ||
3266                 error "set $qid to use default quota failed"
3267
3268         cancel_lru_locks osc
3269         cancel_lru_locks mdc
3270         sync; sync_all_data || true
3271         if [ $qpool == "data" ]; then
3272                 $RUNAS $DD of=$TESTFILE count=$((LIMIT*2 >> 10)) oflag=sync ||
3273                         quota_error $qtype $qid "write failed, expect succeed"
3274         else
3275                 $RUNAS createmany -m $TESTFILE $((LIMIT*2)) ||
3276                         quota_error $qtype $qid "create failed, expect succeed"
3277
3278                 unlinkmany $TESTFILE $((LIMIT*2))
3279         fi
3280
3281         log "Cleanup"
3282         rm -f $TESTFILE
3283         wait_delete_completed || error "wait_delete_completed failed"
3284         sync_all_data || true
3285         $LFS setquota $qdtype -b 0 -B 0 -i 0 -I 0 $DIR ||
3286                 error "reset default quota failed"
3287         $LFS setquota $qtype $qid -b 0 -B 0 -i 0 -I 0 $DIR ||
3288                 error "reset quota failed"
3289
3290         cleanup_quota_test
3291 }
3292
3293 test_61() {
3294         test_default_quota "-u" "data"
3295         test_default_quota "-u" "meta"
3296         test_default_quota "-g" "data"
3297         test_default_quota "-g" "meta"
3298         test_default_quota "-p" "data"
3299         test_default_quota "-p" "meta"
3300 }
3301 run_test 61 "default quota tests"
3302
3303 test_62() {
3304         ! is_project_quota_supported &&
3305                 skip "Project quota is not supported"
3306          [[ "$(chattr -h 2>&1)" =~ "project" ]] ||
3307                 skip "chattr did not support project quota"
3308         setup_quota_test || error "setup quota failed with $?"
3309         local testdir=$DIR/$tdir/
3310
3311         $RUNAS mkdir -p $testdir || error "failed to mkdir"
3312         change_project -s $testdir
3313         [[ $($LFS project -d $testdir) =~ "P" ]] ||
3314                 error "inherit attribute should be set"
3315         # chattr used FS_IOC_SETFLAGS ioctl
3316         $RUNAS chattr -P $testdir &&
3317                 error "regular user clear inherit should fail"
3318         [[ $($LFS project -d $testdir) =~ "P" ]] ||
3319                 error "inherit attribute should still be set"
3320         chattr -P $testdir || error "root failed to clear inherit"
3321         [[ $($LFS project -d $testdir) =~ "P" ]] &&
3322                 error "inherit attribute should be cleared"
3323         cleanup_quota_test
3324 }
3325 run_test 62 "Project inherit should be only changed by root"
3326
3327 test_dom() {
3328         [ "$MDS1_VERSION" -lt $(version_code 2.11.55) ] &&
3329                 skip "Not supported before 2.11.55"
3330
3331         local qtype=$1
3332         local qid=$TSTUSR
3333         local dd_failed=false
3334         local tdir_dom=${tdir}_dom
3335         local LIMIT=20480 #20M
3336
3337         [ $qtype == "p" ] && ! is_project_quota_supported &&
3338                 echo "Project quota is not supported" && return 0
3339
3340         [ $qtype == "p" ] && qid=$TSTPRJID
3341
3342         setup_quota_test || error "setup quota failed with $?"
3343         trap cleanup_quota_test EXIT
3344
3345         quota_init
3346
3347         # enable mdt/ost quota
3348         set_mdt_qtype $QTYPE || error "enable mdt quota failed"
3349         set_ost_qtype $QTYPE || error "enable ost quota failed"
3350
3351         # make sure the system is clean
3352         local USED=$(getquota -$qtype $qid global curspace)
3353         [ $USED -ne 0 ] && error "Used space for $qid isn't 0."
3354
3355         chown $TSTUSR.$TSTUSR $DIR/$tdir || error "chown $tdir failed"
3356
3357         mkdir $DIR/$tdir_dom || error "mkdir $tdir_dom failed"
3358         $LFS setstripe -E 1M -L mdt $DIR/$tdir_dom ||
3359                 error "setstripe $tdir_dom failed"
3360         chown $TSTUSR.$TSTUSR $DIR/$tdir_dom || error "chown $tdir_dom failed"
3361
3362         [ $qtype == "p" ] && {
3363                 change_project -sp $TSTPRJID $DIR/$tdir
3364                 change_project -sp $TSTPRJID $DIR/$tdir_dom
3365         }
3366
3367         $LFS setquota -$qtype $qid -b $LIMIT -B $LIMIT $DIR ||
3368                 error "set $qid quota failed"
3369
3370         for ((i = 0; i < $((LIMIT/2048)); i++)); do
3371                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3372                                                                 dd_failed=true
3373         done
3374
3375         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
3376
3377         for ((i = $((LIMIT/2048)); i < $((LIMIT/1024 + 10)); i++)); do
3378                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3379                                                                 dd_failed=true
3380         done
3381
3382         $dd_failed || quota_error $qtype $qid "write succeed, expect EDQUOT"
3383
3384         rm -f $DIR/$tdir_dom/*
3385
3386         # flush cache, ensure noquota flag is set on client
3387         cancel_lru_locks osc
3388         cancel_lru_locks mdc
3389         sync; sync_all_data || true
3390
3391         dd_failed=false
3392
3393         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048)) oflag=sync ||
3394                 quota_error $qtype $qid "write failed, expect succeed"
3395
3396         for ((i = 0; i < $((LIMIT/2048 + 10)); i++)); do
3397                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3398                                                                 dd_failed=true
3399         done
3400
3401         $dd_failed || quota_error $qtype $TSTID "write succeed, expect EDQUOT"
3402
3403         rm -f $DIR/$tdir/*
3404         rm -f $DIR/$tdir_dom/*
3405
3406         # flush cache, ensure noquota flag is set on client
3407         cancel_lru_locks osc
3408         cancel_lru_locks mdc
3409         sync; sync_all_data || true
3410
3411         dd_failed=false
3412
3413         for ((i = 0; i < $((LIMIT/2048)); i++)); do
3414                 $RUNAS $DD of=$DIR/$tdir_dom/$tfile-$i count=1 oflag=sync ||
3415                                                                 dd_failed=true
3416         done
3417
3418         $dd_failed && quota_error $qtype $qid "write failed, expect succeed"
3419
3420         $RUNAS $DD of=$DIR/$tdir/file count=$((LIMIT/2048 + 10)) oflag=sync &&
3421                 quota_error $qtype $qid "write succeed, expect EDQUOT"
3422
3423         rm -f $DIR/$tdir/*
3424         rm -fr $DIR/$tdir_dom
3425
3426         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 0 $DIR ||
3427                 error "reset usr quota failed"
3428
3429         cleanup_quota_test
3430 }
3431
3432 test_63() {
3433         test_dom "u"
3434         test_dom "g"
3435         test_dom "p"
3436 }
3437 run_test 63 "quota on DoM tests"
3438
3439 test_64() {
3440         ! is_project_quota_supported &&
3441                 skip "Project quota is not supported"
3442         setup_quota_test || error "setup quota failed with $?"
3443         local dir1="$DIR/$tdir/"
3444
3445         touch $dir1/file
3446         ln -s $dir1/file $dir1/file_link
3447
3448         $LFS project -sp $TSTPRJID $dir1/file_link >&/dev/null &&
3449                 error "set symlink file's project should fail"
3450
3451         $LFS project $TSTPRJID $dir1/file_link >&/dev/null &&
3452                 error "get symlink file's project should fail"
3453
3454         cleanup_quota_test
3455 }
3456 run_test 64 "lfs project on symlink files should fail"
3457
3458 test_65() {
3459         local SIZE=10 #10M
3460         local TESTFILE="$DIR/$tdir/$tfile-0"
3461
3462         setup_quota_test || error "setup quota failed with $?"
3463         set_ost_qtype $QTYPE || error "enable ost quota failed"
3464         quota_init
3465
3466         echo "Write..."
3467         $RUNAS $DD of=$TESTFILE count=$SIZE ||
3468                 error "failed to write"
3469         # flush cache, ensure noquota flag is set on client
3470         cancel_lru_locks osc
3471         sync; sync_all_data || true
3472
3473         local quota_u=$($LFS quota -u $TSTUSR $DIR)
3474         local quota_g=$($LFS quota -g $TSTUSR $DIR)
3475         local quota_all=$($RUNAS $LFS quota $DIR)
3476
3477         [ "$(echo "$quota_all" | head -n3)" != "$quota_u" ] &&
3478                 error "usr quota not match"
3479         [ "$(echo "$quota_all" | tail -n3)" != "$quota_g" ] &&
3480                 error "grp quota not match"
3481
3482         rm -f $TESTFILE
3483         # cleanup
3484         cleanup_quota_test
3485 }
3486 run_test 65 "Check lfs quota result"
3487
3488 test_66() {
3489         ! is_project_quota_supported &&
3490                 skip "Project quota is not supported"
3491         setup_quota_test || error "setup quota failed with $?"
3492         stack_trap cleanup_quota_test EXIT
3493         local old=$(do_facet mds1 $LCTL get_param -n \
3494                     mdt.*.enable_chprojid_gid | head -1)
3495         local testdir=$DIR/$tdir/foo
3496
3497         do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=0
3498         stack_trap "do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=0" \
3499                 EXIT
3500
3501         test_mkdir -i 0 -c 1 $testdir || error "failed to mkdir"
3502         chown -R $TSTID:$TSTID $testdir
3503         change_project -sp $TSTPRJID $testdir
3504         $RUNAS mkdir $testdir/foo || error "failed to mkdir foo"
3505
3506         $RUNAS lfs project -p 0 $testdir/foo &&
3507                 error "nonroot user should fail to set projid"
3508
3509         $RUNAS lfs project -C $testdir/foo &&
3510                 error "nonroot user should fail to clear projid"
3511
3512         change_project -C $testdir/foo || error "failed to clear project"
3513
3514         do_facet mds1 $LCTL set_param mdt.*.enable_chprojid_gid=-1
3515         $RUNAS lfs project -p $TSTPRJID $testdir/foo || error \
3516         "failed to set projid with normal user when enable_chprojid_gid=-1"
3517
3518         $RUNAS lfs project -rC $testdir/ || error \
3519 "failed to clear project state with normal user when enable_chprojid_gid=-1"
3520
3521         touch $testdir/bar || error "failed touch $testdir/bar"
3522         $RUNAS lfs project -p $TSTPRJID $testdir/bar && error \
3523         "normal user should not be able to set projid on root owned file"
3524
3525         change_project -p $TSTPRJID $testdir/bar || error \
3526                 "root should be able to change its own file's projid"
3527
3528         cleanup_quota_test
3529 }
3530 run_test 66 "nonroot user can not change project state in default"
3531
3532 quota_fini()
3533 {
3534         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=-quota"
3535         if $PQ_CLEANUP; then
3536                 disable_project_quota
3537         fi
3538 }
3539 reset_quota_settings
3540 quota_fini
3541
3542 cd $ORIG_PWD
3543 complete $SECONDS
3544 check_and_cleanup_lustre
3545 export QUOTA_AUTO=$QUOTA_AUTO_OLD
3546 exit_status