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"' );
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('f:l:v', \%opt);
62 die "Usage: ag-wipe [-f firstAG] [-l lastAG] [-v] device\n" unless (@ARGV == 1);
63 my $device = shift @ARGV;
64 die "$device: no such file\n" unless (-e $device);
65 my $verbose = defined($opt{'v'}) ? 1 : 0;
66 my $nagfirst = defined($opt{'f'}) ? $opt{'f'} : 0;
67 my $naglast = defined($opt{'l'}) ? $opt{'l'} : 0;
70 my $xfsdb = "xfs_db -x $device";
74 $xfsdb .= ' -c ' . $_;
76 print $xfsdb, "\n" if ($verbose);
78 die unless open(DB, "$xfsdb 2>/dev/null |");
80 if (/^(\S+) = (.*)$/) {
88 my %sb = xfs_db 'sb', @sbprint;
89 print "=== Initially ", $sb{'fdblocks'}, " blocks free across ",
90 $sb{'agcount'}, " AGs\n";
91 if ($nagfirst >= $sb{'agcount'}) {
92 print " o First AG number is too large, quiting\n";
95 if ($naglast >= $sb{'agcount'}) {
96 print " o Last AG number is too large, quiting\n";
99 if ($naglast - $nagfirst < 0) {
100 print " o No AGs to clear, quiting\n";
104 print "=== Wiping ", $naglast - $nagfirst,
105 " AGs starting from AG #", $nagfirst, "\n";
108 while ($ag <= $naglast) {
112 print " o AG#", $ag, " AGF fields\n";
114 %agf = xfs_db "'agf $ag'", @agfprint;
115 xfs_db "'agf $ag'", @agfcommands;
117 $sb{'fdblocks'} -= $agf{'freeblks'};
118 print " cleared ", $agf{'freeblks'}, " blocks from AG#", $ag, "\n";
120 %btree = xfs_db "'agf $ag'", @bnoprint;
121 xfs_db "'agf $ag'", @bnocommands;
122 print " cleared ", $btree{'numrecs'}, " BNO btree records in AG#",
125 %btree = xfs_db "'agf $ag'", @cntprint;
126 xfs_db "'agf $ag'", @cntcommands;
127 print " cleared ", $btree{'numrecs'}, " CNT btree in AG#",
132 print "=== Updating final freespace count, ", $sb{'fdblocks'}, " blocks\n";
133 xfs_db "'sb 0'", "'write fdblocks $sb{'fdblocks'}'"