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