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