Home | History | Annotate | Line # | Download | only in resize_lfs
resize_lfs.c revision 1.3
      1 /*	$NetBSD: resize_lfs.c,v 1.3 2006/09/04 18:34:22 wiz 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 (at) 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/file.h>
     44 #include <sys/mount.h>
     45 #include <sys/statvfs.h>
     46 
     47 #include <ufs/ufs/dinode.h>
     48 #include <ufs/lfs/lfs.h>
     49 
     50 #include <disktab.h>
     51 #include <err.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <unistd.h>
     56 
     57 static void
     58 usage(void)
     59 {
     60 	errx(1, "usage: resize_lfs [-v] [-s new-size] [filesystem]");
     61 }
     62 
     63 int
     64 main(int argc, char **argv)
     65 {
     66 	char *cp, *rdev, *fsname, buf[LFS_SBPAD];
     67 	daddr_t newsize, newnsegs;
     68 	int devfd, rootfd;
     69 	int ch, i, verbose;
     70 	off_t secsize, sboff;
     71 	struct disklabel lbl;
     72 	struct lfs *fs;
     73 	struct partition *pp;
     74 	struct statvfs vfs;
     75 
     76 	/* Initialize and parse arguments */
     77 	verbose = 0;
     78 	newsize = 0;
     79 	while ((ch = getopt(argc, argv, "s:v")) != -1) {
     80 		switch(ch) {
     81 		case 's':	/* New size, in sectors */
     82 			newsize = strtoll(optarg, NULL, 10);
     83 			break;
     84 		case 'v':
     85 			++verbose;
     86 			break;
     87 		default:
     88 			usage();
     89 		}
     90 	}
     91 	fsname = argv[optind];
     92 
     93 	if (fsname == NULL)
     94 		usage();
     95 
     96 	/*
     97 	 * If the user did not supply a filesystem size, use the
     98 	 * length of the mounted partition.
     99 	 */
    100 	if (statvfs(fsname, &vfs) < 0)
    101 		err(1, "%s", fsname);
    102 	rdev = (char *)malloc(strlen(vfs.f_mntfromname + 2));
    103 	sprintf(rdev, "/dev/r%s", vfs.f_mntfromname + 5);
    104 	devfd = open(rdev, O_RDONLY);
    105 	if (devfd < 0)
    106 		err(1, "open raw device");
    107 
    108 	/*
    109 	 * Read the disklabel to find the sector size.  Check the
    110 	 * given size against the partition size.  We can skip some
    111 	 * error checking here since we know the fs is mountable.
    112 	 */
    113 	if (ioctl(devfd, DIOCGDINFO, &lbl) < 0)
    114 		err(1, "%s: ioctl (GDINFO)", rdev);
    115 	secsize = lbl.d_secsize;
    116 	cp = strchr(rdev, '\0') - 1;
    117 	pp = &lbl.d_partitions[*cp - 'a'];
    118 	if (newsize > pp->p_size)
    119 		errx(1, "new size must be <= the partition size");
    120 	if (newsize == 0)
    121 		newsize = pp->p_size;
    122 
    123 	/* Open the root of the filesystem so we can fcntl() it */
    124 	rootfd = open(fsname, O_RDONLY);
    125 	if (rootfd < 0)
    126 		err(1, "open filesystem root");
    127 
    128 	/* Read the superblock, finding alternates if necessary */
    129 	fs = (struct lfs *)malloc(sizeof(*fs));
    130 	for (sboff = LFS_LABELPAD;;) {
    131 		pread(devfd, buf, sboff, LFS_SBPAD);
    132 		memcpy(&fs->lfs_dlfs, buf, sizeof(struct dlfs));
    133 		if (sboff == LFS_LABELPAD && fsbtob(fs, 1) > LFS_LABELPAD)
    134 			sboff = fsbtob(fs, (off_t)fs->lfs_sboffs[0]);
    135 		else
    136 			break;
    137 	}
    138 	close(devfd);
    139 
    140 	/* Calculate new number of segments. */
    141 	newnsegs = (newsize * secsize) / fs->lfs_ssize;
    142 	if (newnsegs == fs->lfs_nseg) {
    143 		errx(0, "the filesystem is unchanged.");
    144 	}
    145 
    146 	/*
    147 	 * If the new filesystem is smaller than the old, we have to
    148 	 * invalidate the segments that extend beyond the new boundary.
    149 	 * Make the cleaner do this for us.
    150 	 * (XXX make the kernel able to do this instead?)
    151 	 */
    152 	for (i = fs->lfs_nseg - 1; i >= newnsegs; --i) {
    153 		char cmd[80];
    154 
    155 		/* If it's already empty, don't call the cleaner */
    156 		if (fcntl(rootfd, LFCNINVAL, &i) == 0)
    157 			continue;
    158 
    159 		sprintf(cmd, "/usr/libexec/lfs_cleanerd -q -i %d %s", i, fsname);
    160 		if (system(cmd) != 0)
    161 			err(1, "invalidating segment %d", i);
    162 	}
    163 
    164 	/* Tell the filesystem to resize itself. */
    165 	if (fcntl(rootfd, LFCNRESIZE, &newnsegs) == -1) {
    166 		err(1, "resizing");
    167 	}
    168 
    169 	if (verbose)
    170 		printf("Successfully resized %s from %d to %lld segments\n",
    171 			fsname, fs->lfs_nseg, (long long)newnsegs);
    172 	return 0;
    173 }
    174