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;
84235989Strasz		/* Check for magic. We also need to check if file system size is equal
85235989Strasz		 * to providers size, because sysinstall(8) used to bogusly put first
86235989Strasz		 * partition at offset 0 instead of 16, and glabel/ufs would find file
87235989Strasz		 * system on slice instead of partition.
88235989Strasz		 */
89235989Strasz		if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_fsize > 0 &&
90235989Strasz		    pp->mediasize / fs->fs_fsize == fs->fs_old_size) {
91156299Spjd		    	/* Valid UFS1. */
92235989Strasz		} else if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_fsize > 0 &&
93242379Strasz		    ((pp->mediasize / fs->fs_fsize == fs->fs_size) ||
94242379Strasz		    (pp->mediasize / fs->fs_fsize == fs->fs_providersize))) {
95156299Spjd		    	/* Valid UFS2. */
96156299Spjd		} else {
97131476Spjd			g_free(fs);
98131476Spjd			continue;
99131476Spjd		}
100155801Spjd		if (fs->fs_sblockloc != superblock || fs->fs_ncg < 1 ||
101155801Spjd		    fs->fs_bsize < MINBSIZE ||
102155801Spjd		    fs->fs_bsize < sizeof(struct fs)) {
103155801Spjd			g_free(fs);
104155801Spjd			continue;
105155801Spjd		}
106155801Spjd		G_LABEL_DEBUG(1, "%s file system detected on %s.",
107155801Spjd		    fs->fs_magic == FS_UFS1_MAGIC ? "UFS1" : "UFS2", pp->name);
108190423Sivoras		switch (what) {
109190423Sivoras		case G_LABEL_UFS_VOLUME:
110190423Sivoras			/* Check for volume label */
111190423Sivoras			if (fs->fs_volname[0] == '\0') {
112190423Sivoras				g_free(fs);
113190423Sivoras				continue;
114190423Sivoras			}
115190423Sivoras			strlcpy(label, fs->fs_volname, size);
116190423Sivoras			break;
117190423Sivoras		case G_LABEL_UFS_ID:
118190423Sivoras			if (fs->fs_id[0] == 0 && fs->fs_id[1] == 0) {
119190423Sivoras				g_free(fs);
120190423Sivoras				continue;
121190423Sivoras			}
122190423Sivoras			snprintf(label, size, "%08x%08x", fs->fs_id[0],
123190423Sivoras			    fs->fs_id[1]);
124190423Sivoras			break;
125131476Spjd		}
126131476Spjd		g_free(fs);
127131476Spjd		break;
128131476Spjd	}
129131476Spjd}
130131476Spjd
131190423Sivorasstatic void
132190423Sivorasg_label_ufs_volume_taste(struct g_consumer *cp, char *label, size_t size)
133190423Sivoras{
134190423Sivoras
135190423Sivoras	g_label_ufs_taste_common(cp, label, size, G_LABEL_UFS_VOLUME);
136190423Sivoras}
137190423Sivoras
138190423Sivorasstatic void
139190423Sivorasg_label_ufs_id_taste(struct g_consumer *cp, char *label, size_t size)
140190423Sivoras{
141190423Sivoras
142190423Sivoras	g_label_ufs_taste_common(cp, label, size, G_LABEL_UFS_ID);
143190423Sivoras}
144190423Sivoras
145199875Straszstruct g_label_desc g_label_ufs_volume = {
146190423Sivoras	.ld_taste = g_label_ufs_volume_taste,
147199875Strasz	.ld_dir = G_LABEL_UFS_VOLUME_DIR,
148199875Strasz	.ld_enabled = 1
149131476Spjd};
150190423Sivoras
151199875Straszstruct g_label_desc g_label_ufs_id = {
152190423Sivoras	.ld_taste = g_label_ufs_id_taste,
153199875Strasz	.ld_dir = G_LABEL_UFS_ID_DIR,
154199875Strasz	.ld_enabled = 1
155190423Sivoras};
156199875Strasz
157199875StraszG_LABEL_INIT(ufsid, g_label_ufs_id, "Create device nodes for UFS file system IDs");
158199875StraszG_LABEL_INIT(ufs, g_label_ufs_volume, "Create device nodes for UFS volume names");
159