Whamcloud - gitweb
b08e2502f2d3df8be410ebe0d8d5247e2beeb6b7
[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 # Run test by setting NOSETUP=true when ltest has setup env for us
7 set -e
8
9 SRCDIR=`dirname $0`
10 export PATH=$PWD/$SRCDIR:$SRCDIR:$PWD/$SRCDIR/../utils:$PATH:/sbin
11
12 ONLY=${ONLY:-"$*"}
13 # only accounting tests and test 22 are supported for the time being
14 ALWAYS_EXCEPT="0 1 2 3 4 5 6 7 8 9 10 11 12 13 15 17 18 19 20 21 23 24 27 30 $SANITY_QUOTA_EXCEPT"
15 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
16
17 [ "$ALWAYS_EXCEPT$EXCEPT" ] &&
18         echo "Skipping tests: `echo $ALWAYS_EXCEPT $EXCEPT`"
19
20 TMP=${TMP:-/tmp}
21
22 ORIG_PWD=${PWD}
23 TSTID=${TSTID:-60000}
24 TSTID2=${TSTID2:-60001}
25 TSTUSR=${TSTUSR:-"quota_usr"}
26 TSTUSR2=${TSTUSR2:-"quota_2usr"}
27 BLK_SZ=1024
28 MAX_DQ_TIME=604800
29 MAX_IQ_TIME=604800
30
31 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
32 . $LUSTRE/tests/test-framework.sh
33 init_test_env $@
34 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
35 init_logging
36 DIRECTIO=${DIRECTIO:-$LUSTRE/tests/directio}
37
38 [ $MDSCOUNT -gt 1 ] && skip "CMD case" && exit 0
39
40 require_dsh_mds || exit 0
41 require_dsh_ost || exit 0
42
43 # XXX Once we drop the interoperability with old server (< 2.3.50), the
44 #     sanity-quota-old.sh should be removed.
45 if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.50) ]; then
46         exec $LUSTRE/tests/sanity-quota-old.sh
47 fi
48
49 # if e2fsprogs support quota feature?
50 if [ $(facet_fstype $SINGLEMDS) == ldiskfs ] && \
51         ! $DEBUGFS -c -R supported_features | grep -q 'quota'; then
52         skip "e2fsprogs doesn't support quota" && exit 0
53 fi
54
55 [ "$SLOW" = "no" ] && EXCEPT_SLOW="9 18 21"
56
57 QUOTALOG=${TESTSUITELOG:-$TMP/$(basename $0 .sh).log}
58
59 [ "$QUOTALOG" ] && rm -f $QUOTALOG || true
60
61 DIR=${DIR:-$MOUNT}
62 DIR2=${DIR2:-$MOUNT2}
63
64 QUOTA_AUTO_OLD=$QUOTA_AUTO
65 export QUOTA_AUTO=0
66
67 check_and_setup_lustre
68
69 LOVNAME=`lctl get_param -n llite.*.lov.common_name | tail -n 1`
70 OSTCOUNT=`lctl get_param -n lov.$LOVNAME.numobd`
71
72 SHOW_QUOTA_USER="$LFS quota -v -u $TSTUSR $DIR"
73 SHOW_QUOTA_USERID="$LFS quota -v -u $TSTID $DIR"
74 SHOW_QUOTA_USER2="$LFS quota -v -u $TSTUSR2 $DIR"
75 SHOW_QUOTA_GROUP="$LFS quota -v -g $TSTUSR $DIR"
76 SHOW_QUOTA_GROUPID="$LFS quota -v -g $TSTID $DIR"
77 SHOW_QUOTA_GROUP2="$LFS quota -v -g $TSTUSR2 $DIR"
78 SHOW_QUOTA_INFO_USER="$LFS quota -t -u $DIR"
79 SHOW_QUOTA_INFO_GROUP="$LFS quota -t -g $DIR"
80
81 build_test_filter
82
83 # Use OFD for all quota testing
84 cleanupall
85 USE_OFD_OLD="$USE_OFD"
86 LOAD_MODULES_REMOTE_OLD="$LOAD_MODULES_REMOTE"
87 export USE_OFD="yes"
88 export LOAD_MODULES_REMOTE="true"
89 setupall
90
91 lustre_fail() {
92         local fail_node=$1
93         local fail_loc=$2
94         local fail_val=${3:-0}
95
96         if [ $fail_node == "mds" ] || [ $fail_node == "mds_ost" ]; then
97                 do_facet $SINGLEMDS "lctl set_param fail_val=$fail_val"
98                 do_facet $SINGLEMDS "lctl set_param fail_loc=$fail_loc"
99         fi
100
101         if [ $fail_node == "ost" ] || [ $fail_node == "mds_ost" ]; then
102                 for num in `seq $OSTCOUNT`; do
103                         do_facet ost$num "lctl set_param fail_val=$fail_val"
104                         do_facet ost$num "lctl set_param fail_loc=$fail_loc"
105                 done
106         fi
107 }
108
109 RUNAS="runas -u $TSTID -g $TSTID"
110 RUNAS2="runas -u $TSTID2 -g $TSTID2"
111 FAIL_ON_ERROR=true check_runas_id $TSTID $TSTID $RUNAS
112 FAIL_ON_ERROR=true check_runas_id $TSTID2 $TSTID2 $RUNAS2
113 DD="dd if=/dev/zero bs=1M"
114
115 FAIL_ON_ERROR=false
116
117 # clear quota limits for a user or a group
118 # usage: resetquota -u username
119 #        resetquota -g groupname
120
121 resetquota() {
122         [ "$#" != 2 ] && error "resetquota: wrong number of arguments: $#"
123         [ "$1" != "-u" -a "$1" != "-g" ] &&
124                 error "resetquota: wrong specifier $1 passed"
125
126         $LFS setquota "$1" "$2" -b 0 -B 0 -i 0 -I 0 $MOUNT
127         # The usage should be 0 now, enforce quota to trigger quota
128         # release for each slave, sleep 3 seconds to wait for the release
129         # done, then we can make sure granted quota for each slave is 0.
130         $LFS setquota "$1" "$2" -b 0 -B 1 -i 0 -I 1 $MOUNT
131         sleep 3
132         $LFS setquota "$1" "$2" -b 0 -B 0 -i 0 -I 0 $MOUNT
133 }
134
135 quota_scan() {
136         local LOCAL_UG=$1
137         local LOCAL_ID=$2
138
139         if [ "$LOCAL_UG" == "a" -o "$LOCAL_UG" == "u" ]; then
140                 $LFS quota -v -u $LOCAL_ID $DIR
141                 log "Files for user ($LOCAL_ID):"
142                 ($LFS find -user $LOCAL_ID $DIR | head -n 4 |
143                         xargs stat 2>/dev/null)
144         fi
145
146         if [ "$LOCAL_UG" == "a" -o "$LOCAL_UG" == "g" ]; then
147                 $LFS quota -v -u $LOCAL_ID $DIR
148                 log "Files for group ($LOCAL_ID):"
149                 ($LFS find -group $LOCAL_ID $DIR | head -n 4 |
150                         xargs stat 2>/dev/null)
151         fi
152 }
153
154 quota_error() {
155         quota_scan $1 $2
156         shift 2
157         error "$*"
158 }
159
160 quota_log() {
161         quota_scan $1 $2
162         shift 2
163         log "$*"
164 }
165
166 # get quota for a user or a group
167 # usage: getquota -u|-g <username>|<groupname> global|<obd_uuid> \
168 #                 bhardlimit|bsoftlimit|bgrace|ihardlimit|isoftlimit|igrace
169 #
170 getquota() {
171         local spec
172         local uuid
173
174         [ "$#" != 4 ] && error "getquota: wrong number of arguments: $#"
175         [ "$1" != "-u" -a "$1" != "-g" ] &&
176                 error "getquota: wrong u/g specifier $1 passed"
177
178         uuid="$3"
179
180         case "$4" in
181                 curspace)   spec=1;;
182                 bsoftlimit) spec=2;;
183                 bhardlimit) spec=3;;
184                 bgrace)     spec=4;;
185                 curinodes)  spec=5;;
186                 isoftlimit) spec=6;;
187                 ihardlimit) spec=7;;
188                 igrace)     spec=8;;
189                 *)          error "unknown quota parameter $4";;
190         esac
191
192         [ "$uuid" = "global" ] && uuid=$DIR
193
194         $LFS quota -v "$1" "$2" $DIR |
195                 awk 'BEGIN { num='$spec' } { if ($1 == "'$uuid'") \
196                 { if (NF == 1) { getline } else { num++ } ; print $num;} }' \
197                 | tr -d "*"
198 }
199
200 # set mdt quota type
201 # usage: set_mdt_qtype ug|u|g|none
202 set_mdt_qtype() {
203         local qtype=$1
204         local varsvc=${SINGLEMDS}_svc
205         do_facet mgs $LCTL conf_param $FSNAME.quota.mdt=$qtype
206         if $(facet_up $SINGLEMDS); then
207                 wait_update_facet $SINGLEMDS "$LCTL get_param -n \
208                         osd-$FSTYPE.${!varsvc}.quota_slave.enabled" "$qtype" ||
209                                 return 1
210         fi
211         return 0
212 }
213
214 # set ost quota type
215 # usage: set_ost_quota_type ug|u|g|none
216 set_ost_qtype() {
217         local qtype=$1
218         local varsvc
219         local osts=$(get_facets OST)
220         do_facet mgs $LCTL conf_param $FSNAME.quota.ost=$qtype
221         # we have to make sure each OST received config changes
222         for ost in ${osts//,/ }; do
223                 varsvc=${ost}_svc
224                 if $(facet_up $ost); then
225                         wait_update_facet $ost "$LCTL get_param -n \
226                                 osd-$FSTYPE.${!varsvc}.quota_slave.enabled" \
227                                         "$qtype" || return 1
228                 fi
229         done
230         return 0
231 }
232
233 wait_reintegration() {
234         local ntype=$1
235         local qtype=$2
236         local result="glb[1],slv[1],reint[0]"
237         local varsvc
238
239         if [ $ntype == "mdt" ]; then
240                 varsvc=${SINGLEMDS}_svc
241                 if $(facet_up $SINGLEMDS); then
242                         wait_update_facet $SINGLEMDS "$LCTL get_param -n \
243                         osd-$FSTYPE.${!varsvc}.quota_slave.info |
244                         grep "$qtype" | awk '{ print \\\$3 }'" "$result" ||
245                                 return 1
246                 fi
247         else
248                 local osts=$(get_facets OST)
249                 for ost in ${osts//,/ }; do
250                         varsvc=${ost}_svc
251                         if $(facet_up $ost); then
252                                 wait_update_facet $ost "$LCTL get_param -n \
253                                 osd-$FSTYPE.${!varsvc}.quota_slave.info |
254                                 grep "$qtype" | awk '{ print \\\$3 }'" \
255                                         "$result" || return 1
256                         fi
257                 done
258         fi
259         return 0
260 }
261
262 wait_mdt_reint() {
263         local qtype=$1
264
265         if [ $qtype == "u" ] || [ $qtype == "ug" ]; then
266                 wait_reintegration "mdt" "user" || return 1
267         fi
268
269         if [ $qtype == "g" ] || [ $qtype == "ug" ]; then
270                 wait_reintegration "mdt" "group" || return 1
271         fi
272         return 0
273 }
274
275 wait_ost_reint() {
276         local qtype=$1
277
278         if [ $qtype == "u" ] || [ $qtype == "ug" ]; then
279                 wait_reintegration "ost" "user" || return 1
280         fi
281
282         if [ $qtype == "g" ] || [ $qtype == "ug" ]; then
283                 wait_reintegration "ost" "group" || return 1
284         fi
285         return 0
286 }
287
288 setup_quota_test() {
289         rm -rf $DIR/$tdir
290         wait_delete_completed
291         echo "Creating test directory"
292         mkdir -p $DIR/$tdir
293         chmod 077 $DIR/$tdir
294         # always clear fail_loc in case of fail_loc isn't cleared
295         # properly when previous test failed
296         lustre_fail mds_ost 0
297 }
298
299 cleanup_quota_test() {
300         trap 0
301         echo "Delete files..."
302         rm -rf $DIR/$tdir
303         echo "Wait for unlink objects finished..."
304         wait_delete_completed
305         sync_all_data || true
306 }
307
308 quota_show_check() {
309         local bf=$1
310         local ug=$2
311         local qid=$3
312         local usage
313
314         $LFS quota -v -$ug $qid $DIR
315
316         if [ "$bf" == "a" -o "$bf" == "b" ]; then
317                 usage=$(getquota -$ug $qid global curspace)
318                 if [ -z $usage ]; then
319                         quota_error $ug $qid \
320                                 "Query block quota failed ($ug:$qid)."
321                 else
322                         [ $usage -ne 0 ] && quota_log $ug $qid \
323                                 "Block quota isn't 0 ($ug:$qid:$usage)."
324                 fi
325         fi
326
327         if [ "$bf" == "a" -o "$bf" == "f" ]; then
328                 usage=$(getquota -$ug $qid global curinodes)
329                 if [ -z $usage ]; then
330                         quota_error $ug $qid \
331                                 "Query file quota failed ($ug:$qid)."
332                 else
333                         [ $usage -ne 0 ] && quota_log $ug $qid \
334                                 "File quota isn't 0 ($ug:$qid:$usage)."
335                 fi
336         fi
337 }
338
339 # enable quota debug
340 quota_init() {
341         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=+quota"
342 }
343 quota_init
344
345 test_quota_performance() {
346         local TESTFILE="$DIR/$tdir/$tfile-0"
347         local size=$1 # in MB
348         local stime=$(date +%s)
349         $RUNAS $DD of=$TESTFILE count=$size conv=fsync ||
350                 quota_error u $TSTUSR "write failure"
351         local etime=$(date +%s)
352         delta=$((etime - stime))
353         if [ $delta -gt 0 ]; then
354             rate=$((size * 1024 / delta))
355             [ $rate -gt 1024 ] ||
356                 error "SLOW IO for $TSTUSR (user): $rate KB/sec"
357         fi
358         rm -f $TESTFILE
359 }
360
361 # test basic quota performance b=21696
362 test_0() {
363         setup_quota_test
364         trap cleanup_quota_test EXIT
365
366         local MB=100 # 100M
367         [ "$SLOW" = "no" ] && MB=10
368
369         set_ost_qtype "none" || error "disable ost quota failed"
370         test_quota_performance $MB
371
372         set_ost_qtype "ug" || error "enable ost quota failed"
373         $LFS setquota -u $TSTUSR -b 0 -B 10G -i 0 -I 0 $DIR
374         test_quota_performance $MB
375
376         cleanup_quota_test
377         resetquota -u $TSTUSR
378 }
379 run_test 0 "Test basic quota performance"
380
381 # test block hardlimit
382 test_1() {
383         local LIMIT=10  # 10M
384         local TESTFILE="$DIR/$tdir/$tfile-0"
385
386         setup_quota_test
387         trap cleanup_quota_test EXIT
388
389         # enable ost quota
390         set_ost_qtype "ug" || "enable ost quota failed"
391
392         # test for user
393         log "User quota (block hardlimit:$LIMIT MB)"
394         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR
395
396         # make sure the system is clean
397         local USED=$(getquota -u $TSTUSR global curspace)
398         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
399
400         $LFS setstripe $TESTFILE -c 1
401         chown $TSTUSR.$TSTUSR $TESTFILE
402
403         log "Write..."
404         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) ||
405                 quota_error u $TSTUSR "user write failure, but expect success"
406         log "Write out of block quota ..."
407         # this time maybe cache write,  ignore it's failure
408         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
409         # flush cache, ensure noquota flag is set on client
410         cancel_lru_locks osc
411         $RUNAS $DD of=$TESTFILE count=1 seek=$LIMIT &&
412                 quota_error u $TSTUSR "user write success, but expect EDQUOT"
413
414         rm -f $TESTFILE
415         wait_delete_completed
416         sync_all_data || true
417         USED=$(getquota -u $TSTUSR global curspace)
418         [ $USED -ne 0 ] && quota_error u $TSTUSR \
419                 "user quota isn't released after deletion"
420         resetquota -u $TSTUSR
421
422         # test for group
423         log "--------------------------------------"
424         log "Group quota (block hardlimit:$LIMIT MB)"
425         $LFS setquota -g $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR
426
427         TESTFILE="$DIR/$tdir/$tfile-1"
428         # make sure the system is clean
429         USED=$(getquota -g $TSTUSR global curspace)
430         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0"
431
432         $LFS setstripe $TESTFILE -c 1
433         chown $TSTUSR.$TSTUSR $TESTFILE
434
435         log "Write ..."
436         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) ||
437                 quota_error g $TSTUSR "group write failure, but expect success"
438         log "Write out of block quota ..."
439         # this time maybe cache write, ignore it's failure
440         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) seek=$((LIMIT/2)) || true
441         cancel_lru_locks osc
442         $RUNAS $DD of=$TESTFILE count=10 seek=$LIMIT &&
443                 quota_error g $TSTUSR "group write success, but expect EDQUOT"
444
445         # cleanup
446         cleanup_quota_test
447
448         USED=$(getquota -g $TSTUSR global curspace)
449         [ $USED -ne 0 ] && quota_error g $TSTUSR \
450                 "group quota isn't released after deletion"
451
452         resetquota -g $TSTUSR
453 }
454 run_test 1 "Block hard limit (normal use and out of quota)"
455
456 # test inode hardlimit
457 test_2() {
458         local LIMIT=$((1024 * 1024)) # 1M inodes
459         local TESTFILE="$DIR/$tdir/$tfile-0"
460
461         [ "$SLOW" = "no" ] && LIMIT=1024 # 1k inodes
462
463         setup_quota_test
464         trap cleanup_quota_test EXIT
465
466         # enable mdt quota
467         set_mdt_qtype "ug" || "enable mdt quota failed"
468
469         # test for user
470         log "User quota (inode hardlimit:$LIMIT files)"
471         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR
472
473         # make sure the system is clean
474         local USED=$(getquota -u $TSTUSR global curinodes)
475         [ $USED -ne 0 ] && error "Used inodes($USED) for user $TSTUSR isn't 0."
476
477         log "Create $LIMIT files ..."
478         $RUNAS createmany -m ${TESTFILE} $LIMIT || \
479                 quota_error u $TSTUSR "user create failure, but expect success"
480         log "Create out of file quota ..."
481         $RUNAS touch ${TESTFILE}_xxx && \
482                 quota_error u $TSTUSR "user create success, but expect EDQUOT"
483
484         # cleanup
485         unlinkmany ${TESTFILE} $LIMIT
486         rm -f ${TESTFILE}_xxx
487         wait_delete_completed
488
489         USED=$(getquota -u $TSTUSR global curinodes)
490         [ $USED -ne 0 ] && quota_error u $TSTUSR \
491                 "user quota isn't released after deletion"
492         resetquota -u $TSTUSR
493
494         # test for group
495         log "--------------------------------------"
496         log "Group quota (inode hardlimit:$LIMIT files)"
497         $LFS setquota -g $TSTUSR -b 0 -B 0 -i 0 -I $LIMIT $DIR
498
499         TESTFILE=$DIR/$tdir/$tfile-1
500         # make sure the system is clean
501         USED=$(getquota -g $TSTUSR global curinodes)
502         [ $USED -ne 0 ] && error "Used inodes($USED) for group $TSTUSR isn't 0."
503
504         log "Create $LIMIT files ..."
505         $RUNAS createmany -m ${TESTFILE} $LIMIT ||
506                 quota_error g $TSTUSR "group create failure, but expect success"
507         log "Create out of file quota ..."
508         $RUNAS touch ${TESTFILE}_xxx && \
509                 quota_error g $TSTUSR "group create success, but expect EDQUOT"
510
511         # cleanup
512         unlinkmany ${TESTFILE} $LIMIT
513         rm -f ${TESTFILE}_xxx
514         wait_delete_completed
515
516         USED=$(getquota -g $TSTUSR global curinodes)
517         [ $USED -ne 0 ] && quota_error g $TSTUSR \
518                 "user quota isn't released after deletion"
519
520         cleanup_quota_test
521         resetquota -g $TSTUSR
522 }
523 run_test 2 "File hard limit (normal use and out of quota)"
524
525 test_block_soft() {
526         local TESTFILE=$1
527         local TIMER=$(($2 * 3 / 2))
528         local LIMIT=$3
529         local OFFSET=0
530
531         setup_quota_test
532         trap cleanup_quota_test EXIT
533
534         $LFS setstripe $TESTFILE -c 1 -i 0
535         chown $TSTUSR.$TSTUSR $TESTFILE
536
537         echo "Write up to soft limit"
538         $RUNAS $DD of=$TESTFILE count=$LIMIT ||
539                 quota_error a $TSTUSR "write failure, but expect success"
540         OFFSET=$((LIMIT * 1024))
541         cancel_lru_locks osc
542
543         echo "Write to exceed soft limit"
544         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET ||
545                 quota_error a $TSTUSR "write failure, but expect success"
546         OFFSET=$((OFFSET + 1024)) # make sure we don't write to same block
547         cancel_lru_locks osc
548
549         $SHOW_QUOTA_USER
550         $SHOW_QUOTA_GROUP
551         $SHOW_QUOTA_INFO_USER
552         $SHOW_QUOTA_INFO_GROUP
553
554         echo "Write before timer goes off"
555         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET ||
556                 quota_error a $TSTUSR "write failure, but expect success"
557         OFFSET=$((OFFSET + 1024))
558         cancel_lru_locks osc
559
560         echo "Sleep $TIMER seconds ..."
561         sleep $TIMER
562
563         $SHOW_QUOTA_USER
564         $SHOW_QUOTA_GROUP
565         $SHOW_QUOTA_INFO_USER
566         $SHOW_QUOTA_INFO_GROUP
567
568         echo "Write after timer goes off"
569         # maybe cache write, ignore.
570         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET || true
571         OFFSET=$((OFFSET + 1024))
572         cancel_lru_locks osc
573         $RUNAS dd if=/dev/zero of=$TESTFILE bs=1K count=10 seek=$OFFSET &&
574                 quota_error a $TSTUSR "write success, but expect EDQUOT"
575
576         $SHOW_QUOTA_USER
577         $SHOW_QUOTA_GROUP
578         $SHOW_QUOTA_INFO_USER
579         $SHOW_QUOTA_INFO_GROUP
580
581         echo "Unlink file to stop timer"
582         rm -f $TESTFILE
583         wait_delete_completed
584         sync_all_data || true
585
586         $SHOW_QUOTA_USER
587         $SHOW_QUOTA_GROUP
588         $SHOW_QUOTA_INFO_USER
589         $SHOW_QUOTA_INFO_GROUP
590
591         $LFS setstripe $TESTFILE -c 1 -i 0
592         chown $TSTUSR.$TSTUSR $TESTFILE
593
594         echo "Write ..."
595         $RUNAS $DD of=$TESTFILE count=$LIMIT ||
596                 quota_error a $TSTUSR "write failure, but expect success"
597         # cleanup
598         cleanup_quota_test
599 }
600
601 # block soft limit
602 test_3() {
603         local LIMIT=1  # 1MB
604         local GRACE=10 # 10s
605         local TESTFILE=$DIR/$tdir/$tfile-0
606
607         set_ost_qtype "ug" || error "enable ost quota failed"
608
609         echo "User quota (soft limit:$LIMIT MB  grace:$GRACE seconds)"
610         # make sure the system is clean
611         local USED=$(getquota -u $TSTUSR global curspace)
612         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
613
614         $LFS setquota -t -u --block-grace $GRACE --inode-grace $MAX_IQ_TIME $DIR
615         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR
616
617         test_block_soft $TESTFILE $GRACE $LIMIT
618         resetquota -u $TSTUSR
619
620         echo "Group quota (soft limit:$LIMIT MB  grace:$GRACE seconds)"
621         TESTFILE=$DIR/$tdir/$tfile-1
622         # make sure the system is clean
623         USED=$(getquota -g $TSTUSR global curspace)
624         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
625
626         $LFS setquota -t -g --block-grace $GRACE --inode-grace $MAX_IQ_TIME $DIR
627         $LFS setquota -g $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR
628
629         test_block_soft $TESTFILE $GRACE $LIMIT
630         resetquota -g $TSTUSR
631
632         # cleanup
633         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
634                 $MAX_IQ_TIME $DIR
635         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
636                 $MAX_IQ_TIME $DIR
637 }
638 run_test 3 "Block soft limit (start timer, timer goes off, stop timer)"
639
640 test_file_soft() {
641         local TESTFILE=$1
642         local LIMIT=$2
643         local TIMER=$(($3 * 3 / 2))
644
645         setup_quota_test
646         trap cleanup_quota_test EXIT
647
648         echo "Create files to exceed soft limit"
649         $RUNAS createmany -m ${TESTFILE}_ $((LIMIT + 1)) ||
650                 quota_error a $TSTUSR "create failure, but expect success"
651         sync; sleep 1; sync
652
653         echo "Create file before timer goes off"
654         $RUNAS touch ${TESTFILE}_before ||
655                 quota_error a $TSTUSR "failed create before timer expired," \
656                         "but expect success"
657         sync; sleep 1; sync
658
659         echo "Sleep $TIMER seconds ..."
660         sleep $TIMER
661
662         $SHOW_QUOTA_USER
663         $SHOW_QUOTA_GROUP
664         $SHOW_QUOTA_INFO_USER
665         $SHOW_QUOTA_INFO_GROUP
666
667         echo "Create file after timer goes off"
668         # There is a window that space is accounted in the quota usage but
669         # hasn't been decreased from the pending write, if we acquire quota
670         # in this window, we'll acquire more than we needed.
671         $RUNAS touch ${TESTFILE}_after_1 ${TESTFILE}_after_2 || true
672         sync; sleep 1; sync
673         $RUNAS touch ${TESTFILE}_after_3 &&
674                 quota_error a $TSTUSR "create after timer expired," \
675                         "but expect EDQUOT"
676         sync; sleep 1; sync
677
678         $SHOW_QUOTA_USER
679         $SHOW_QUOTA_GROUP
680         $SHOW_QUOTA_INFO_USER
681         $SHOW_QUOTA_INFO_GROUP
682
683         echo "Unlink files to stop timer"
684         find $(dirname $TESTFILE) -name "$(basename ${TESTFILE})*" | xargs rm -f
685         wait_delete_completed
686
687         echo "Create file"
688         $RUNAS touch ${TESTFILE}_xxx ||
689                 quota_error a $TSTUSR "touch after timer stop failure," \
690                         "but expect success"
691         sync; sleep 1; sync
692
693         # cleanup
694         cleanup_quota_test
695 }
696
697 # file soft limit
698 test_4a() {
699         local LIMIT=10 # inodes
700         local TESTFILE=$DIR/$tdir/$tfile-0
701         local GRACE=5
702
703         set_mdt_qtype "ug" || error "enable mdt quota failed"
704
705         echo "User quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
706         # make sure the system is clean
707         local USED=$(getquota -u $TSTUSR global curinodes)
708         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
709
710         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace $GRACE $DIR
711         $LFS setquota -u $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR
712
713         test_file_soft $TESTFILE $LIMIT $GRACE
714         resetquota -u $TSTUSR
715
716         echo "Group quota (soft limit:$LIMIT files  grace:$GRACE seconds)"
717         # make sure the system is clean
718         USED=$(getquota -g $TSTUSR global curinodes)
719         [ $USED -ne 0 ] && error "Used space($USED) for group $TSTUSR isn't 0."
720
721         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace $GRACE $DIR
722         $LFS setquota -g $TSTUSR -b 0 -B 0 -i $LIMIT -I 0 $DIR
723         TESTFILE=$DIR/$tdir/$tfile-1
724
725         test_file_soft $TESTFILE $LIMIT $GRACE
726         resetquota -g $TSTUSR
727
728         # cleanup
729         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
730                 $MAX_IQ_TIME $DIR
731         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
732                 $MAX_IQ_TIME $DIR
733 }
734 run_test 4a "File soft limit (start timer, timer goes off, stop timer)"
735
736 test_4b() {
737         local GR_STR1="1w3d"
738         local GR_STR2="1000s"
739         local GR_STR3="5s"
740         local GR_STR4="1w2d3h4m5s"
741         local GR_STR5="5c"
742         local GR_STR6="1111111111111111"
743
744         wait_delete_completed
745
746         # test of valid grace strings handling
747         echo "Valid grace strings test"
748         $LFS setquota -t -u --block-grace $GR_STR1 --inode-grace $GR_STR2 $DIR
749         $LFS quota -u -t $DIR | grep "Block grace time: $GR_STR1"
750         $LFS setquota -t -g --block-grace $GR_STR3 --inode-grace $GR_STR4 $DIR
751         $LFS quota -g -t $DIR | grep "Inode grace time: $GR_STR4"
752
753         # test of invalid grace strings handling
754         echo "  Invalid grace strings test"
755         ! $LFS setquota -t -u --block-grace $GR_STR4 --inode-grace $GR_STR5 $DIR
756         ! $LFS setquota -t -g --block-grace $GR_STR4 --inode-grace $GR_STR6 $DIR
757
758         # cleanup
759         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
760                 $MAX_IQ_TIME $DIR
761         $LFS setquota -t -g --block-grace $MAX_DQ_TIME --inode-grace \
762                 $MAX_IQ_TIME $DIR
763 }
764 run_test 4b "Grace time strings handling"
765
766 # chown & chgrp (chown & chgrp successfully even out of block/file quota)
767 test_5() {
768         local BLIMIT=10 # 10M
769         local ILIMIT=10 # 10 inodes
770
771         setup_quota_test
772         trap cleanup_quota_test EXIT
773
774         set_mdt_qtype "ug" || error "enable mdt quota failed"
775         set_ost_qtype "ug" || error "enable ost quota failed"
776
777         echo "Set quota limit (0 ${BLIMIT}M 0 $ILIMIT) for $TSTUSR.$TSTUSR"
778         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR
779         $LFS setquota -g $TSTUSR -b 0 -B ${BLIMIT}M -i 0 -I $ILIMIT $DIR
780
781         # make sure the system is clean
782         local USED=$(getquota -u $TSTUSR global curinodes)
783         [ $USED -ne 0 ] && error "Used inode($USED) for user $TSTUSR isn't 0."
784         USED=$(getquota -g $TSTUSR global curinodes)
785         [ $USED -ne 0 ] && error "Used inode($USED) for group $TSTUSR isn't 0."
786         USED=$(getquota -u $TSTUSR global curspace)
787         [ $USED -ne 0 ] && error "Used block($USED) for user $TSTUSR isn't 0."
788         USED=$(getquota -g $TSTUSR global curspace)
789         [ $USED -ne 0 ] && error "Used block($USED) for group $TSTUSR isn't 0."
790
791         echo "Create more than $ILIMIT files and more than $BLIMIT MB ..."
792         createmany -m $DIR/$tdir/$tfile-0_ $((ILIMIT + 1)) ||
793                 error "create failure, expect success"
794         $DD of=$DIR/$tdir/$tfile-0_1 count=$((BLIMIT+1)) ||
795                 error "write failure, expect success"
796
797         echo "Chown files to $TSTUSR.$TSTUSR ..."
798         for i in `seq 0 $ILIMIT`; do
799                 chown $TSTUSR.$TSTUSR $DIR/$tdir/$tfile-0_$i ||
800                         quota_error a $TSTUSR "chown failure, expect success"
801         done
802
803         # cleanup
804         unlinkmany $DIR/$tdir/$tfile-0_ $((ILIMIT + 1))
805         cleanup_quota_test
806
807         resetquota -u $TSTUSR
808         resetquota -g $TSTUSR
809 }
810 run_test 5 "Chown & chgrp successfully even out of block/file quota"
811
812 # test dropping acquire request on master
813 test_6() {
814         setup_quota_test
815         trap cleanup_quota_test EXIT
816
817         # make sure the system is clean
818         local USED=$(getquota -u $TSTUSR global curspace)
819         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
820
821         # make sure no granted quota on ost
822         set_ost_qtype "ug" || error "enable ost quota failed"
823         resetquota -u $TSTUSR
824
825         # create file for $TSTUSR
826         local TESTFILE=$DIR/$tdir/$tfile-$TSTUSR
827         $LFS setstripe $TESTFILE -c 1 -i 0
828         chown $TSTUSR.$TSTUSR $TESTFILE
829
830         # create file for $TSTUSR2
831         local TESTFILE2=$DIR/$tdir/$tfile-$TSTUSR2
832         $LFS setstripe $TESTFILE2 -c 1 -i 0
833         chown $TSTUSR2.$TSTUSR2 $TESTFILE2
834
835         # cache per-ID lock for $TSTUSR on slave
836         $LFS setquota -u $TSTUSR -b 0 -B 2M -i 0 -I 0 $DIR
837         $RUNAS $DD of=$TESTFILE count=1 ||
838                 error "write $TESTFILE failure, expect success"
839         $RUNAS2 $DD of=$TESTFILE2 count=1 ||
840                 error "write $TESTFILE2 failure, expect success"
841         sync; sync
842         sync_all_data || true
843
844         # set a small timeout value, to make the quota RPC timedout before
845         # the watchdog triggered.
846         local timeout
847         if at_is_enabled; then
848                 timeout=$(at_max_get ost)
849                 at_max_set 20 ost
850         else
851                 timeout=$(do_facet ost1 $LCTL get_param -n timeout)
852                 do_facet ost1 $LCTL set_param timeout=20
853         fi
854
855         #define QUOTA_DQACQ 601
856         #define OBD_FAIL_PTLRPC_DROP_REQ_OPC 0x513
857         lustre_fail mds 0x513 601
858
859         # write to un-enforced ID ($TSTUSR2) should succeed
860         $RUNAS2 $DD of=$TESTFILE2 count=1 seek=1 oflag=sync conv=notrunc ||
861                 error "write failure, expect success"
862
863         # write to enforced ID ($TSTUSR) in background, exceeding limit
864         # to make sure DQACQ is sent
865         $RUNAS $DD of=$TESTFILE count=2 seek=1 oflag=sync conv=notrunc &
866         DDPID=$!
867
868         echo "Sleep for $timeout"
869         sleep $timeout
870
871         # write should be blocked and never finished
872         if ! ps -p $DDPID  > /dev/null 2>&1; then
873                 lustre_fail mds 0 0
874                 error "write finished incorrectly!"
875         fi
876
877         if at_is_enabled; then
878                 at_max_set $timeout ost
879         else
880                 do_facet ost1 $LCTL set_param timeout=$timeout
881         fi
882
883         lustre_fail mds 0 0
884
885         # no watchdog is triggered
886         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
887         watchdog=$(awk '/sanity-quota test 6/ {start = 1;}
888                        /Service thread pid/ && /was inactive/ {
889                                if (start) {
890                                        print;
891                                }
892                        }' $TMP/lustre-log-${TESTNAME}.log)
893         [ -z "$watchdog" ] || error "$watchdog"
894
895         rm -f $TMP/lustre-log-${TESTNAME}.log
896
897         # write should continue & succeed
898         local count=0
899         while [ true ]; do
900                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
901                 if [ $count -ge 120 ]; then
902                         quota_error u $TSTUSR "dd not finished in $count secs"
903                 fi
904                 count=$((count + 1))
905                 [ $((count % 10)) -eq 0 ] && echo "Waiting $count secs"
906                 sleep 1
907         done
908
909         cleanup_quota_test
910         resetquota -u $TSTUSR
911 }
912 run_test 6 "Test dropping acquire request on master"
913
914 # quota reintegration (global index)
915 test_7a() {
916         local TESTFILE=$DIR/$tdir/$tfile
917         local LIMIT=20 # 20M
918
919         [ "$SLOW" = "no" ] && LIMIT=5
920
921         setup_quota_test
922         trap cleanup_quota_test EXIT
923
924         # make sure the system is clean
925         local USED=$(getquota -u $TSTUSR global curspace)
926         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
927
928         # make sure no granted quota on ost1
929         set_ost_qtype "ug" || error "enable ost quota failed"
930         resetquota -u $TSTUSR
931         set_ost_qtype "none" || error "disable ost quota failed"
932
933         local OSTUUID=$(ostuuid_from_index 0)
934         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
935         [ $USED -ne 0 ] && error "limit($USED) on $OSTUUID for user" \
936                 "$TSTUSR isn't 0."
937
938         # create test file
939         $LFS setstripe $TESTFILE -c 1 -i 0
940         chown $TSTUSR.$TSTUSR $TESTFILE
941
942         echo "Stop ost1..."
943         stop ost1
944
945         echo "Enable quota & set quota limit for $TSTUSR"
946         set_ost_qtype "ug" || error "enable ost quota failed"
947         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR
948
949         echo "Start ost1..."
950         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
951
952         wait_ost_reint "ug" || error "reintegration failed"
953
954         # hardlimit should have been fetched by slave during global
955         # reintegration, write will exceed quota
956         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
957                 quota_error u $TSTUSR "write success, but expect EDQUOT"
958
959         cleanup_quota_test
960         resetquota -u $TSTUSR
961 }
962 run_test 7a "Quota reintegration (global index)"
963
964 # quota reintegration (slave index)
965 test_7b() {
966         local LIMIT="100G"
967         local TESTFILE=$DIR/$tdir/$tfile
968
969         setup_quota_test
970         trap cleanup_quota_test EXIT
971
972         # make sure the system is clean
973         local USED=$(getquota -u $TSTUSR global curspace)
974         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
975
976         # make sure no granted quota on ost1
977         set_ost_qtype "ug" || error "enable ost quota failed"
978         resetquota -u $TSTUSR
979         set_ost_qtype "none" || error "disable ost quota failed"
980
981         local OSTUUID=$(ostuuid_from_index 0)
982         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
983         [ $USED -ne 0 ] && error "limit($USED) on $OSTUUID for user" \
984                 "$TSTUSR isn't 0."
985
986         # create test file
987         $LFS setstripe $TESTFILE -c 1 -i 0
988         chown $TSTUSR.$TSTUSR $TESTFILE
989
990         # consume some space to make sure the granted space will not
991         # be released during reconciliation
992         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
993                 error "consume space failure, expect success"
994
995         # define OBD_FAIL_QUOTA_EDQUOT 0xa02
996         lustre_fail mds 0xa02
997
998         set_ost_qtype "ug" || error "enable ost quota failed"
999         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
1000
1001         # ignore the write error
1002         $RUNAS $DD of=$TESTFILE count=1 seek=1 oflag=sync conv=notrunc
1003
1004         local old_used=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1005
1006         lustre_fail mds 0
1007
1008         echo "Restart ost to trigger reintegration..."
1009         stop ost1
1010         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
1011
1012         wait_ost_reint "ug" || error "reintegration failed"
1013
1014         USED=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1015         [ $USED -gt $old_used ] || error "limit on $OSTUUID $USED <= $old_used"
1016
1017         cleanup_quota_test
1018         resetquota -u $TSTUSR
1019         $SHOW_QUOTA_USER
1020 }
1021 run_test 7b "Quota reintegration (slave index)"
1022
1023 # quota reintegration (restart mds during reintegration)
1024 test_7c() {
1025         local LIMIT=20 # 20M
1026         local TESTFILE=$DIR/$tdir/$tfile
1027
1028         [ "$SLOW" = "no" ] && LIMIT=5
1029
1030         setup_quota_test
1031         trap cleanup_quota_test EXIT
1032
1033         # make sure the system is clean
1034         local USED=$(getquota -u $TSTUSR global curspace)
1035         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1036
1037         set_ost_qtype "none" || error "disable ost quota failed"
1038         $LFS setquota -u $TSTUSR -b 0 -B ${LIMIT}M -i 0 -I 0 $DIR
1039
1040         # define OBD_FAIL_QUOTA_DELAY_REINT 0xa03
1041         lustre_fail ost 0xa03
1042
1043         # enable ost quota to trigger reintegration
1044         set_ost_qtype "ug" || error "enable ost quota failed"
1045
1046         echo "Stop mds..."
1047         stop mds1
1048
1049         lustre_fail ost 0
1050
1051         echo "Start mds..."
1052         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
1053
1054         wait_ost_reint "ug" || error "reintegration failed"
1055
1056         # hardlimit should have been fetched by slave during global
1057         # reintegration, write will exceed quota
1058         $RUNAS $DD of=$TESTFILE count=$((LIMIT + 1)) oflag=sync &&
1059                 quota_error u $TSTUSR "write success, but expect EDQUOT"
1060
1061         cleanup_quota_test
1062         resetquota -u $TSTUSR
1063 }
1064 run_test 7c "Quota reintegration (restart mds during reintegration)"
1065
1066 # run dbench with quota enabled
1067 test_8() {
1068         local BLK_LIMIT="100g" #100G
1069         local FILE_LIMIT=1000000
1070
1071         setup_quota_test
1072         trap cleanup_quota_test EXIT
1073
1074         set_mdt_qtype "ug" || error "enable mdt quota failed"
1075         set_ost_qtype "ug" || error "enable ost quota failed"
1076
1077         echo "Set enough high limit for user: $TSTUSR"
1078         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
1079         echo "Set enough high limit for group: $TSTUSR"
1080         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
1081
1082         local duration=""
1083         [ "$SLOW" = "no" ] && duration=" -t 120"
1084         $RUNAS bash rundbench -D $DIR/$tdir 3 $duration ||
1085                 quota_error a $TSTUSR "dbench failed!"
1086
1087         cleanup_quota_test
1088         resetquota -u $TSTUSR
1089         resetquota -g $TSTUSR
1090 }
1091 run_test 8 "Run dbench with quota enabled"
1092
1093 # this check is just for test_9
1094 OST0_MIN=4900000 #4.67G
1095
1096 check_whether_skip () {
1097         local OST0_SIZE=$($LFS df $DIR | awk '/\[OST:0\]/ {print $4}')
1098         log "OST0_SIZE: $OST0_SIZE  required: $OST0_MIN"
1099         if [ $OST0_SIZE -lt $OST0_MIN ]; then
1100                 echo "WARN: OST0 has less than $OST0_MIN free, skip this test."
1101                 return 0
1102         else
1103                 return 1
1104         fi
1105 }
1106
1107 # run for fixing bug10707, it needs a big room. test for 64bit
1108 test_9() {
1109         local filesize=$((1024 * 1024 * 1024 * 9 / 2)) # 4.5G
1110
1111         check_whether_skip && return 0
1112
1113         setup_quota_test
1114         trap cleanup_quota_test EXIT
1115
1116         set_ost_qtype "ug" || error "enable ost quota failed"
1117
1118         local TESTFILE="$DIR/$tdir/$tfile-0"
1119         local BLK_LIMIT=100G #100G
1120         local FILE_LIMIT=1000000
1121
1122         echo "Set block limit $BLK_LIMIT bytes to $TSTUSR.$TSTUSR"
1123
1124         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1125                 "for user: $TSTUSR"
1126         $LFS setquota -u $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
1127
1128         log "Set enough high limit(block:$BLK_LIMIT; file: $FILE_LIMIT)" \
1129                 "for group: $TSTUSR"
1130         $LFS setquota -g $TSTUSR -b 0 -B $BLK_LIMIT -i 0 -I $FILE_LIMIT $DIR
1131
1132         quota_show_check a u $TSTUSR
1133         quota_show_check a g $TSTUSR
1134
1135         echo "Create test file"
1136         $LFS setstripe $TESTFILE -c 1 -i 0
1137         chown $TSTUSR.$TSTUSR $TESTFILE
1138
1139         log "Write the big file of 4.5G ..."
1140         $RUNAS $DD of=$TESTFILE count=$filesize ||
1141                 quota_error a $TSTUSR "write 4.5G file failure, expect success"
1142
1143         $SHOW_QUOTA_USER
1144         $SHOW_QUOTA_GROUP
1145
1146         cleanup_quota_test
1147         resetquota -u $TSTUSR
1148         resetquota -g $TSTUSR
1149
1150         $SHOW_QUOTA_USER
1151         $SHOW_QUOTA_GROUP
1152 }
1153 run_test 9 "Block limit larger than 4GB (b10707)"
1154
1155 test_10() {
1156         local TESTFILE=$DIR/$tdir/$tfile
1157
1158         setup_quota_test
1159         trap cleanup_quota_test EXIT
1160
1161         # set limit to root user should fail
1162         $LFS setquota -u root -b 100G -B 500G -i 1K -I 1M $DIR &&
1163                 error "set limit for root user successfully, expect failure"
1164         $LFS setquota -g root -b 1T -B 10T -i 5K -I 100M $DIR &&
1165                 error "set limit for root group successfully, expect failure"
1166
1167         # root user can overrun quota
1168         set_ost_qtype "ug" || "enable ost quota failed"
1169
1170         $LFS setquota -u $TSTUSR -b 0 -B 2M -i 0 -I 0 $DIR
1171         quota_show_check b u $TSTUSR
1172
1173         $LFS setstripe $TESTFILE -c 1
1174         chown $TSTUSR.$TSTUSR $TESTFILE
1175
1176         runas -u 0 -g 0 $DD of=$TESTFILE count=3 oflag=sync ||
1177                 error "write failure, expect success"
1178
1179         cleanup_quota_test
1180         resetquota -u $TSTUSR
1181 }
1182 run_test 10 "Test quota for root user"
1183
1184 test_11() {
1185         local TESTFILE=$DIR/$tdir/$tfile
1186         setup_quota_test
1187         trap cleanup_quota_test EXIT
1188
1189         set_mdt_qtype "ug" || "enable mdt quota failed"
1190         $LFS setquota -u $TSTUSR -b 0 -B 0 -i 0 -I 1 $DIR
1191
1192         touch "$TESTFILE"-0
1193         touch "$TESTFILE"-1
1194
1195         chown $TSTUSR.$TSTUSR "$TESTFILE"-0
1196         chown $TSTUSR.$TSTUSR "$TESTFILE"-1
1197
1198         $SHOW_QUOTA_USER
1199         local USED=$(getquota -u $TSTUSR global curinodes)
1200         [ $USED -ge 2 ] || error "Used inodes($USED) is less than 2"
1201
1202         cleanup_quota_test
1203         resetquota -u $TSTUSR
1204 }
1205 run_test 11 "Chown/chgrp ignores quota"
1206
1207 test_12() {
1208         [ "$OSTCOUNT" -lt "2" ] && skip "skipping rebalancing test" && return
1209
1210         local blimit=22 # 22M
1211         local blk_cnt=$((blimit - 5))
1212         local TESTFILE0="$DIR/$tdir/$tfile"-0
1213         local TESTFILE1="$DIR/$tdir/$tfile"-1
1214
1215         setup_quota_test
1216         trap cleanup_quota_test EXIT
1217
1218         set_ost_qtype "u" || "enable ost quota failed"
1219         quota_show_check b u $TSTUSR
1220
1221         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $DIR
1222
1223         $LFS setstripe $TESTFILE0 -c 1 -i 0
1224         $LFS setstripe $TESTFILE1 -c 1 -i 1
1225         chown $TSTUSR.$TSTUSR $TESTFILE0
1226         chown $TSTUSR.$TSTUSR $TESTFILE1
1227
1228         echo "Write to ost0..."
1229         $RUNAS $DD of=$TESTFILE0 count=$blk_cnt oflag=sync ||
1230                 quota_error a $TSTUSR "dd failed"
1231
1232         echo "Write to ost1..."
1233         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync &&
1234                 quota_error a $TSTUSR "dd succeed, expect EDQUOT"
1235
1236         echo "Free space from ost0..."
1237         rm -f $TESTFILE0
1238         wait_delete_completed
1239         sync_all_data || true
1240
1241         echo "Write to ost1 after space freed from ost0..."
1242         $RUNAS $DD of=$TESTFILE1 count=$blk_cnt oflag=sync ||
1243                 quota_error a $TSTUSR "rebalancing failed"
1244
1245         cleanup_quota_test
1246         resetquota -u $TSTUSR
1247 }
1248 run_test 12 "Block quota rebalancing"
1249
1250 test_13(){
1251         local TESTFILE=$DIR/$tdir/$tfile
1252         # the name of osp on ost1 name is MDT0000-osp-OST0000
1253         local procf="ldlm.namespaces.*MDT0000-osp-OST0000.lru_size"
1254
1255         setup_quota_test
1256         trap cleanup_quota_test EXIT
1257
1258         set_ost_qtype "u" || "enable ost quota failed"
1259         quota_show_check b u $TSTUSR
1260
1261         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR
1262         $LFS setstripe $TESTFILE -c 1 -i 0
1263         chown $TSTUSR.$TSTUSR $TESTFILE
1264
1265         # clear the locks in cache first
1266         do_facet ost1 $LCTL set_param -n $procf=clear
1267         local nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1268         [ $nlock -eq 0 ] || error "$nlock cached locks"
1269
1270         # write to acquire the per-ID lock
1271         $RUNAS $DD of=$TESTFILE count=1 oflag=sync ||
1272                 quota_error a $TSTUSR "dd failed"
1273
1274         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1275         [ $nlock -eq 1 ] || error "lock count($nlock) isn't 1"
1276
1277         # clear quota doesn't trigger per-ID lock cancellation
1278         resetquota -u $TSTUSR
1279         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1280         [ $nlock -eq 1 ] || error "per-ID lock is lost on quota clear"
1281
1282         # clear the per-ID lock
1283         do_facet ost1 $LCTL set_param -n $procf=clear
1284         nlock=$(do_facet ost1 $LCTL get_param -n $procf)
1285         [ $nlock -eq 0 ] || error "per-ID lock isn't cleared"
1286
1287         # spare quota should be released
1288         local OSTUUID=$(ostuuid_from_index 0)
1289         local limit=$(getquota -u $TSTUSR $OSTUUID bhardlimit)
1290         local space=$(getquota -u $TSTUSR $OSTUUID curspace)
1291         [ $limit -le $space ] ||
1292                 error "spare quota isn't released, limit:$limit, space:$space"
1293
1294         cleanup_quota_test
1295 }
1296 run_test 13 "Cancel per-ID lock in the LRU list"
1297
1298 test_15(){
1299         local LIMIT=$((24 * 1024 * 1024 * 1024 * 1024)) # 24 TB
1300
1301         wait_delete_completed
1302         sync_all_data || true
1303
1304         # test for user
1305         $LFS setquota -u $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
1306         local TOTAL_LIMIT=$(getquota -u $TSTUSR global bhardlimit)
1307         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1308                 error "(user) limit:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1309         resetquota -u $TSTUSR
1310
1311         # test for group
1312         $LFS setquota -g $TSTUSR -b 0 -B $LIMIT -i 0 -I 0 $DIR
1313         TOTAL_LIMIT=$(getquota -g $TSTUSR global bhardlimit)
1314         [ $TOTAL_LIMIT -eq $LIMIT ] ||
1315                 error "(group) limits:$TOTAL_LIMIT, expect:$LIMIT, failed!"
1316         resetquota -g $TSTUSR
1317 }
1318 run_test 15 "Set over 4T block quota"
1319
1320 test_17sub() {
1321         local err_code=$1
1322         local BLKS=1    # 1M less than limit
1323         local TESTFILE=$DIR/$tdir/$tfile
1324
1325         setup_quota_test
1326         trap cleanup_quota_test EXIT
1327
1328         # make sure the system is clean
1329         local USED=$(getquota -u $TSTUSR global curspace)
1330         [ $USED -ne 0 ] && error "Used space($USED) for user $TSTUSR isn't 0."
1331
1332         set_ost_qtype "ug" || error "enable ost quota failed"
1333         # make sure no granted quota on ost
1334         resetquota -u $TSTUSR
1335         $LFS setquota -u $TSTUSR -b 0 -B 10M -i 0 -I 0 $DIR
1336
1337         quota_show_check b u $TSTUSR
1338
1339         #define OBD_FAIL_QUOTA_RECOVERABLE_ERR 0xa04
1340         lustre_fail mds 0xa04 $err_code
1341
1342         # write in background
1343         $RUNAS $DD of=$TESTFILE count=$BLKS oflag=direct &
1344         local DDPID=$!
1345
1346         sleep 2
1347         # write should be blocked and never finished
1348         if ! ps -p $DDPID  > /dev/null 2>&1; then
1349                 lustre_fail mds 0 0
1350                 quota_error u $TSTUSR "write finished incorrectly!"
1351         fi
1352
1353         lustre_fail mds 0 0
1354
1355         local count=0
1356         local timeout=30
1357         while [ true ]; do
1358                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1359                 count=$((count+1))
1360                 if [ $count -gt $timeout ]; then
1361                         quota_error u $TSTUSR "dd is not finished!"
1362                 fi
1363                 sleep 1
1364         done
1365
1366         sync; sync_all_data || true
1367
1368         USED=$(getquota -u $TSTUSR global curspace)
1369         [ $USED -ge $(($BLKS * 1024)) ] || quota_error u $TSTUSR \
1370                 "Used space(${USED}K) is less than ${BLKS}M"
1371
1372         cleanup_quota_test
1373         resetquota -u $TSTUSR
1374 }
1375
1376 # DQACQ return recoverable error
1377 test_17() {
1378         echo "DQACQ return -ENOLCK"
1379         #define ENOLCK  37
1380         test_17sub 37 || error "Handle -ENOLCK failed"
1381
1382         echo "DQACQ return -EAGAIN"
1383         #define EAGAIN  11
1384         test_17sub 11 || error "Handle -EAGAIN failed"
1385
1386         echo "DQACQ return -ETIMEDOUT"
1387         #define ETIMEDOUT 110
1388         test_17sub 110 || error "Handle -ETIMEDOUT failed"
1389
1390         echo "DQACQ return -ENOTCONN"
1391         #define ENOTCONN 107
1392         test_17sub 107 || error "Handle -ENOTCONN failed"
1393 }
1394
1395 run_test 17 "DQACQ return recoverable error"
1396
1397 test_18_sub () {
1398         local io_type=$1
1399         local blimit="200m" # 200M
1400         local TESTFILE="$DIR/$tdir/$tfile"
1401
1402         setup_quota_test
1403         trap cleanup_quota_test EXIT
1404
1405         set_ost_qtype "u" || error "enable ost quota failed"
1406         log "User quota (limit: $blimit)"
1407         $LFS setquota -u $TSTUSR -b 0 -B $blimit -i 0 -I 0 $MOUNT
1408         quota_show_check b u $TSTUSR
1409
1410         $LFS setstripe $TESTFILE -i 0 -c 1
1411         chown $TSTUSR.$TSTUSR $TESTFILE
1412
1413         local timeout=$(sysctl -n lustre.timeout)
1414
1415         if [ $io_type = "directio" ]; then
1416                 log "Write 100M (directio) ..."
1417                 $RUNAS $DD of=$TESTFILE count=100 oflag=direct &
1418         else
1419                 log "Write 100M (buffered) ..."
1420                 $RUNAS $DD of=$TESTFILE count=100 &
1421         fi
1422         local DDPID=$!
1423
1424         replay_barrier $SINGLEMDS
1425         log "Fail mds for $((2 * timeout)) seconds"
1426         fail $SINGLEMDS $((2 * timeout))
1427
1428         local count=0
1429         if at_is_enabled; then
1430                 timeout=$(at_max_get mds)
1431         else
1432                 timeout=$(lctl get_param -n timeout)
1433         fi
1434
1435         while [ true ]; do
1436                 if ! ps -p ${DDPID} > /dev/null 2>&1; then break; fi
1437                 if [ $((++count % (2 * timeout) )) -eq 0 ]; then
1438                         log "it took $count second"
1439                 fi
1440                 sleep 1
1441         done
1442
1443         log "(dd_pid=$DDPID, time=$count, timeout=$timeout)"
1444         sync
1445         cancel_lru_locks mdc
1446         cancel_lru_locks osc
1447         $SHOW_QUOTA_USER
1448
1449         local testfile_size=$(stat -c %s $TESTFILE)
1450         if [ $testfile_size -ne $((BLK_SZ * 1024 * 100)) ] ; then
1451                 quota_error u $TSTUSR "expect $((BLK_SZ * 1024 * 100)),"
1452                         "got ${testfile_size}. Verifying file failed!"
1453         fi
1454         cleanup_quota_test
1455         resetquota -u $TSTUSR
1456 }
1457
1458 # test when mds does failover, the ost still could work well
1459 # this test shouldn't trigger watchdog b=14840
1460 test_18() {
1461         test_18_sub normal
1462         test_18_sub directio
1463
1464         # check if watchdog is triggered
1465         do_facet ost1 dmesg > $TMP/lustre-log-${TESTNAME}.log
1466         local watchdog=$(awk '/test 18b/ {start = 1;}
1467                        /Service thread pid/ && /was inactive/ {
1468                                if (start) {
1469                                        print;
1470                                }
1471                        }' $TMP/lustre-log-${TESTNAME}.log)
1472         [ -z "$watchdog" ] || error "$watchdog"
1473         rm -f $TMP/lustre-log-${TESTNAME}.log
1474 }
1475 run_test 18 "MDS failover while writing, no watchdog triggered (b14840)"
1476
1477 test_19() {
1478         local blimit=5 # 5M
1479         local TESTFILE=$DIR/$tdir/$tfile
1480
1481         setup_quota_test
1482         trap cleanup_quota_test EXIT
1483
1484         set_ost_qtype "ug" || error "enable ost quota failed"
1485
1486         # bind file to a single OST
1487         $LFS setstripe -c 1 $TESTFILE
1488         chown $TSTUSR.$TSTUSR $TESTFILE
1489
1490         echo "Set user quota (limit: "$blimit"M)"
1491         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT
1492         quota_show_check b u $TSTUSR
1493         echo "Update quota limits"
1494         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT
1495         quota_show_check b u $TSTUSR
1496
1497         # first wirte might be cached
1498         $RUNAS $DD of=$TESTFILE count=$((blimit + 1))
1499         cancel_lru_locks osc
1500         $SHOW_QUOTA_USER
1501         $RUNAS $DD of=$TESTFILE count=$((blimit + 1)) seek=$((blimit + 1)) &&
1502                 quota_error u $TSTUSR "Write success, expect failure"
1503         $SHOW_QUOTA_USER
1504
1505         cleanup_quota_test
1506         resetquota -u $TSTUSR
1507 }
1508 run_test 19 "Updating admin limits doesn't zero operational limits(b14790)"
1509
1510 test_20() { # b15754
1511         local LSTR=(2g 1t 4k 3m) # limits strings
1512         # limits values
1513         local LVAL=($[2*1024*1024] $[1*1024*1024*1024] $[4*1024] $[3*1024*1024])
1514
1515         resetquota -u $TSTUSR
1516
1517         $LFS setquota -u $TSTUSR --block-softlimit ${LSTR[0]} \
1518                 $MOUNT || error "could not set quota limits"
1519         $LFS setquota -u $TSTUSR --block-hardlimit ${LSTR[1]} \
1520                                 --inode-softlimit ${LSTR[2]} \
1521                                 --inode-hardlimit ${LSTR[3]} \
1522                                 $MOUNT || error "could not set quota limits"
1523
1524         [ "$(getquota -u $TSTUSR global bsoftlimit)" = "${LVAL[0]}" ] ||
1525                 error "bsoftlimit was not set properly"
1526         [ "$(getquota -u $TSTUSR global bhardlimit)" = "${LVAL[1]}" ] ||
1527                 error "bhardlimit was not set properly"
1528         [ "$(getquota -u $TSTUSR global isoftlimit)" = "${LVAL[2]}" ] ||
1529                 error "isoftlimit was not set properly"
1530         [ "$(getquota -u $TSTUSR global ihardlimit)" = "${LVAL[3]}" ] ||
1531                 error "ihardlimit was not set properly"
1532
1533         resetquota -u $TSTUSR
1534 }
1535 run_test 20 "Test if setquota specifiers work properly (b15754)"
1536
1537 test_21_sub() {
1538         local testfile=$1
1539         local blk_number=$2
1540         local seconds=$3
1541
1542         local time=$(($(date +%s) + seconds))
1543         while [ $(date +%s) -lt $time ]; do
1544                 $RUNAS $DD of=$testfile count=$blk_number > /dev/null 2>&1
1545         done
1546 }
1547
1548 # run for fixing bug16053, setquota shouldn't fail when writing and
1549 # deleting are happening
1550 test_21() {
1551         local TESTFILE="$DIR/$tdir/$tfile"
1552         local BLIMIT=10 # 10G
1553         local ILIMIT=1000000
1554
1555         setup_quota_test
1556         trap cleanup_quota_test EXIT
1557
1558         set_ost_qtype "ug" || error "Enable ost quota failed"
1559
1560         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for user: $TSTUSR"
1561         $LFS setquota -u $TSTUSR -b 0 -B ${BLIMIT}G -i 0 -I $ILIMIT $MOUNT
1562         log "Set limit(block:${BLIMIT}G; file:$ILIMIT) for group: $TSTUSR"
1563         $LFS setquota -g $TSTUSR -b 0 -B $BLIMIT -i 0 -I $ILIMIT $MOUNT
1564
1565         # repeat writing on a 1M file
1566         test_21_sub ${TESTFILE}_1 1 30 &
1567         local DDPID1=$!
1568         # repeat writing on a 128M file
1569         test_21_sub ${TESTFILE}_2 128 30 &
1570         local DDPID2=$!
1571
1572         local time=$(($(date +%s) + 30))
1573         local i=1
1574         while [ $(date +%s) -lt $time ]; do
1575                 log "Set quota for $i times"
1576                 $LFS setquota -u $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
1577                         -I $((ILIMIT + i)) $MOUNT ||
1578                                 error "Set user quota failed"
1579                 $LFS setquota -g $TSTUSR -b 0 -B "$((BLIMIT + i))G" -i 0 \
1580                         -I $((ILIMIT + i)) $MOUNT ||
1581                                 error "Set group quota failed"
1582                 i=$((i+1))
1583                 sleep 1
1584         done
1585
1586         local count=0
1587         while [ true ]; do
1588                 if ! ps -p ${DDPID1} > /dev/null 2>&1; then break; fi
1589                 count=$((count+1))
1590                 if [ $count -gt 60 ]; then
1591                         quota_error a $TSTUSR "dd should be finished!"
1592                 fi
1593                 sleep 1
1594         done
1595         echo "(dd_pid=$DDPID1, time=$count)successful"
1596
1597         count=0
1598         while [ true ]; do
1599                 if ! ps -p ${DDPID2} > /dev/null 2>&1; then break; fi
1600                 count=$((count+1))
1601                 if [ $count -gt 60 ]; then
1602                         quota_error a $TSTUSR "dd should be finished!"
1603                 fi
1604                 sleep 1
1605         done
1606         echo "(dd_pid=$DDPID2, time=$count)successful"
1607
1608         cleanup_quota_test
1609         resetquota -u $TSTUSR
1610         resetquota -g $TSTUSR
1611 }
1612 run_test 21 "Setquota while writing & deleting (b16053)"
1613
1614 # enable/disable quota enforcement permanently
1615 test_22() {
1616         echo "Set both mdt & ost quota type as ug"
1617         set_mdt_qtype "ug" || error "enable mdt quota failed"
1618         set_ost_qtype "ug" || error "enable ost quota failed"
1619
1620         echo "Restart..."
1621         stopall
1622         mount
1623         setupall
1624
1625         echo "Verify if quota is enabled"
1626         local qtype=$(mdt_quota_type)
1627         [ $qtype != "ug" ] && error "mdt quota setting is lost"
1628         qtype=$(ost_quota_type)
1629         [ $qtype != "ug" ] && error "ost quota setting is lost"
1630
1631         echo "Set both mdt & ost quota type as none"
1632         set_mdt_qtype "none" || error "disable mdt quota failed"
1633         set_ost_qtype "none" || error "disable ost quota failed"
1634
1635         echo "Restart..."
1636         stopall
1637         mount
1638         setupall
1639
1640         echo "Verify if quota is disabled"
1641         qtype=$(mdt_quota_type)
1642         [ $qtype != "none" ] && error "mdt quota setting is lost"
1643         qtype=$(ost_quota_type)
1644         [ $qtype != "none" ] && error "ost quota setting is lost"
1645
1646         quota_init
1647 }
1648 run_test 22 "enable/disable quota by 'lctl conf_param'"
1649
1650 test_23_sub() {
1651         local TESTFILE="$DIR/$tdir/$tfile"
1652         local LIMIT=$1
1653
1654         setup_quota_test
1655         trap cleanup_quota_test EXIT
1656
1657         set_ost_qtype "ug" || error "Enable ost quota failed"
1658
1659         # test for user
1660         log "User quota (limit: $LIMIT MB)"
1661         $LFS setquota -u $TSTUSR -b 0 -B "$LIMIT"M -i 0 -I 0 $DIR
1662         quota_show_check b u $TSTUSR
1663
1664         $LFS setstripe $TESTFILE -c 1 -i 0
1665         chown $TSTUSR.$TSTUSR $TESTFILE
1666
1667         log "Step1: trigger EDQUOT with O_DIRECT"
1668         log "Write half of file"
1669         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2)) oflag=direct ||
1670                 quota_error u $TSTUSR "(1) Write failure, expect success." \
1671                         "limit=$LIMIT"
1672         log "Write out of block quota ..."
1673         $RUNAS $DD of=$TESTFILE count=$((LIMIT/2 + 1)) seek=$((LIMIT/2)) \
1674                 oflag=direct conv=notrunc &&
1675                 quota_error u $TSTUSR "(2) Write success, expect EDQUOT." \
1676                         "limit=$LIMIT"
1677         log "Step1: done"
1678
1679         log "Step2: rewrite should succeed"
1680         $RUNAS $DD of=$TESTFILE count=1 oflag=direct conv=notrunc||
1681                 quota_error u $TSTUSR "(3) Write failure, expect success." \
1682                         "limit=$LIMIT"
1683         log "Step2: done"
1684
1685         cleanup_quota_test
1686
1687         local OST0_UUID=$(ostuuid_from_index 0)
1688         local OST0_QUOTA_USED=$(getquota -u $TSTUSR $OST0_UUID curspace)
1689         [ $OST0_QUOTA_USED -ne 0 ] &&
1690                 ($SHOW_QUOTA_USER; \
1691                 quota_error u $TSTUSR "quota isn't released")
1692         $SHOW_QUOTA_USER
1693         resetquota -u $TSTUSR
1694 }
1695
1696 test_23() {
1697         local OST0_MIN=$((6 * 1024)) # 6MB, extra space for meta blocks.
1698         check_whether_skip && return 0
1699         log "run for 4MB test file"
1700         test_23_sub 4
1701
1702         OST0_MIN=$((60 * 1024)) # 60MB, extra space for meta blocks.
1703         check_whether_skip && return 0
1704         log "run for 40MB test file"
1705         test_23_sub 40
1706 }
1707 run_test 23 "Quota should be honored with directIO (b16125)"
1708
1709 test_24() {
1710         local blimit=5 # 5M
1711         local TESTFILE="$DIR/$tdir/$tfile"
1712
1713         setup_quota_test
1714         trap cleanup_quota_test EXIT
1715
1716         set_ost_qtype "ug" || error "enable ost quota failed"
1717
1718         # bind file to a single OST
1719         $LFS setstripe -c 1 $TESTFILE
1720         chown $TSTUSR.$TSTUSR $TESTFILE
1721
1722         echo "Set user quota (limit: "$blimit"M)"
1723         $LFS setquota -u $TSTUSR -b 0 -B "$blimit"M -i 0 -I 0 $MOUNT
1724
1725         # overrun quota by root user
1726         runas -u 0 -g 0 $DD of=$TESTFILE count=$((blimit + 1)) ||
1727                 error "write failure, expect success"
1728         cancel_lru_locks osc
1729         sync_all_data || true
1730
1731         $SHOW_QUOTA_USER | grep '*' || error "no matching *"
1732
1733         cleanup_quota_test
1734         resetquota -u $TSTUSR
1735 }
1736 run_test 24 "lfs draws an asterix when limit is reached (b16646)"
1737
1738 test_27a() { # b19612
1739         $LFS quota $TSTUSR $DIR &&
1740                 error "lfs succeeded with no type, but should have failed"
1741         $LFS setquota $TSTUSR $DIR &&
1742                 error "lfs succeeded with no type, but should have failed"
1743         return 0
1744 }
1745 run_test 27a "lfs quota/setquota should handle wrong arguments (b19612)"
1746
1747 test_27b() { # b20200
1748         $LFS setquota -u $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
1749                 error "lfs setquota failed with uid argument"
1750         $LFS setquota -g $TSTID -b 1000 -B 1000 -i 1000 -I 1000 $DIR ||
1751                 error "lfs stequota failed with gid argument"
1752         $SHOW_QUOTA_USERID || error "lfs quota failed with uid argument"
1753         $SHOW_QUOTA_GROUPID || error "lfs quota failed with gid argument"
1754         resetquota -u $TSTUSR
1755         resetquota -g $TSTUSR
1756         return 0
1757 }
1758 run_test 27b "lfs quota/setquota should handle user/group ID (b20200)"
1759
1760 test_30() {
1761         local output
1762         local LIMIT=4 # 4MB
1763         local TESTFILE="$DIR/$tdir/$tfile"
1764         local GRACE=10
1765
1766         setup_quota_test
1767         trap cleanup_quota_test EXIT
1768
1769         set_ost_qtype "u" || error "enable ost quota failed"
1770
1771         $LFS setstripe $TESTFILE -i 0 -c 1
1772         chown $TSTUSR.$TSTUSR $TESTFILE
1773
1774         $LFS setquota -t -u --block-grace $GRACE --inode-grace $MAX_IQ_TIME $DIR
1775         $LFS setquota -u $TSTUSR -b ${LIMIT}M -B 0 -i 0 -I 0 $DIR
1776         $RUNAS $DD of=$TESTFILE count=$((LIMIT * 2)) || true
1777         cancel_lru_locks osc
1778         sleep $GRACE
1779         $LFS setquota -u $TSTUSR -B 0 $DIR
1780         # over-quota flag has not yet settled since we do not trigger async
1781         # events based on grace time period expiration
1782         $SHOW_QUOTA_USER
1783         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=1 || true
1784         cancel_lru_locks osc
1785         # now over-quota flag should be settled and further writes should fail
1786         $SHOW_QUOTA_USER
1787         $RUNAS $DD of=$TESTFILE conv=notrunc oflag=append count=1 &&
1788                 error "grace times were reset"
1789         # cleanup
1790         cleanup_quota_test
1791         resetquota -u $TSTUSR
1792         $LFS setquota -t -u --block-grace $MAX_DQ_TIME --inode-grace \
1793                 $MAX_IQ_TIME $DIR
1794 }
1795 run_test 30 "Hard limit updates should not reset grace times"
1796
1797 # basic usage tracking for user & group
1798 test_33() {
1799         local INODES=10 # 10 files
1800         local BLK_CNT=2 # of 2M each
1801         local TOTAL_BLKS=$((INODES * BLK_CNT * 1024))
1802
1803         setup_quota_test
1804         trap cleanup_quota_test EXIT
1805
1806         # make sure the system is clean
1807         local USED=$(getquota -u $TSTID global curspace)
1808         [ $USED -ne 0 ] &&
1809                 error "Used space ($USED) for user $TSTID isn't 0."
1810         USED=$(getquota -g $TSTID global curspace)
1811         [ $USED -ne 0 ] &&
1812                 error "Used space ($USED) for group $TSTID isn't 0."
1813
1814         echo "Write files..."
1815         for i in `seq 0 $INODES`; do
1816                 $RUNAS $DD of=$DIR/$tdir/$tfile-$i count=$BLK_CNT 2>/dev/null ||
1817                         error "write failed"
1818                 echo "Iteration $i/$INODES completed"
1819         done
1820         cancel_lru_locks osc
1821         sync; sync_all_data || true
1822
1823         echo "Verify disk usage after write"
1824         USED=$(getquota -u $TSTID global curspace)
1825         [ $USED -lt $TOTAL_BLKS ] &&
1826                 error "Used space for user $TSTID:$USED, expected:$TOTAL_BLKS"
1827         USED=$(getquota -g $TSTID global curspace)
1828         [ $USED -lt $TOTAL_BLKS ] &&
1829                 error "Used space for group $TSTID:$USED, expected:$TOTAL_BLKS"
1830
1831         echo "Verify inode usage after write"
1832         USED=$(getquota -u $TSTID global curinodes)
1833         [ $USED -lt $INODES ] &&
1834                 error "Used inode for user $TSTID is $USED, expected $INODES"
1835         USED=$(getquota -g $TSTID global curinodes)
1836         [ $USED -lt $INODES ] &&
1837                 error "Used inode for group $TSTID is $USED, expected $INODES"
1838
1839         cleanup_quota_test
1840
1841         echo "Verify disk usage after delete"
1842         USED=$(getquota -u $TSTID global curspace)
1843         [ $USED -eq 0 ] || error "Used space for user $TSTID isn't 0. $USED"
1844         USED=$(getquota -u $TSTID global curinodes)
1845         [ $USED -eq 0 ] || error "Used inodes for user $TSTID isn't 0. $USED"
1846         USED=$(getquota -g $TSTID global curspace)
1847         [ $USED -eq 0 ] || error "Used space for group $TSTID isn't 0. $USED"
1848         USED=$(getquota -g $TSTID global curinodes)
1849         [ $USED -eq 0 ] || error "Used inodes for group $TSTID isn't 0. $USED"
1850 }
1851 run_test 33 "Basic usage tracking for user & group"
1852
1853 # usage transfer test for user & group
1854 test_34() {
1855         local BLK_CNT=2 # 2MB
1856
1857         setup_quota_test
1858         trap cleanup_quota_test EXIT
1859
1860         # make sure the system is clean
1861         local USED=$(getquota -u $TSTID global curspace)
1862         [ $USED -ne 0 ] && error "Used space ($USED) for user $TSTID isn't 0."
1863         USED=$(getquota -g $TSTID global curspace)
1864         [ $USED -ne 0 ] && error "Used space ($USED) for group $TSTID isn't 0."
1865
1866         echo "Write file..."
1867         $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
1868                 error "write failed"
1869         cancel_lru_locks osc
1870         sync; sync_all_data || true
1871
1872         echo "chown the file to user $TSTID"
1873         chown $TSTID $DIR/$tdir/$tfile || error "chown failed"
1874
1875         echo "Wait for setattr on objects finished..."
1876         wait_delete_completed
1877
1878         BLK_CNT=$((BLK_CNT * 1024))
1879
1880         echo "Verify disk usage for user $TSTID"
1881         USED=$(getquota -u $TSTID global curspace)
1882         [ $USED -lt $BLK_CNT ] &&
1883                 error "Used space for user $TSTID is ${USED}, expected $BLK_CNT"
1884         USED=$(getquota -u $TSTID global curinodes)
1885         [ $USED -ne 1 ] &&
1886                 error "Used inodes for user $TSTID is $USED, expected 1"
1887
1888         echo "chgrp the file to group $TSTID"
1889         chgrp $TSTID $DIR/$tdir/$tfile || error "chgrp failed"
1890
1891         echo "Wait for setattr on objects finished..."
1892         wait_delete_completed
1893
1894         echo "Verify disk usage for group $TSTID"
1895         USED=$(getquota -g $TSTID global curspace)
1896         [ $USED -ge $BLK_CNT ] ||
1897                 error "Used space for group $TSTID is $USED, expected $BLK_CNT"
1898         USED=$(getquota -g $TSTID global curinodes)
1899         [ $USED -eq 1 ] ||
1900                 error "Used inodes for group $TSTID is $USED, expected 1"
1901
1902         cleanup_quota_test
1903 }
1904 run_test 34 "Usage transfer for user & group"
1905
1906 # usage is still accessible across restart
1907 test_35() {
1908         local BLK_CNT=2 # 2 MB
1909
1910         setup_quota_test
1911         trap cleanup_quota_test EXIT
1912
1913         echo "Write file..."
1914         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT 2>/dev/null ||
1915                 error "write failed"
1916         cancel_lru_locks osc
1917         sync; sync_all_data || true
1918
1919         echo "Save disk usage before restart"
1920         local ORIG_USR_SPACE=$(getquota -u $TSTID global curspace)
1921         [ $ORIG_USR_SPACE -eq 0 ] &&
1922                 error "Used space for user $TSTID is 0, expected ${BLK_CNT}M"
1923         local ORIG_USR_INODES=$(getquota -u $TSTID global curinodes)
1924         [ $ORIG_USR_INODES -eq 0 ] &&
1925                 error "Used inodes for user $TSTID is 0, expected 1"
1926         echo "User $TSTID: ${ORIG_USR_SPACE}KB $ORIG_USR_INODES inodes"
1927         local ORIG_GRP_SPACE=$(getquota -g $TSTID global curspace)
1928         [ $ORIG_GRP_SPACE -eq 0 ] &&
1929                 error "Used space for group $TSTID is 0, expected ${BLK_CNT}M"
1930         local ORIG_GRP_INODES=$(getquota -g $TSTID global curinodes)
1931         [ $ORIG_GRP_INODES -eq 0 ] &&
1932                 error "Used inodes for group $TSTID is 0, expected 1"
1933         echo "Group $TSTID: ${ORIG_GRP_SPACE}KB $ORIG_GRP_INODES inodes"
1934
1935         log "Restart..."
1936         local ORIG_REFORMAT=$REFORMAT
1937         REFORMAT=""
1938         cleanup_and_setup_lustre
1939         REFORMAT=$ORIG_REFORMAT
1940         quota_init
1941
1942         echo "Verify disk usage after restart"
1943         local USED=$(getquota -u $TSTID global curspace)
1944         [ $USED -eq $ORIG_USR_SPACE ] ||
1945                 error "Used space for user $TSTID changed from " \
1946                         "$ORIG_USR_SPACE to $USED"
1947         USED=$(getquota -u $TSTID global curinodes)
1948         [ $USED -eq $ORIG_USR_INODES ] ||
1949                 error "Used inodes for user $TSTID changed from " \
1950                         "$ORIG_USR_INODES to $USED"
1951         USED=$(getquota -g $TSTID global curspace)
1952         [ $USED -eq $ORIG_GRP_SPACE ] ||
1953                 error "Used space for group $TSTID changed from " \
1954                         "$ORIG_GRP_SPACE to $USED"
1955         USED=$(getquota -g $TSTID global curinodes)
1956         [ $USED -eq $ORIG_GRP_INODES ] ||
1957                 error "Used inodes for group $TSTID changed from " \
1958                         "$ORIG_GRP_INODES to $USED"
1959
1960         # check if the vfs_dq_init() is called before writing
1961         echo "Append to the same file..."
1962         $RUNAS $DD of=$DIR/$tdir/$tfile count=$BLK_CNT seek=1 2>/dev/null ||
1963                 error "write failed"
1964         cancel_lru_locks osc
1965         sync; sync_all_data || true
1966
1967         echo "Verify space usage is increased"
1968         USED=$(getquota -u $TSTID global curspace)
1969         [ $USED -gt $ORIG_USR_SPACE ] ||
1970                 error "Used space for user $TSTID isn't increased" \
1971                         "orig:$ORIG_USR_SPACE, now:$USED"
1972         USED=$(getquota -g $TSTID global curspace)
1973         [ $USED -gt $ORIG_GRP_SPACE ] ||
1974                 error "Used space for group $TSTID isn't increased" \
1975                         "orig:$ORIG_GRP_SPACE, now:$USED"
1976
1977         cleanup_quota_test
1978 }
1979 run_test 35 "Usage is still accessible across reboot"
1980
1981 # test migrating old amdin quota files (in Linux quota file format v2) into new
1982 # quota global index (in IAM format)
1983 test_36() {
1984         [ $(facet_fstype $SINGLEMDS) != ldiskfs ] && \
1985                 skip "skipping migration test" && return
1986
1987         # get the mdt0 device name
1988         local mdt0_node=$(facet_active_host $SINGLEMDS)
1989         local mdt0_dev=$(mdsdevname ${SINGLEMDS//mds/})
1990
1991         echo "Reformat..."
1992         formatall
1993
1994         echo "Copy admin quota files into MDT0..."
1995         local mntpt=$(facet_mntpt $SINGLEMDS)
1996         local mdt0_fstype=$(facet_fstype $SINGLEMDS)
1997         local opt
1998         if ! do_node $mdt0_node test -b $mdt0_fstype; then
1999                 opt="-o loop"
2000         fi
2001         echo "$mdt0_node, $mdt0_dev, $mntpt, $opt"
2002         do_node $mdt0_node mount -t $mdt0_fstype $opt $mdt0_dev $mntpt
2003         do_node $mdt0_node mkdir $mntpt/OBJECTS
2004         do_node $mdt0_node cp $LUSTRE/tests/admin_quotafile_v2.usr $mntpt/OBJECTS
2005         do_node $mdt0_node cp $LUSTRE/tests/admin_quotafile_v2.grp $mntpt/OBJECTS
2006         do_node $mdt0_node umount -f $mntpt
2007
2008         echo "Setup all..."
2009         setupall
2010
2011         echo "Verify global limits..."
2012         local id_cnt
2013         local limit
2014
2015         local proc="qmt.*.md-0x0.glb-usr"
2016         id_cnt=$(do_node $mdt0_node $LCTL get_param -n $proc | wc -l)
2017         [ $id_cnt -eq 401 ] || error "Migrate inode user limit failed: $id_cnt"
2018         limit=$(getquota -u 1 global isoftlimit)
2019         [ $limit -eq 1024 ] || error "User inode softlimit: $limit"
2020         limit=$(getquota -u 1 global ihardlimit)
2021         [ $limit -eq 2048 ] || error "User inode hardlimit: $limit"
2022
2023         proc="qmt.*.md-0x0.glb-grp"
2024         id_cnt=$(do_node $mdt0_node $LCTL get_param -n $proc | wc -l)
2025         [ $id_cnt -eq 401 ] || error "Migrate inode group limit failed: $id_cnt"
2026         limit=$(getquota -g 1 global isoftlimit)
2027         [ $limit -eq 1024 ] || error "Group inode softlimit: $limit"
2028         limit=$(getquota -g 1 global ihardlimit)
2029         [ $limit -eq 2048 ] || error "Group inode hardlimit: $limit"
2030
2031         proc=" qmt.*.dt-0x0.glb-usr"
2032         id_cnt=$(do_node $mdt0_node $LCTL get_param -n $proc | wc -l)
2033         [ $id_cnt -eq 401 ] || error "Migrate block user limit failed: $id_cnt"
2034         limit=$(getquota -u 60001 global bsoftlimit)
2035         [ $limit -eq 10485760 ] || error "User block softlimit: $limit"
2036         limit=$(getquota -u 60001 global bhardlimit)
2037         [ $limit -eq 20971520 ] || error "User block hardlimit: $limit"
2038
2039         proc="qmt.*.dt-0x0.glb-grp"
2040         id_cnt=$(do_node $mdt0_node $LCTL get_param -n $proc | wc -l)
2041         [ $id_cnt -eq 401 ] || error "Migrate block user limit failed: $id_cnt"
2042         limit=$(getquota -g 60001 global bsoftlimit)
2043         [ $limit -eq 10485760 ] || error "Group block softlimit: $limit"
2044         limit=$(getquota -g 60001 global bhardlimit)
2045         [ $limit -eq 20971520 ] || error "Group block hardlimit: $limit"
2046
2047         echo "Cleanup..."
2048         formatall
2049         setupall
2050 }
2051 run_test 36 "Migrate old admin files into new global indexes"
2052
2053 quota_fini()
2054 {
2055         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=-quota"
2056 }
2057 quota_fini
2058
2059 cd $ORIG_PWD
2060 complete $(basename $0) $SECONDS
2061 check_and_cleanup_lustre
2062 export USE_OFD="$USE_OFD_OLD"
2063 export LOAD_MODULES_REMOTE="$LOAD_MODULES_REMOTE_OLD"
2064 export QUOTA_AUTO=$QUOTA_AUTO_OLD
2065 exit_status