1 1.16 mrg /* $NetBSD: resize_lfs.c,v 1.16 2023/08/07 23:27:07 mrg 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/lfs/lfs.h> 42 1.12 dholland #include <ufs/lfs/lfs_accessors.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.15 brad #include <util.h> 51 1.1 perseant 52 1.4 riz #include "partutil.h" 53 1.4 riz 54 1.1 perseant static void 55 1.1 perseant usage(void) 56 1.1 perseant { 57 1.3 wiz errx(1, "usage: resize_lfs [-v] [-s new-size] [filesystem]"); 58 1.1 perseant } 59 1.1 perseant 60 1.1 perseant int 61 1.1 perseant main(int argc, char **argv) 62 1.1 perseant { 63 1.4 riz char *rdev, *fsname, buf[LFS_SBPAD]; 64 1.8 dholland size_t rdevlen; 65 1.1 perseant daddr_t newsize, newnsegs; 66 1.1 perseant int devfd, rootfd; 67 1.1 perseant int ch, i, verbose; 68 1.1 perseant off_t secsize, sboff; 69 1.4 riz struct disk_geom geo; 70 1.4 riz struct dkwedge_info dkw; 71 1.1 perseant struct lfs *fs; 72 1.1 perseant struct statvfs vfs; 73 1.1 perseant 74 1.1 perseant /* Initialize and parse arguments */ 75 1.2 lukem verbose = 0; 76 1.1 perseant newsize = 0; 77 1.1 perseant while ((ch = getopt(argc, argv, "s:v")) != -1) { 78 1.1 perseant switch(ch) { 79 1.1 perseant case 's': /* New size, in sectors */ 80 1.1 perseant newsize = strtoll(optarg, NULL, 10); 81 1.1 perseant break; 82 1.1 perseant case 'v': 83 1.1 perseant ++verbose; 84 1.1 perseant break; 85 1.1 perseant default: 86 1.1 perseant usage(); 87 1.1 perseant } 88 1.1 perseant } 89 1.1 perseant fsname = argv[optind]; 90 1.1 perseant 91 1.1 perseant if (fsname == NULL) 92 1.1 perseant usage(); 93 1.1 perseant 94 1.1 perseant /* 95 1.1 perseant * If the user did not supply a filesystem size, use the 96 1.1 perseant * length of the mounted partition. 97 1.1 perseant */ 98 1.1 perseant if (statvfs(fsname, &vfs) < 0) 99 1.1 perseant err(1, "%s", fsname); 100 1.8 dholland rdevlen = strlen(vfs.f_mntfromname) + 2; 101 1.8 dholland rdev = malloc(rdevlen); 102 1.15 brad if (getdiskrawname(rdev, rdevlen, vfs.f_mntfromname) == NULL) 103 1.15 brad err(1, "Could not convert '%s' to raw name", vfs.f_mntfromname); 104 1.1 perseant devfd = open(rdev, O_RDONLY); 105 1.1 perseant if (devfd < 0) 106 1.1 perseant err(1, "open raw device"); 107 1.1 perseant 108 1.1 perseant /* 109 1.1 perseant * Read the disklabel to find the sector size. Check the 110 1.1 perseant * given size against the partition size. We can skip some 111 1.1 perseant * error checking here since we know the fs is mountable. 112 1.1 perseant */ 113 1.4 riz if (getdiskinfo(rdev, devfd, NULL, &geo, &dkw) == -1) 114 1.4 riz err(1, "%s: could not get info", rdev); 115 1.4 riz secsize = geo.dg_secsize; 116 1.4 riz if (newsize > dkw.dkw_size) 117 1.1 perseant errx(1, "new size must be <= the partition size"); 118 1.1 perseant if (newsize == 0) 119 1.4 riz newsize = dkw.dkw_size; 120 1.1 perseant 121 1.1 perseant /* Open the root of the filesystem so we can fcntl() it */ 122 1.1 perseant rootfd = open(fsname, O_RDONLY); 123 1.1 perseant if (rootfd < 0) 124 1.1 perseant err(1, "open filesystem root"); 125 1.1 perseant 126 1.1 perseant /* Read the superblock, finding alternates if necessary */ 127 1.16 mrg fs = (struct lfs *)calloc(sizeof(*fs), 1); 128 1.1 perseant for (sboff = LFS_LABELPAD;;) { 129 1.1 perseant pread(devfd, buf, sboff, LFS_SBPAD); 130 1.14 dholland __CTASSERT(sizeof(struct dlfs) == sizeof(struct dlfs64)); 131 1.14 dholland memcpy(&fs->lfs_dlfs_u, buf, sizeof(struct dlfs)); 132 1.7 christos if (sboff == LFS_LABELPAD && lfs_fsbtob(fs, 1) > LFS_LABELPAD) 133 1.11 dholland sboff = lfs_fsbtob(fs, (off_t)lfs_sb_getsboff(fs, 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.10 dholland newnsegs = (newsize * secsize) / lfs_sb_getssize(fs); 141 1.11 dholland if (newnsegs == lfs_sb_getnseg(fs)) { 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.11 dholland for (i = lfs_sb_getnseg(fs) - 1; i >= newnsegs; --i) { 152 1.9 dholland char cmd[128]; 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.9 dholland snprintf(cmd, sizeof(cmd), "/libexec/lfs_cleanerd -q -i %d %s", 159 1.9 dholland i, fsname); 160 1.1 perseant if (system(cmd) != 0) 161 1.1 perseant err(1, "invalidating segment %d", i); 162 1.1 perseant } 163 1.1 perseant 164 1.1 perseant /* Tell the filesystem to resize itself. */ 165 1.1 perseant if (fcntl(rootfd, LFCNRESIZE, &newnsegs) == -1) { 166 1.1 perseant err(1, "resizing"); 167 1.1 perseant } 168 1.1 perseant 169 1.1 perseant if (verbose) 170 1.11 dholland printf("Successfully resized %s from %u to %lld segments\n", 171 1.11 dholland fsname, lfs_sb_getnseg(fs), (long long)newnsegs); 172 1.1 perseant return 0; 173 1.1 perseant } 174