xref: /netbsd-src/sbin/resize_lfs/resize_lfs.c (revision a0698ed9d41653d7a2378819ad501a285ca0d401)
1 /*	$NetBSD: resize_lfs.c,v 1.14 2015/08/02 18:18:09 dholland 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  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/ioctl.h>
35 #include <sys/disklabel.h>
36 #include <sys/disk.h>
37 #include <sys/file.h>
38 #include <sys/mount.h>
39 #include <sys/statvfs.h>
40 
41 #include <ufs/lfs/lfs.h>
42 #include <ufs/lfs/lfs_accessors.h>
43 
44 #include <disktab.h>
45 #include <err.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 
51 #include "partutil.h"
52 
53 static void
54 usage(void)
55 {
56 	errx(1, "usage: resize_lfs [-v] [-s new-size] [filesystem]");
57 }
58 
59 int
60 main(int argc, char **argv)
61 {
62 	char *rdev, *fsname, buf[LFS_SBPAD];
63 	size_t rdevlen;
64 	daddr_t newsize, newnsegs;
65 	int devfd, rootfd;
66 	int ch, i, verbose;
67 	off_t secsize, sboff;
68 	struct disk_geom geo;
69 	struct dkwedge_info dkw;
70 	struct lfs *fs;
71 	struct statvfs vfs;
72 
73 	/* Initialize and parse arguments */
74 	verbose = 0;
75 	newsize = 0;
76 	while ((ch = getopt(argc, argv, "s:v")) != -1) {
77 		switch(ch) {
78 		case 's':	/* New size, in sectors */
79 			newsize = strtoll(optarg, NULL, 10);
80 			break;
81 		case 'v':
82 			++verbose;
83 			break;
84 		default:
85 			usage();
86 		}
87 	}
88 	fsname = argv[optind];
89 
90 	if (fsname == NULL)
91 		usage();
92 
93 	/*
94 	 * If the user did not supply a filesystem size, use the
95 	 * length of the mounted partition.
96 	 */
97 	if (statvfs(fsname, &vfs) < 0)
98 		err(1, "%s", fsname);
99 	rdevlen = strlen(vfs.f_mntfromname) + 2;
100 	rdev = malloc(rdevlen);
101 	snprintf(rdev, rdevlen, "/dev/r%s", vfs.f_mntfromname + 5);
102 	devfd = open(rdev, O_RDONLY);
103 	if (devfd < 0)
104 		err(1, "open raw device");
105 
106 	/*
107 	 * Read the disklabel to find the sector size.  Check the
108 	 * given size against the partition size.  We can skip some
109 	 * error checking here since we know the fs is mountable.
110 	 */
111 	if (getdiskinfo(rdev, devfd, NULL, &geo, &dkw) == -1)
112 		err(1, "%s: could not get info", rdev);
113 	secsize = geo.dg_secsize;
114 	if (newsize > dkw.dkw_size)
115 		errx(1, "new size must be <= the partition size");
116 	if (newsize == 0)
117 		newsize = dkw.dkw_size;
118 
119 	/* Open the root of the filesystem so we can fcntl() it */
120 	rootfd = open(fsname, O_RDONLY);
121 	if (rootfd < 0)
122 		err(1, "open filesystem root");
123 
124 	/* Read the superblock, finding alternates if necessary */
125 	fs = (struct lfs *)malloc(sizeof(*fs));
126 	for (sboff = LFS_LABELPAD;;) {
127 		pread(devfd, buf, sboff, LFS_SBPAD);
128 		__CTASSERT(sizeof(struct dlfs) == sizeof(struct dlfs64));
129 		memcpy(&fs->lfs_dlfs_u, buf, sizeof(struct dlfs));
130 		if (sboff == LFS_LABELPAD && lfs_fsbtob(fs, 1) > LFS_LABELPAD)
131 			sboff = lfs_fsbtob(fs, (off_t)lfs_sb_getsboff(fs, 0));
132 		else
133 			break;
134 	}
135 	close(devfd);
136 
137 	/* Calculate new number of segments. */
138 	newnsegs = (newsize * secsize) / lfs_sb_getssize(fs);
139 	if (newnsegs == lfs_sb_getnseg(fs)) {
140 		errx(0, "the filesystem is unchanged.");
141 	}
142 
143 	/*
144 	 * If the new filesystem is smaller than the old, we have to
145 	 * invalidate the segments that extend beyond the new boundary.
146 	 * Make the cleaner do this for us.
147 	 * (XXX make the kernel able to do this instead?)
148 	 */
149 	for (i = lfs_sb_getnseg(fs) - 1; i >= newnsegs; --i) {
150 		char cmd[128];
151 
152 		/* If it's already empty, don't call the cleaner */
153 		if (fcntl(rootfd, LFCNINVAL, &i) == 0)
154 			continue;
155 
156 		snprintf(cmd, sizeof(cmd), "/libexec/lfs_cleanerd -q -i %d %s",
157 			 i, fsname);
158 		if (system(cmd) != 0)
159 			err(1, "invalidating segment %d", i);
160 	}
161 
162 	/* Tell the filesystem to resize itself. */
163 	if (fcntl(rootfd, LFCNRESIZE, &newnsegs) == -1) {
164 		err(1, "resizing");
165 	}
166 
167 	if (verbose)
168 		printf("Successfully resized %s from %u to %lld segments\n",
169 			fsname, lfs_sb_getnseg(fs), (long long)newnsegs);
170 	return 0;
171 }
172