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