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