Home | History | Annotate | Line # | Download | only in resize_lfs
resize_lfs.c revision 1.6.26.2
      1  1.6.26.1       tls /*	$NetBSD: resize_lfs.c,v 1.6.26.2 2014/08/20 00:02:27 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.6.26.2       tls 	size_t rdevlen;
     64       1.1  perseant 	daddr_t newsize, newnsegs;
     65       1.1  perseant 	int devfd, rootfd;
     66       1.1  perseant 	int ch, i, verbose;
     67       1.1  perseant 	off_t secsize, sboff;
     68       1.4       riz 	struct disk_geom geo;
     69       1.4       riz 	struct dkwedge_info dkw;
     70       1.1  perseant 	struct lfs *fs;
     71       1.1  perseant 	struct statvfs vfs;
     72       1.1  perseant 
     73       1.1  perseant 	/* Initialize and parse arguments */
     74       1.2     lukem 	verbose = 0;
     75       1.1  perseant 	newsize = 0;
     76       1.1  perseant 	while ((ch = getopt(argc, argv, "s:v")) != -1) {
     77       1.1  perseant 		switch(ch) {
     78       1.1  perseant 		case 's':	/* New size, in sectors */
     79       1.1  perseant 			newsize = strtoll(optarg, NULL, 10);
     80       1.1  perseant 			break;
     81       1.1  perseant 		case 'v':
     82       1.1  perseant 			++verbose;
     83       1.1  perseant 			break;
     84       1.1  perseant 		default:
     85       1.1  perseant 			usage();
     86       1.1  perseant 		}
     87       1.1  perseant 	}
     88       1.1  perseant 	fsname = argv[optind];
     89       1.1  perseant 
     90       1.1  perseant 	if (fsname == NULL)
     91       1.1  perseant 		usage();
     92       1.1  perseant 
     93       1.1  perseant 	/*
     94       1.1  perseant 	 * If the user did not supply a filesystem size, use the
     95       1.1  perseant 	 * length of the mounted partition.
     96       1.1  perseant 	 */
     97       1.1  perseant 	if (statvfs(fsname, &vfs) < 0)
     98       1.1  perseant 		err(1, "%s", fsname);
     99  1.6.26.2       tls 	rdevlen = strlen(vfs.f_mntfromname) + 2;
    100  1.6.26.2       tls 	rdev = malloc(rdevlen);
    101  1.6.26.2       tls 	snprintf(rdev, rdevlen, "/dev/r%s", vfs.f_mntfromname + 5);
    102       1.1  perseant 	devfd = open(rdev, O_RDONLY);
    103       1.1  perseant 	if (devfd < 0)
    104       1.1  perseant 		err(1, "open raw device");
    105       1.1  perseant 
    106       1.1  perseant 	/*
    107       1.1  perseant 	 * Read the disklabel to find the sector size.  Check the
    108       1.1  perseant 	 * given size against the partition size.  We can skip some
    109       1.1  perseant 	 * error checking here since we know the fs is mountable.
    110       1.1  perseant 	 */
    111       1.4       riz 	if (getdiskinfo(rdev, devfd, NULL, &geo, &dkw) == -1)
    112       1.4       riz 		err(1, "%s: could not get info", rdev);
    113       1.4       riz 	secsize = geo.dg_secsize;
    114       1.4       riz 	if (newsize > dkw.dkw_size)
    115       1.1  perseant 		errx(1, "new size must be <= the partition size");
    116       1.1  perseant 	if (newsize == 0)
    117       1.4       riz 		newsize = dkw.dkw_size;
    118       1.1  perseant 
    119       1.1  perseant 	/* Open the root of the filesystem so we can fcntl() it */
    120       1.1  perseant 	rootfd = open(fsname, O_RDONLY);
    121       1.1  perseant 	if (rootfd < 0)
    122       1.1  perseant 		err(1, "open filesystem root");
    123       1.1  perseant 
    124       1.1  perseant 	/* Read the superblock, finding alternates if necessary */
    125       1.1  perseant 	fs = (struct lfs *)malloc(sizeof(*fs));
    126       1.1  perseant 	for (sboff = LFS_LABELPAD;;) {
    127       1.1  perseant 		pread(devfd, buf, sboff, LFS_SBPAD);
    128       1.1  perseant 		memcpy(&fs->lfs_dlfs, buf, sizeof(struct dlfs));
    129  1.6.26.1       tls 		if (sboff == LFS_LABELPAD && lfs_fsbtob(fs, 1) > LFS_LABELPAD)
    130  1.6.26.1       tls 			sboff = lfs_fsbtob(fs, (off_t)fs->lfs_sboffs[0]);
    131       1.1  perseant 		else
    132       1.1  perseant 			break;
    133       1.1  perseant 	}
    134       1.1  perseant 	close(devfd);
    135       1.1  perseant 
    136       1.1  perseant 	/* Calculate new number of segments. */
    137       1.1  perseant 	newnsegs = (newsize * secsize) / fs->lfs_ssize;
    138       1.1  perseant 	if (newnsegs == fs->lfs_nseg) {
    139       1.1  perseant 		errx(0, "the filesystem is unchanged.");
    140       1.1  perseant 	}
    141       1.1  perseant 
    142       1.1  perseant 	/*
    143       1.1  perseant 	 * If the new filesystem is smaller than the old, we have to
    144       1.1  perseant 	 * invalidate the segments that extend beyond the new boundary.
    145       1.1  perseant 	 * Make the cleaner do this for us.
    146       1.1  perseant 	 * (XXX make the kernel able to do this instead?)
    147       1.1  perseant 	 */
    148       1.1  perseant 	for (i = fs->lfs_nseg - 1; i >= newnsegs; --i) {
    149  1.6.26.2       tls 		char cmd[128];
    150       1.1  perseant 
    151       1.1  perseant 		/* If it's already empty, don't call the cleaner */
    152       1.1  perseant 		if (fcntl(rootfd, LFCNINVAL, &i) == 0)
    153       1.1  perseant 			continue;
    154       1.1  perseant 
    155  1.6.26.2       tls 		snprintf(cmd, sizeof(cmd), "/libexec/lfs_cleanerd -q -i %d %s",
    156  1.6.26.2       tls 			 i, fsname);
    157       1.1  perseant 		if (system(cmd) != 0)
    158       1.1  perseant 			err(1, "invalidating segment %d", i);
    159       1.1  perseant 	}
    160       1.1  perseant 
    161       1.1  perseant 	/* Tell the filesystem to resize itself. */
    162       1.1  perseant 	if (fcntl(rootfd, LFCNRESIZE, &newnsegs) == -1) {
    163       1.1  perseant 		err(1, "resizing");
    164       1.1  perseant 	}
    165       1.1  perseant 
    166       1.1  perseant 	if (verbose)
    167       1.1  perseant 		printf("Successfully resized %s from %d to %lld segments\n",
    168       1.1  perseant 			fsname, fs->lfs_nseg, (long long)newnsegs);
    169       1.1  perseant 	return 0;
    170       1.1  perseant }
    171