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