1/*- 2 * Copyright (c) 2002, 2003 Gordon Tetlow 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD$"); 29 30#include <sys/param.h> 31#include <sys/errno.h> 32#include <sys/systm.h> 33#include <sys/sysctl.h> 34#include <sys/kernel.h> 35#include <sys/malloc.h> 36#include <sys/bio.h> 37#include <sys/lock.h> 38#include <sys/mutex.h> 39 40#include <ufs/ufs/dinode.h> 41#include <ufs/ffs/fs.h> 42 43#include <geom/geom.h> 44#include <geom/geom_slice.h> 45 46FEATURE(geom_vol, "GEOM support for volume names from UFS superblock"); 47 48#define VOL_FFS_CLASS_NAME "VOL_FFS" 49 50static int superblocks[] = SBLOCKSEARCH; 51 52struct g_vol_ffs_softc { 53 char * vol; 54}; 55 56static int 57g_vol_ffs_start(struct bio *bp __unused) 58{ 59 return(0); 60} 61 62static struct g_geom * 63g_vol_ffs_taste(struct g_class *mp, struct g_provider *pp, int flags) 64{ 65 struct g_geom *gp; 66 struct g_consumer *cp; 67 struct g_vol_ffs_softc *ms; 68 int sb, superblock; 69 struct fs *fs; 70 71 g_trace(G_T_TOPOLOGY, "vol_taste(%s,%s)", mp->name, pp->name); 72 g_topology_assert(); 73 74 /* 75 * XXX This is a really weak way to make sure we don't recurse. 76 * Probably ought to use BIO_GETATTR to check for this. 77 */ 78 if (flags == G_TF_NORMAL && 79 !strcmp(pp->geom->class->name, VOL_FFS_CLASS_NAME)) 80 return (NULL); 81 82 gp = g_slice_new(mp, 1, pp, &cp, &ms, sizeof(*ms), g_vol_ffs_start); 83 if (gp == NULL) 84 return (NULL); 85 g_topology_unlock(); 86 /* 87 * Walk through the standard places that superblocks hide and look 88 * for UFS magic. If we find magic, then check that the size in the 89 * superblock corresponds to the size of the underlying provider. 90 * Finally, look for a volume label and create an appropriate 91 * provider based on that. 92 */ 93 for (sb=0; (superblock = superblocks[sb]) != -1; sb++) { 94 /* 95 * Take care not to issue an invalid I/O request. The 96 * offset and size of the superblock candidate must be 97 * multiples of the provider's sector size, otherwise an 98 * FFS can't exist on the provider anyway. 99 */ 100 if (superblock % cp->provider->sectorsize != 0 || 101 SBLOCKSIZE % cp->provider->sectorsize != 0) 102 continue; 103 104 fs = (struct fs *) g_read_data(cp, superblock, 105 SBLOCKSIZE, NULL); 106 if (fs == NULL) 107 continue; 108 /* Check for magic and make sure things are the right size */ 109 if (fs->fs_magic == FS_UFS1_MAGIC) { 110 if (fs->fs_old_size * fs->fs_fsize != 111 (int32_t) pp->mediasize) { 112 g_free(fs); 113 continue; 114 } 115 } else if (fs->fs_magic == FS_UFS2_MAGIC) { 116 if (fs->fs_size * fs->fs_fsize != 117 (int64_t) pp->mediasize) { 118 g_free(fs); 119 continue; 120 } 121 } else { 122 g_free(fs); 123 continue; 124 } 125 /* Check for volume label */ 126 if (fs->fs_volname[0] == '\0') { 127 g_free(fs); 128 continue; 129 } 130 /* XXX We need to check for namespace conflicts. */ 131 /* XXX How do you handle a mirror set? */ 132 /* XXX We don't validate the volume name. */ 133 g_topology_lock(); 134 /* Alright, we have a label and a volume name, reconfig. */ 135 g_slice_config(gp, 0, G_SLICE_CONFIG_SET, (off_t) 0, 136 pp->mediasize, pp->sectorsize, "vol/%s", 137 fs->fs_volname); 138 g_free(fs); 139 g_topology_unlock(); 140 break; 141 } 142 g_topology_lock(); 143 g_access(cp, -1, 0, 0); 144 if (LIST_EMPTY(&gp->provider)) { 145 g_slice_spoiled(cp); 146 return (NULL); 147 } 148 return (gp); 149} 150 151static struct g_class g_vol_ffs_class = { 152 .name = VOL_FFS_CLASS_NAME, 153 .version = G_VERSION, 154 .taste = g_vol_ffs_taste, 155}; 156 157DECLARE_GEOM_CLASS(g_vol_ffs_class, g_vol_ffs); 158