7 # Copyright (c) 2003 Silicon Graphics, Inc. All Rights Reserved.
9 # This program is free software; you can redistribute it and/or modify it
10 # under the terms of version 2 of the GNU General Public License as
11 # published by the Free Software Foundation.
13 # This program is distributed in the hope that it would be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 # Further, this software is distributed without any warranty that it is
18 # free of the rightful claim of any third person regarding infringement
19 # or the like. Any license provided herein, whether implied or
20 # otherwise, applies only to this software file. Patent licenses, if
21 # any, provided herein do not apply to combinations of this program with
22 # other software, or any other product whatsoever.
24 # You should have received a copy of the GNU General Public License along
25 # with this program; if not, write the Free Software Foundation, Inc., 59
26 # Temple Place - Suite 330, Boston MA 02111-1307, USA.
28 # Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
29 # Mountain View, CA 94043, or:
33 # For further information regarding this notice, see:
35 # http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
39 # Modify a filesystem's superblock and AGF metadata structures
40 # so that only a subset of the allocation groups will be used.
41 # Intended use is in testing large virtual devices (eg. loop)
42 # with extremely large filesystems, where we want to ensure
43 # high allocation groups are used as much as possible (where
44 # the block addresses are large).
47 my @sbprint = ( '"print fdblocks"', '"print agcount"' );
48 my @agfprint = ( '"print freeblks"', '"print flcount"' );
49 my @agfcommands = ( '"write freeblks 0"',
50 '"write longest 0"', '"write flcount 0"',
51 '"write bnolevel 1"', '"write cntlevel 1"',
52 '"write flfirst 0"', '"write fllast 0"' );
53 my @bnoprint = ( '"addr bnoroot"', '"print numrecs"' );
54 my @bnocommands = ( '"addr bnoroot"', '"write numrecs 0"',
55 '"write leftsib -1"', '"write rightsib -1"' );
56 my @cntprint = ( '"addr cntroot"', '"print numrecs"' );
57 my @cntcommands = ( '"addr cntroot"', '"write numrecs 0"',
58 '"write leftsib -1"', '"write rightsib -1"' );
61 getopts('cf:l:v', \%opt);
62 die "Usage: ag-wipe [-f firstAG] [-l lastAG] [-cv] device\n" unless (@ARGV == 1);
63 my $device = shift @ARGV;
64 die "$device: no such file\n" unless (-e $device);
65 my $clearall = defined($opt{'c'}) ? 1 : 0;
66 my $verbose = defined($opt{'v'}) ? 1 : 0;
67 my $nagfirst = defined($opt{'f'}) ? $opt{'f'} : 0;
68 my $naglast = defined($opt{'l'}) ? $opt{'l'} : 0;
71 my $xfsdb = "xfs_db -x $device";
75 $xfsdb .= ' -c ' . $_;
77 print $xfsdb, "\n" if ($verbose);
79 die unless open(DB, "$xfsdb 2>/dev/null |");
81 if (/^(\S+) = (.*)$/) {
89 my %sb = xfs_db 'sb', @sbprint;
90 print "=== Initially ", $sb{'fdblocks'}, " blocks free across ",
91 $sb{'agcount'}, " AGs\n";
92 if ($clearall && ($nagfirst || $naglast)) {
93 print STDERR " o Clearall specified with first/last AG, quiting\n";
96 if ($nagfirst >= $sb{'agcount'}) {
97 print " o First AG number is too large, quiting\n";
100 if ($naglast >= $sb{'agcount'}) {
101 print " o Last AG number is too large, quiting\n";
104 if ($naglast - $nagfirst < 0) {
105 print " o No AGs to clear, quiting\n";
109 $naglast = $sb{'agcount'} - 2;
112 print "=== Wiping ", $naglast - $nagfirst + 1,
113 " AGs starting from AG #", $nagfirst, "\n";
116 while ($ag <= $naglast) {
120 print " o AG#", $ag, " AGF fields\n";
122 %agf = xfs_db "'agf $ag'", @agfprint;
123 xfs_db "'agf $ag'", @agfcommands;
125 my $blockcnt = $agf{'freeblks'} + $agf{'flcount'};
126 $sb{'fdblocks'} -= $blockcnt;
127 print " cleared ", $blockcnt, " blocks from AG#", $ag, "\n";
129 %btree = xfs_db "'agf $ag'", @bnoprint;
130 xfs_db "'agf $ag'", @bnocommands;
131 print " cleared ", $btree{'numrecs'}, " BNO btree records in AG#",
134 %btree = xfs_db "'agf $ag'", @cntprint;
135 xfs_db "'agf $ag'", @cntcommands;
136 print " cleared ", $btree{'numrecs'}, " CNT btree in AG#",
141 print "=== Updating final freespace count, ", $sb{'fdblocks'}, " blocks\n";
142 xfs_db "'sb 0'", "'write fdblocks $sb{'fdblocks'}'"