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