1131476Spjd/*-
2131476Spjd * Copyright (c) 2002, 2003 Gordon Tetlow
3155801Spjd * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4131476Spjd * All rights reserved.
5131476Spjd *
6131476Spjd * Redistribution and use in source and binary forms, with or without
7131476Spjd * modification, are permitted provided that the following conditions
8131476Spjd * are met:
9131476Spjd * 1. Redistributions of source code must retain the above copyright
10131476Spjd *    notice, this list of conditions and the following disclaimer.
11131476Spjd * 2. Redistributions in binary form must reproduce the above copyright
12131476Spjd *    notice, this list of conditions and the following disclaimer in the
13131476Spjd *    documentation and/or other materials provided with the distribution.
14131476Spjd *
15155801Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16131476Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17131476Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18155801Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19131476Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20131476Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21131476Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22131476Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23131476Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24131476Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25131476Spjd * SUCH DAMAGE.
26131476Spjd */
27131476Spjd
28131476Spjd#include <sys/cdefs.h>
29131476Spjd__FBSDID("$FreeBSD$");
30131476Spjd
31131476Spjd#include <sys/param.h>
32131476Spjd#include <sys/systm.h>
33131476Spjd#include <sys/kernel.h>
34131476Spjd#include <sys/malloc.h>
35131476Spjd
36131476Spjd#include <ufs/ufs/dinode.h>
37131476Spjd#include <ufs/ffs/fs.h>
38131476Spjd
39131476Spjd#include <geom/geom.h>
40131476Spjd#include <geom/label/g_label.h>
41131476Spjd
42190423Sivoras#define G_LABEL_UFS_VOLUME_DIR	"ufs"
43190423Sivoras#define G_LABEL_UFS_ID_DIR	"ufsid"
44131476Spjd
45190423Sivoras#define	G_LABEL_UFS_VOLUME	0
46190423Sivoras#define	G_LABEL_UFS_ID		1
47190423Sivoras
48131476Spjdstatic const int superblocks[] = SBLOCKSEARCH;
49131476Spjd
50131476Spjdstatic void
51190423Sivorasg_label_ufs_taste_common(struct g_consumer *cp, char *label, size_t size, int what)
52131476Spjd{
53131476Spjd	struct g_provider *pp;
54152971Ssobomax	int sb, superblock;
55131476Spjd	struct fs *fs;
56131476Spjd
57131476Spjd	g_topology_assert_not();
58131476Spjd	pp = cp->provider;
59131476Spjd	label[0] = '\0';
60155797Spjd
61155797Spjd	if (SBLOCKSIZE % cp->provider->sectorsize != 0)
62155798Spjd		return;
63155797Spjd
64131476Spjd	/*
65131476Spjd	 * Walk through the standard places that superblocks hide and look
66131476Spjd	 * for UFS magic. If we find magic, then check that the size in the
67131476Spjd	 * superblock corresponds to the size of the underlying provider.
68155174Spjd	 * Finally, look for a volume label and create an appropriate
69131476Spjd	 * provider based on that.
70131476Spjd	 */
71131476Spjd	for (sb = 0; (superblock = superblocks[sb]) != -1; sb++) {
72141513Sdes		/*
73155797Spjd		 * Take care not to issue an invalid I/O request. The offset of
74155797Spjd		 * the superblock candidate must be multiples of the provider's
75155797Spjd		 * sector size, otherwise an FFS can't exist on the provider
76155797Spjd		 * anyway.
77141513Sdes		 */
78155797Spjd		if (superblock % cp->provider->sectorsize != 0)
79141513Sdes			continue;
80141513Sdes
81155797Spjd		fs = (struct fs *)g_read_data(cp, superblock, SBLOCKSIZE, NULL);
82152967Ssobomax		if (fs == NULL)
83131476Spjd			continue;
84131476Spjd		/* Check for magic and make sure things are the right size */
85162357Spjd		if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_fsize > 0 &&
86162357Spjd		    pp->mediasize / fs->fs_fsize == fs->fs_old_size) {
87156299Spjd		    	/* Valid UFS1. */
88156299Spjd		} else if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_fsize > 0 &&
89246284Strasz		    ((pp->mediasize / fs->fs_fsize == fs->fs_size) ||
90246284Strasz		    (pp->mediasize / fs->fs_fsize == fs->fs_providersize))) {
91156299Spjd		    	/* Valid UFS2. */
92156299Spjd		} else {
93131476Spjd			g_free(fs);
94131476Spjd			continue;
95131476Spjd		}
96155801Spjd		if (fs->fs_sblockloc != superblock || fs->fs_ncg < 1 ||
97155801Spjd		    fs->fs_bsize < MINBSIZE ||
98155801Spjd		    fs->fs_bsize < sizeof(struct fs)) {
99155801Spjd			g_free(fs);
100155801Spjd			continue;
101155801Spjd		}
102155801Spjd		G_LABEL_DEBUG(1, "%s file system detected on %s.",
103155801Spjd		    fs->fs_magic == FS_UFS1_MAGIC ? "UFS1" : "UFS2", pp->name);
104190423Sivoras		switch (what) {
105190423Sivoras		case G_LABEL_UFS_VOLUME:
106190423Sivoras			/* Check for volume label */
107190423Sivoras			if (fs->fs_volname[0] == '\0') {
108190423Sivoras				g_free(fs);
109190423Sivoras				continue;
110190423Sivoras			}
111190423Sivoras			strlcpy(label, fs->fs_volname, size);
112190423Sivoras			break;
113190423Sivoras		case G_LABEL_UFS_ID:
114190423Sivoras			if (fs->fs_id[0] == 0 && fs->fs_id[1] == 0) {
115190423Sivoras				g_free(fs);
116190423Sivoras				continue;
117190423Sivoras			}
118190423Sivoras			snprintf(label, size, "%08x%08x", fs->fs_id[0],
119190423Sivoras			    fs->fs_id[1]);
120190423Sivoras			break;
121131476Spjd		}
122131476Spjd		g_free(fs);
123131476Spjd		break;
124131476Spjd	}
125131476Spjd}
126131476Spjd
127190423Sivorasstatic void
128190423Sivorasg_label_ufs_volume_taste(struct g_consumer *cp, char *label, size_t size)
129190423Sivoras{
130190423Sivoras
131190423Sivoras	g_label_ufs_taste_common(cp, label, size, G_LABEL_UFS_VOLUME);
132190423Sivoras}
133190423Sivoras
134190423Sivorasstatic void
135190423Sivorasg_label_ufs_id_taste(struct g_consumer *cp, char *label, size_t size)
136190423Sivoras{
137190423Sivoras
138190423Sivoras	g_label_ufs_taste_common(cp, label, size, G_LABEL_UFS_ID);
139190423Sivoras}
140190423Sivoras
141199875Straszstruct g_label_desc g_label_ufs_volume = {
142190423Sivoras	.ld_taste = g_label_ufs_volume_taste,
143199875Strasz	.ld_dir = G_LABEL_UFS_VOLUME_DIR,
144199875Strasz	.ld_enabled = 1
145131476Spjd};
146190423Sivoras
147199875Straszstruct g_label_desc g_label_ufs_id = {
148190423Sivoras	.ld_taste = g_label_ufs_id_taste,
149199875Strasz	.ld_dir = G_LABEL_UFS_ID_DIR,
150199875Strasz	.ld_enabled = 1
151190423Sivoras};
152199875Strasz
153199875StraszG_LABEL_INIT(ufsid, g_label_ufs_id, "Create device nodes for UFS file system IDs");
154199875StraszG_LABEL_INIT(ufs, g_label_ufs_volume, "Create device nodes for UFS volume names");
155