xref: /netbsd-src/sbin/resize_lfs/resize_lfs.c (revision ab52baf9b6b2594d6d6289c6ed50e7552f6a4747)
1*ab52baf9Smrg /*	$NetBSD: resize_lfs.c,v 1.16 2023/08/07 23:27:07 mrg Exp $	*/
22f695b54Sperseant /*-
32f695b54Sperseant  * Copyright (c) 2005 The NetBSD Foundation, Inc.
42f695b54Sperseant  * All rights reserved.
52f695b54Sperseant  *
62f695b54Sperseant  * This code is derived from software contributed to The NetBSD Foundation
72f695b54Sperseant  * by Konrad E. Schroder <perseant@hhhh.org>.
82f695b54Sperseant  *
92f695b54Sperseant  * Redistribution and use in source and binary forms, with or without
102f695b54Sperseant  * modification, are permitted provided that the following conditions
112f695b54Sperseant  * are met:
122f695b54Sperseant  * 1. Redistributions of source code must retain the above copyright
132f695b54Sperseant  *    notice, this list of conditions and the following disclaimer.
142f695b54Sperseant  * 2. Redistributions in binary form must reproduce the above copyright
152f695b54Sperseant  *    notice, this list of conditions and the following disclaimer in the
162f695b54Sperseant  *    documentation and/or other materials provided with the distribution.
172f695b54Sperseant  *
182f695b54Sperseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
192f695b54Sperseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
202f695b54Sperseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
212f695b54Sperseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
222f695b54Sperseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
232f695b54Sperseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
242f695b54Sperseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
252f695b54Sperseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
262f695b54Sperseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
272f695b54Sperseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
282f695b54Sperseant  * POSSIBILITY OF SUCH DAMAGE.
292f695b54Sperseant  */
302f695b54Sperseant 
312f695b54Sperseant #include <sys/param.h>
322f695b54Sperseant #include <sys/types.h>
332f695b54Sperseant #include <sys/stat.h>
342f695b54Sperseant #include <sys/ioctl.h>
352f695b54Sperseant #include <sys/disklabel.h>
36d28b2beeSriz #include <sys/disk.h>
372f695b54Sperseant #include <sys/file.h>
382f695b54Sperseant #include <sys/mount.h>
392f695b54Sperseant #include <sys/statvfs.h>
402f695b54Sperseant 
412f695b54Sperseant #include <ufs/lfs/lfs.h>
4234f0d74cSdholland #include <ufs/lfs/lfs_accessors.h>
432f695b54Sperseant 
442f695b54Sperseant #include <disktab.h>
452f695b54Sperseant #include <err.h>
462f695b54Sperseant #include <stdio.h>
472f695b54Sperseant #include <stdlib.h>
482f695b54Sperseant #include <string.h>
492f695b54Sperseant #include <unistd.h>
50277f8a75Sbrad #include <util.h>
512f695b54Sperseant 
52d28b2beeSriz #include "partutil.h"
53d28b2beeSriz 
542f695b54Sperseant static void
usage(void)552f695b54Sperseant usage(void)
562f695b54Sperseant {
579f284573Swiz 	errx(1, "usage: resize_lfs [-v] [-s new-size] [filesystem]");
582f695b54Sperseant }
592f695b54Sperseant 
602f695b54Sperseant int
main(int argc,char ** argv)612f695b54Sperseant main(int argc, char **argv)
622f695b54Sperseant {
63d28b2beeSriz 	char *rdev, *fsname, buf[LFS_SBPAD];
64ccc32d78Sdholland 	size_t rdevlen;
652f695b54Sperseant 	daddr_t newsize, newnsegs;
662f695b54Sperseant 	int devfd, rootfd;
672f695b54Sperseant 	int ch, i, verbose;
682f695b54Sperseant 	off_t secsize, sboff;
69d28b2beeSriz 	struct disk_geom geo;
70d28b2beeSriz 	struct dkwedge_info dkw;
712f695b54Sperseant 	struct lfs *fs;
722f695b54Sperseant 	struct statvfs vfs;
732f695b54Sperseant 
742f695b54Sperseant 	/* Initialize and parse arguments */
75620037aeSlukem 	verbose = 0;
762f695b54Sperseant 	newsize = 0;
772f695b54Sperseant 	while ((ch = getopt(argc, argv, "s:v")) != -1) {
782f695b54Sperseant 		switch(ch) {
792f695b54Sperseant 		case 's':	/* New size, in sectors */
802f695b54Sperseant 			newsize = strtoll(optarg, NULL, 10);
812f695b54Sperseant 			break;
822f695b54Sperseant 		case 'v':
832f695b54Sperseant 			++verbose;
842f695b54Sperseant 			break;
852f695b54Sperseant 		default:
862f695b54Sperseant 			usage();
872f695b54Sperseant 		}
882f695b54Sperseant 	}
892f695b54Sperseant 	fsname = argv[optind];
902f695b54Sperseant 
912f695b54Sperseant 	if (fsname == NULL)
922f695b54Sperseant 		usage();
932f695b54Sperseant 
942f695b54Sperseant 	/*
952f695b54Sperseant 	 * If the user did not supply a filesystem size, use the
962f695b54Sperseant 	 * length of the mounted partition.
972f695b54Sperseant 	 */
982f695b54Sperseant 	if (statvfs(fsname, &vfs) < 0)
992f695b54Sperseant 		err(1, "%s", fsname);
100ccc32d78Sdholland 	rdevlen = strlen(vfs.f_mntfromname) + 2;
101ccc32d78Sdholland 	rdev = malloc(rdevlen);
102277f8a75Sbrad 	if (getdiskrawname(rdev, rdevlen, vfs.f_mntfromname) == NULL)
103277f8a75Sbrad 		err(1, "Could not convert '%s' to raw name", vfs.f_mntfromname);
1042f695b54Sperseant 	devfd = open(rdev, O_RDONLY);
1052f695b54Sperseant 	if (devfd < 0)
1062f695b54Sperseant 		err(1, "open raw device");
1072f695b54Sperseant 
1082f695b54Sperseant 	/*
1092f695b54Sperseant 	 * Read the disklabel to find the sector size.  Check the
1102f695b54Sperseant 	 * given size against the partition size.  We can skip some
1112f695b54Sperseant 	 * error checking here since we know the fs is mountable.
1122f695b54Sperseant 	 */
113d28b2beeSriz 	if (getdiskinfo(rdev, devfd, NULL, &geo, &dkw) == -1)
114d28b2beeSriz 		err(1, "%s: could not get info", rdev);
115d28b2beeSriz 	secsize = geo.dg_secsize;
116d28b2beeSriz 	if (newsize > dkw.dkw_size)
1172f695b54Sperseant 		errx(1, "new size must be <= the partition size");
1182f695b54Sperseant 	if (newsize == 0)
119d28b2beeSriz 		newsize = dkw.dkw_size;
1202f695b54Sperseant 
1212f695b54Sperseant 	/* Open the root of the filesystem so we can fcntl() it */
1222f695b54Sperseant 	rootfd = open(fsname, O_RDONLY);
1232f695b54Sperseant 	if (rootfd < 0)
1242f695b54Sperseant 		err(1, "open filesystem root");
1252f695b54Sperseant 
1262f695b54Sperseant 	/* Read the superblock, finding alternates if necessary */
127*ab52baf9Smrg 	fs = (struct lfs *)calloc(sizeof(*fs), 1);
1282f695b54Sperseant 	for (sboff = LFS_LABELPAD;;) {
1292f695b54Sperseant 		pread(devfd, buf, sboff, LFS_SBPAD);
1309e5184b8Sdholland 		__CTASSERT(sizeof(struct dlfs) == sizeof(struct dlfs64));
1319e5184b8Sdholland 		memcpy(&fs->lfs_dlfs_u, buf, sizeof(struct dlfs));
132d418f0d0Schristos 		if (sboff == LFS_LABELPAD && lfs_fsbtob(fs, 1) > LFS_LABELPAD)
133adca8af5Sdholland 			sboff = lfs_fsbtob(fs, (off_t)lfs_sb_getsboff(fs, 0));
1342f695b54Sperseant 		else
1352f695b54Sperseant 			break;
1362f695b54Sperseant 	}
1372f695b54Sperseant 	close(devfd);
1382f695b54Sperseant 
1392f695b54Sperseant 	/* Calculate new number of segments. */
140f59b8f4bSdholland 	newnsegs = (newsize * secsize) / lfs_sb_getssize(fs);
141adca8af5Sdholland 	if (newnsegs == lfs_sb_getnseg(fs)) {
1422f695b54Sperseant 		errx(0, "the filesystem is unchanged.");
1432f695b54Sperseant 	}
1442f695b54Sperseant 
1452f695b54Sperseant 	/*
1462f695b54Sperseant 	 * If the new filesystem is smaller than the old, we have to
1472f695b54Sperseant 	 * invalidate the segments that extend beyond the new boundary.
1482f695b54Sperseant 	 * Make the cleaner do this for us.
1492f695b54Sperseant 	 * (XXX make the kernel able to do this instead?)
1502f695b54Sperseant 	 */
151adca8af5Sdholland 	for (i = lfs_sb_getnseg(fs) - 1; i >= newnsegs; --i) {
152deef9486Sdholland 		char cmd[128];
1532f695b54Sperseant 
1542f695b54Sperseant 		/* If it's already empty, don't call the cleaner */
1552f695b54Sperseant 		if (fcntl(rootfd, LFCNINVAL, &i) == 0)
1562f695b54Sperseant 			continue;
1572f695b54Sperseant 
158deef9486Sdholland 		snprintf(cmd, sizeof(cmd), "/libexec/lfs_cleanerd -q -i %d %s",
159deef9486Sdholland 			 i, fsname);
1602f695b54Sperseant 		if (system(cmd) != 0)
1612f695b54Sperseant 			err(1, "invalidating segment %d", i);
1622f695b54Sperseant 	}
1632f695b54Sperseant 
1642f695b54Sperseant 	/* Tell the filesystem to resize itself. */
1652f695b54Sperseant 	if (fcntl(rootfd, LFCNRESIZE, &newnsegs) == -1) {
1662f695b54Sperseant 		err(1, "resizing");
1672f695b54Sperseant 	}
1682f695b54Sperseant 
1692f695b54Sperseant 	if (verbose)
170adca8af5Sdholland 		printf("Successfully resized %s from %u to %lld segments\n",
171adca8af5Sdholland 			fsname, lfs_sb_getnseg(fs), (long long)newnsegs);
1722f695b54Sperseant 	return 0;
1732f695b54Sperseant }
174