resize_lfs.c revision 1.4 1 1.4 riz /* $NetBSD: resize_lfs.c,v 1.4 2006/09/05 19:46:14 riz Exp $ */
2 1.1 perseant /*-
3 1.1 perseant * Copyright (c) 2005 The NetBSD Foundation, Inc.
4 1.1 perseant * All rights reserved.
5 1.1 perseant *
6 1.1 perseant * This code is derived from software contributed to The NetBSD Foundation
7 1.1 perseant * by Konrad E. Schroder <perseant (at) hhhh.org>.
8 1.1 perseant *
9 1.1 perseant * Redistribution and use in source and binary forms, with or without
10 1.1 perseant * modification, are permitted provided that the following conditions
11 1.1 perseant * are met:
12 1.1 perseant * 1. Redistributions of source code must retain the above copyright
13 1.1 perseant * notice, this list of conditions and the following disclaimer.
14 1.1 perseant * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 perseant * notice, this list of conditions and the following disclaimer in the
16 1.1 perseant * documentation and/or other materials provided with the distribution.
17 1.1 perseant * 3. All advertising materials mentioning features or use of this software
18 1.1 perseant * must display the following acknowledgement:
19 1.1 perseant * This product includes software developed by the NetBSD
20 1.1 perseant * Foundation, Inc. and its contributors.
21 1.1 perseant * 4. Neither the name of The NetBSD Foundation nor the names of its
22 1.1 perseant * contributors may be used to endorse or promote products derived
23 1.1 perseant * from this software without specific prior written permission.
24 1.1 perseant *
25 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 1.1 perseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 perseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 perseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 1.1 perseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 perseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 perseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 perseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 perseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 perseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 perseant * POSSIBILITY OF SUCH DAMAGE.
36 1.1 perseant */
37 1.1 perseant
38 1.1 perseant #include <sys/param.h>
39 1.1 perseant #include <sys/types.h>
40 1.1 perseant #include <sys/stat.h>
41 1.1 perseant #include <sys/ioctl.h>
42 1.1 perseant #include <sys/disklabel.h>
43 1.4 riz #include <sys/disk.h>
44 1.1 perseant #include <sys/file.h>
45 1.1 perseant #include <sys/mount.h>
46 1.1 perseant #include <sys/statvfs.h>
47 1.1 perseant
48 1.1 perseant #include <ufs/ufs/dinode.h>
49 1.1 perseant #include <ufs/lfs/lfs.h>
50 1.1 perseant
51 1.1 perseant #include <disktab.h>
52 1.1 perseant #include <err.h>
53 1.1 perseant #include <stdio.h>
54 1.1 perseant #include <stdlib.h>
55 1.1 perseant #include <string.h>
56 1.1 perseant #include <unistd.h>
57 1.1 perseant
58 1.4 riz #include "partutil.h"
59 1.4 riz
60 1.1 perseant static void
61 1.1 perseant usage(void)
62 1.1 perseant {
63 1.3 wiz errx(1, "usage: resize_lfs [-v] [-s new-size] [filesystem]");
64 1.1 perseant }
65 1.1 perseant
66 1.1 perseant int
67 1.1 perseant main(int argc, char **argv)
68 1.1 perseant {
69 1.4 riz char *rdev, *fsname, buf[LFS_SBPAD];
70 1.1 perseant daddr_t newsize, newnsegs;
71 1.1 perseant int devfd, rootfd;
72 1.1 perseant int ch, i, verbose;
73 1.1 perseant off_t secsize, sboff;
74 1.4 riz struct disk_geom geo;
75 1.4 riz struct dkwedge_info dkw;
76 1.1 perseant struct lfs *fs;
77 1.1 perseant struct statvfs vfs;
78 1.1 perseant
79 1.1 perseant /* Initialize and parse arguments */
80 1.2 lukem verbose = 0;
81 1.1 perseant newsize = 0;
82 1.1 perseant while ((ch = getopt(argc, argv, "s:v")) != -1) {
83 1.1 perseant switch(ch) {
84 1.1 perseant case 's': /* New size, in sectors */
85 1.1 perseant newsize = strtoll(optarg, NULL, 10);
86 1.1 perseant break;
87 1.1 perseant case 'v':
88 1.1 perseant ++verbose;
89 1.1 perseant break;
90 1.1 perseant default:
91 1.1 perseant usage();
92 1.1 perseant }
93 1.1 perseant }
94 1.1 perseant fsname = argv[optind];
95 1.1 perseant
96 1.1 perseant if (fsname == NULL)
97 1.1 perseant usage();
98 1.1 perseant
99 1.1 perseant /*
100 1.1 perseant * If the user did not supply a filesystem size, use the
101 1.1 perseant * length of the mounted partition.
102 1.1 perseant */
103 1.1 perseant if (statvfs(fsname, &vfs) < 0)
104 1.1 perseant err(1, "%s", fsname);
105 1.1 perseant rdev = (char *)malloc(strlen(vfs.f_mntfromname + 2));
106 1.1 perseant sprintf(rdev, "/dev/r%s", vfs.f_mntfromname + 5);
107 1.1 perseant devfd = open(rdev, O_RDONLY);
108 1.1 perseant if (devfd < 0)
109 1.1 perseant err(1, "open raw device");
110 1.1 perseant
111 1.1 perseant /*
112 1.1 perseant * Read the disklabel to find the sector size. Check the
113 1.1 perseant * given size against the partition size. We can skip some
114 1.1 perseant * error checking here since we know the fs is mountable.
115 1.1 perseant */
116 1.4 riz if (getdiskinfo(rdev, devfd, NULL, &geo, &dkw) == -1)
117 1.4 riz err(1, "%s: could not get info", rdev);
118 1.4 riz secsize = geo.dg_secsize;
119 1.4 riz if (newsize > dkw.dkw_size)
120 1.1 perseant errx(1, "new size must be <= the partition size");
121 1.1 perseant if (newsize == 0)
122 1.4 riz newsize = dkw.dkw_size;
123 1.1 perseant
124 1.1 perseant /* Open the root of the filesystem so we can fcntl() it */
125 1.1 perseant rootfd = open(fsname, O_RDONLY);
126 1.1 perseant if (rootfd < 0)
127 1.1 perseant err(1, "open filesystem root");
128 1.1 perseant
129 1.1 perseant /* Read the superblock, finding alternates if necessary */
130 1.1 perseant fs = (struct lfs *)malloc(sizeof(*fs));
131 1.1 perseant for (sboff = LFS_LABELPAD;;) {
132 1.1 perseant pread(devfd, buf, sboff, LFS_SBPAD);
133 1.1 perseant memcpy(&fs->lfs_dlfs, buf, sizeof(struct dlfs));
134 1.1 perseant if (sboff == LFS_LABELPAD && fsbtob(fs, 1) > LFS_LABELPAD)
135 1.1 perseant sboff = fsbtob(fs, (off_t)fs->lfs_sboffs[0]);
136 1.1 perseant else
137 1.1 perseant break;
138 1.1 perseant }
139 1.1 perseant close(devfd);
140 1.1 perseant
141 1.1 perseant /* Calculate new number of segments. */
142 1.1 perseant newnsegs = (newsize * secsize) / fs->lfs_ssize;
143 1.1 perseant if (newnsegs == fs->lfs_nseg) {
144 1.1 perseant errx(0, "the filesystem is unchanged.");
145 1.1 perseant }
146 1.1 perseant
147 1.1 perseant /*
148 1.1 perseant * If the new filesystem is smaller than the old, we have to
149 1.1 perseant * invalidate the segments that extend beyond the new boundary.
150 1.1 perseant * Make the cleaner do this for us.
151 1.1 perseant * (XXX make the kernel able to do this instead?)
152 1.1 perseant */
153 1.1 perseant for (i = fs->lfs_nseg - 1; i >= newnsegs; --i) {
154 1.1 perseant char cmd[80];
155 1.1 perseant
156 1.1 perseant /* If it's already empty, don't call the cleaner */
157 1.1 perseant if (fcntl(rootfd, LFCNINVAL, &i) == 0)
158 1.1 perseant continue;
159 1.1 perseant
160 1.1 perseant sprintf(cmd, "/usr/libexec/lfs_cleanerd -q -i %d %s", i, fsname);
161 1.1 perseant if (system(cmd) != 0)
162 1.1 perseant err(1, "invalidating segment %d", i);
163 1.1 perseant }
164 1.1 perseant
165 1.1 perseant /* Tell the filesystem to resize itself. */
166 1.1 perseant if (fcntl(rootfd, LFCNRESIZE, &newnsegs) == -1) {
167 1.1 perseant err(1, "resizing");
168 1.1 perseant }
169 1.1 perseant
170 1.1 perseant if (verbose)
171 1.1 perseant printf("Successfully resized %s from %d to %lld segments\n",
172 1.1 perseant fsname, fs->lfs_nseg, (long long)newnsegs);
173 1.1 perseant return 0;
174 1.1 perseant }
175