geom_vol_ffs.c revision 125755
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: head/sys/geom/geom_vol_ffs.c 125755 2004-02-12 22:42:11Z phk $");
29
30#include <sys/param.h>
31#include <sys/errno.h>
32#include <sys/systm.h>
33#include <sys/kernel.h>
34#include <sys/malloc.h>
35#include <sys/bio.h>
36#include <sys/lock.h>
37#include <sys/mutex.h>
38
39#include <ufs/ufs/dinode.h>
40#include <ufs/ffs/fs.h>
41
42#include <geom/geom.h>
43#include <geom/geom_slice.h>
44
45#define VOL_FFS_CLASS_NAME "VOL_FFS"
46
47static int superblocks[] = SBLOCKSEARCH;
48
49struct g_vol_ffs_softc {
50	char *	vol;
51};
52
53static int
54g_vol_ffs_start(struct bio *bp __unused)
55{
56	return(0);
57}
58
59static struct g_geom *
60g_vol_ffs_taste(struct g_class *mp, struct g_provider *pp, int flags)
61{
62	struct g_geom *gp;
63	struct g_consumer *cp;
64	struct g_vol_ffs_softc *ms;
65	int error, sb, superblock;
66	struct fs *fs;
67
68	g_trace(G_T_TOPOLOGY, "vol_taste(%s,%s)", mp->name, pp->name);
69	g_topology_assert();
70
71	/*
72	 * XXX This is a really weak way to make sure we don't recurse.
73	 * Probably ought to use BIO_GETATTR to check for this.
74	 */
75	if (flags == G_TF_NORMAL &&
76	    !strcmp(pp->geom->class->name, VOL_FFS_CLASS_NAME))
77		return (NULL);
78
79	gp = g_slice_new(mp, 1, pp, &cp, &ms, sizeof(*ms), g_vol_ffs_start);
80	if (gp == NULL)
81		return (NULL);
82	g_topology_unlock();
83	/*
84	 * Walk through the standard places that superblocks hide and look
85	 * for UFS magic. If we find magic, then check that the size in the
86	 * superblock corresponds to the size of the underlying provider.
87	 * Finally, look for a volume label and create an appropriate
88	 * provider based on that.
89	 */
90	for (sb=0; (superblock = superblocks[sb]) != -1; sb++) {
91		fs = (struct fs *) g_read_data(cp, superblock,
92			SBLOCKSIZE, &error);
93		if (fs == NULL || error != 0)
94			continue;
95		/* Check for magic and make sure things are the right size */
96		if (fs->fs_magic == FS_UFS1_MAGIC) {
97			if (fs->fs_old_size * fs->fs_fsize !=
98			    (int32_t) pp->mediasize) {
99				g_free(fs);
100				continue;
101			}
102		} else if (fs->fs_magic == FS_UFS2_MAGIC) {
103			if (fs->fs_size * fs->fs_fsize !=
104			    (int64_t) pp->mediasize) {
105				g_free(fs);
106				continue;
107			}
108		} else {
109			g_free(fs);
110			continue;
111		}
112		/* Check for volume label */
113		if (fs->fs_volname[0] == '\0') {
114			g_free(fs);
115			continue;
116		}
117		/* XXX We need to check for namespace conflicts. */
118		/* XXX How do you handle a mirror set? */
119		/* XXX We don't validate the volume name. */
120		g_topology_lock();
121		/* Alright, we have a label and a volume name, reconfig. */
122		g_slice_config(gp, 0, G_SLICE_CONFIG_SET, (off_t) 0,
123		    pp->mediasize, pp->sectorsize, "vol/%s",
124		    fs->fs_volname);
125		g_free(fs);
126		g_topology_unlock();
127		break;
128	}
129	g_topology_lock();
130	g_access(cp, -1, 0, 0);
131	if (LIST_EMPTY(&gp->provider)) {
132		g_slice_spoiled(cp);
133		return (NULL);
134	}
135	return (gp);
136}
137
138static struct g_class g_vol_ffs_class	= {
139	.name = VOL_FFS_CLASS_NAME,
140	.taste = g_vol_ffs_taste,
141};
142
143DECLARE_GEOM_CLASS(g_vol_ffs_class, g_vol_ffs);
144