1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2002, 2003 Gordon Tetlow
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
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>
38#include <sys/bio.h>
39#include <sys/lock.h>
40#include <sys/mutex.h>
41
42#include <ufs/ufs/dinode.h>
43#include <ufs/ffs/fs.h>
44
45#include <geom/geom.h>
46#include <geom/geom_slice.h>
47
48FEATURE(geom_vol, "GEOM support for volume names from UFS superblock");
49
50#define VOL_FFS_CLASS_NAME "VOL_FFS"
51
52static int superblocks[] = SBLOCKSEARCH;
53static int g_vol_ffs_once;
54
55struct g_vol_ffs_softc {
56	char *	vol;
57};
58
59static int
60g_vol_ffs_start(struct bio *bp __unused)
61{
62	return(0);
63}
64
65static struct g_geom *
66g_vol_ffs_taste(struct g_class *mp, struct g_provider *pp, int flags)
67{
68	struct g_geom *gp;
69	struct g_consumer *cp;
70	struct g_vol_ffs_softc *ms;
71	int sb, superblock;
72	struct fs *fs;
73
74	g_trace(G_T_TOPOLOGY, "vol_taste(%s,%s)", mp->name, pp->name);
75	g_topology_assert();
76
77	/*
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.
80	 */
81	if (flags == G_TF_NORMAL &&
82	    !strcmp(pp->geom->class->name, VOL_FFS_CLASS_NAME))
83		return (NULL);
84
85	gp = g_slice_new(mp, 1, pp, &cp, &ms, sizeof(*ms), g_vol_ffs_start);
86	if (gp == NULL)
87		return (NULL);
88	g_topology_unlock();
89	/*
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.
95	 */
96	for (sb=0; (superblock = superblocks[sb]) != -1; sb++) {
97		/*
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.
102		 */
103		if (superblock % cp->provider->sectorsize != 0 ||
104		    SBLOCKSIZE % cp->provider->sectorsize != 0)
105			continue;
106
107		fs = (struct fs *) g_read_data(cp, superblock,
108			SBLOCKSIZE, NULL);
109		if (fs == NULL)
110			continue;
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) {
115				g_free(fs);
116				continue;
117			}
118		} else if (fs->fs_magic == FS_UFS2_MAGIC) {
119			if (fs->fs_size * fs->fs_fsize !=
120			    (int64_t) pp->mediasize) {
121				g_free(fs);
122				continue;
123			}
124		} else {
125			g_free(fs);
126			continue;
127		}
128		/* Check for volume label */
129		if (fs->fs_volname[0] == '\0') {
130			g_free(fs);
131			continue;
132		}
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. */
136		g_topology_lock();
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",
140		    fs->fs_volname);
141		g_free(fs);
142		g_topology_unlock();
143		break;
144	}
145	g_topology_lock();
146	g_access(cp, -1, 0, 0);
147	if (LIST_EMPTY(&gp->provider)) {
148		g_slice_spoiled(cp);
149		return (NULL);
150	}
151	if (!g_vol_ffs_once) {
152		g_vol_ffs_once = 1;
153		printf(
154		    "WARNING: geom_vol_Ffs (geom %s) is deprecated, "
155		    "use glabel instead.\n", gp->name);
156	}
157	return (gp);
158}
159
160static struct g_class g_vol_ffs_class	= {
161	.name = VOL_FFS_CLASS_NAME,
162	.version = G_VERSION,
163	.taste = g_vol_ffs_taste,
164};
165
166DECLARE_GEOM_CLASS(g_vol_ffs_class, g_vol_ffs);
167MODULE_VERSION(geom_vol_ffs, 0);
168