2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002, 2003 Gordon Tetlow
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/errno.h>
34 #include <sys/systm.h>
35 #include <sys/sysctl.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
40 #include <sys/mutex.h>
42 #include <ufs/ufs/dinode.h>
43 #include <ufs/ffs/fs.h>
45 #include <geom/geom.h>
46 #include <geom/geom_slice.h>
48 FEATURE(geom_vol, "GEOM support for volume names from UFS superblock");
50 #define VOL_FFS_CLASS_NAME "VOL_FFS"
52 static int superblocks[] = SBLOCKSEARCH;
53 static int g_vol_ffs_once;
55 struct g_vol_ffs_softc {
60 g_vol_ffs_start(struct bio *bp __unused)
65 static struct g_geom *
66 g_vol_ffs_taste(struct g_class *mp, struct g_provider *pp, int flags)
69 struct g_consumer *cp;
70 struct g_vol_ffs_softc *ms;
74 g_trace(G_T_TOPOLOGY, "vol_taste(%s,%s)", mp->name, pp->name);
78 * XXX This is a really weak way to make sure we don't recurse.
79 * Probably ought to use BIO_GETATTR to check for this.
81 if (flags == G_TF_NORMAL &&
82 !strcmp(pp->geom->class->name, VOL_FFS_CLASS_NAME))
85 gp = g_slice_new(mp, 1, pp, &cp, &ms, sizeof(*ms), g_vol_ffs_start);
90 * Walk through the standard places that superblocks hide and look
91 * for UFS magic. If we find magic, then check that the size in the
92 * superblock corresponds to the size of the underlying provider.
93 * Finally, look for a volume label and create an appropriate
94 * provider based on that.
96 for (sb=0; (superblock = superblocks[sb]) != -1; sb++) {
98 * Take care not to issue an invalid I/O request. The
99 * offset and size of the superblock candidate must be
100 * multiples of the provider's sector size, otherwise an
101 * FFS can't exist on the provider anyway.
103 if (superblock % cp->provider->sectorsize != 0 ||
104 SBLOCKSIZE % cp->provider->sectorsize != 0)
107 fs = (struct fs *) g_read_data(cp, superblock,
111 /* Check for magic and make sure things are the right size */
112 if (fs->fs_magic == FS_UFS1_MAGIC) {
113 if (fs->fs_old_size * fs->fs_fsize !=
114 (int32_t) pp->mediasize) {
118 } else if (fs->fs_magic == FS_UFS2_MAGIC) {
119 if (fs->fs_size * fs->fs_fsize !=
120 (int64_t) pp->mediasize) {
128 /* Check for volume label */
129 if (fs->fs_volname[0] == '\0') {
133 /* XXX We need to check for namespace conflicts. */
134 /* XXX How do you handle a mirror set? */
135 /* XXX We don't validate the volume name. */
137 /* Alright, we have a label and a volume name, reconfig. */
138 g_slice_config(gp, 0, G_SLICE_CONFIG_SET, (off_t) 0,
139 pp->mediasize, pp->sectorsize, "vol/%s",
146 g_access(cp, -1, 0, 0);
147 if (LIST_EMPTY(&gp->provider)) {
151 if (!g_vol_ffs_once) {
154 "WARNING: geom_vol_Ffs (geom %s) is deprecated, "
155 "use glabel instead.\n", gp->name);
160 static struct g_class g_vol_ffs_class = {
161 .name = VOL_FFS_CLASS_NAME,
162 .version = G_VERSION,
163 .taste = g_vol_ffs_taste,
166 DECLARE_GEOM_CLASS(g_vol_ffs_class, g_vol_ffs);
167 MODULE_VERSION(geom_vol_ffs, 0);