1212839Sbrian# $FreeBSD$
2212839Sbrian
3212839Sbrianuse strict;
4212839Sbrianuse warnings;
5212839Sbrianuse Test::More tests => 19;
6212839Sbrianuse Fcntl qw(:DEFAULT :seek);
7212839Sbrian
8212839Sbrianuse constant BLK => 512;
9212839Sbrianuse constant BLKS_PER_MB => 2048;
10212839Sbrian
11212839Sbrianmy $unit;
12212839SbrianEND { system "mdconfig -du$unit" if defined $unit };
13212839Sbrian
14212839Sbriansub setsize {
15212839Sbrian    my ($partszMB, $unitszMB) = @_;
16212839Sbrian
17212839Sbrian    open my $fd, "|-", "disklabel -R md$unit /dev/stdin" or die;
18212839Sbrian    print $fd "a: ", ($partszMB * BLKS_PER_MB), " 0 4.2BSD 1024 8192\n";
19212839Sbrian    print $fd "c: ", ($unitszMB * BLKS_PER_MB), " 0 unused 0 0\n";
20212839Sbrian    close $fd;
21212839Sbrian}
22212839Sbrian
23212839Sbriansub fill {
24212839Sbrian    my ($start, $size, $content) = @_;
25212839Sbrian
26212839Sbrian    my $content512 = $content x (int(512 / length $content) + 1);
27212839Sbrian    substr($content512, 512) = "";
28212839Sbrian    sysopen my $fd, "/dev/md$unit", O_RDWR or die "/dev/md$unit: $!";
29212839Sbrian    seek($fd, $start * BLK, SEEK_SET);
30212839Sbrian    while ($size) {
31212839Sbrian	syswrite($fd, $content512) == 512 or die "write: $!";
32212839Sbrian	$size--;
33212839Sbrian    }
34212839Sbrian}
35212839Sbrian
36212839SbrianSKIP: {
37212839Sbrian    skip "Cannot test without UID 0", 19 if $<;
38212839Sbrian
39212839Sbrian    chomp(my $md = `mdconfig -s40m`);
40212839Sbrian    like($md, qr/^md\d+$/, "Created $md with size 40m") or die;
41212839Sbrian    $unit = substr $md, 2;
42212839Sbrian
43212839Sbrian    for my $type (1..2) {
44212839Sbrian
45212839Sbrian	initialise: {
46212839Sbrian	    ok(setsize(10, 40), "Sized ${md}a to 10m");
47212839Sbrian	    system "newfs -O $type -U ${md}a >/dev/null";
48212839Sbrian	    is($?, 0, "Initialised the filesystem on ${md}a as UFS$type");
49212839Sbrian	    chomp(my @out = `fsck -tufs -y ${md}a`);
50212839Sbrian	    ok(!grep(/MODIFIED/, @out), "fsck says ${md}a is clean, " .
51212839Sbrian		scalar(@out) . " lines of output");
52212839Sbrian	}
53212839Sbrian
54212839Sbrian	extend20_zeroed: {
55212839Sbrian	    ok(setsize(20, 40), "Sized ${md}a to 20m");
56212839Sbrian	    diag "Filling the extent with zeros";
57212839Sbrian	    fill(10 * BLKS_PER_MB, 10 * BLKS_PER_MB, chr(0));
58212839Sbrian	    my $out = `growfs -y ${md}a`;
59212839Sbrian	    is($?, 0, "Extended the filesystem on ${md}a") or print $out;
60212839Sbrian
61212839Sbrian	    my ($unallocated) = $out =~ m{\d+ sectors cannot be allocated};
62212839Sbrian	    fill(30 * BLKS_PER_MB - $unallocated, $unallocated, chr(0))
63212839Sbrian		if $unallocated;
64212839Sbrian
65212839Sbrian	    chomp(my @out = `fsck -tufs -y ${md}a`);
66212839Sbrian	    ok(!grep(/MODIFIED/, @out), "fsck says ${md}a is clean, " .
67212839Sbrian		scalar(@out) . " lines of output");
68212839Sbrian	}
69212839Sbrian
70212839Sbrian	extend30_garbaged: {
71212839Sbrian	    ok(setsize(30, 40), "Sized ${md}a to 30m");
72212839Sbrian	    diag "Filling the extent with garbage";
73212839Sbrian	    fill(20 * BLKS_PER_MB, 10 * BLKS_PER_MB, chr(0xaa) . chr(0x55));
74212839Sbrian	    my $out = `growfs -y ${md}a`;
75212839Sbrian	    is($?, 0, "Extended the filesystem on ${md}a") or print $out;
76212839Sbrian
77212839Sbrian	    my ($unallocated) = $out =~ m{\d+ sectors cannot be allocated};
78212839Sbrian	    fill(30 * BLKS_PER_MB - $unallocated, $unallocated, chr(0))
79212839Sbrian		if $unallocated;
80212839Sbrian
81212839Sbrian	    chomp(my @out = `fsck -tufs -y ${md}a`);
82212839Sbrian	    ok(!grep(/MODIFIED/, @out), "fsck says ${md}a is clean, " .
83212839Sbrian		scalar(@out) . " lines of output");
84212839Sbrian	}
85212839Sbrian    }
86212839Sbrian
87212839Sbrian    system "mdconfig -du$unit";
88212839Sbrian    undef $unit;
89212839Sbrian}
90