g_label_ext2fs.c revision 149395
1248484Sneel/*-
2248484Sneel * Copyright (c) 2005 Stanislav Sedov
3248484Sneel * All rights reserved.
4248484Sneel *
5248484Sneel * Redistribution and use in source and binary forms, with or without
6248484Sneel * modification, are permitted provided that the following conditions
7248484Sneel * are met:
8248484Sneel * 1. Redistributions of source code must retain the above copyright
9248484Sneel *    notice, this list of conditions and the following disclaimer.
10248484Sneel * 2. Redistributions in binary form must reproduce the above copyright
11248484Sneel *    notice, this list of conditions and the following disclaimer in the
12248484Sneel *    documentation and/or other materials provided with the distribution.
13248484Sneel *
14248484Sneel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15248484Sneel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16248484Sneel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17248484Sneel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18248484Sneel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19248484Sneel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20248484Sneel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21248484Sneel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22248484Sneel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23248484Sneel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24248484Sneel * SUCH DAMAGE.
25248484Sneel */
26248484Sneel
27248484Sneel#include <sys/cdefs.h>
28248484Sneel__FBSDID("$FreeBSD: head/sys/geom/label/g_label_ext2fs.c 149395 2005-08-23 18:55:38Z pjd $");
29248484Sneel
30248484Sneel#include <sys/param.h>
31248484Sneel#include <sys/systm.h>
32248484Sneel#include <sys/kernel.h>
33248484Sneel#include <sys/malloc.h>
34256176Sneel
35248484Sneel#include <geom/geom.h>
36248484Sneel#include <geom/label/g_label.h>
37248484Sneel
38248484Sneel#define EXT2FS_SB_OFFSET	1024
39248484Sneel#define EXT2_SUPER_MAGIC	0xef53
40248484Sneel#define EXT2_DYNAMIC_REV	1
41248484Sneel
42248840Sneeltypedef struct e2sb {
43248484Sneel	uint8_t		fake1[56];
44248484Sneel	uint16_t	s_magic;
45248484Sneel	uint8_t		fake2[18];
46248484Sneel	uint32_t	s_rev_level;
47248840Sneel	uint8_t		fake3[40];
48248484Sneel	char		s_volume_name[16];
49248484Sneel} e2sb_t;
50256176Sneel
51248484Sneelstatic void
52248484Sneelg_label_ext2fs_taste(struct g_consumer *cp, char *label, size_t size)
53248484Sneel{
54248484Sneel	struct g_provider *pp;
55248484Sneel	e2sb_t *fs;
56248484Sneel
57248484Sneel	g_topology_assert_not();
58248484Sneel	pp = cp->provider;
59248484Sneel	label[0] = '\0';
60248484Sneel
61248484Sneel	if ((EXT2FS_SB_OFFSET % pp->sectorsize) != 0)
62248484Sneel		return;
63248484Sneel
64248484Sneel	fs = (e2sb_t *)g_read_data(cp, EXT2FS_SB_OFFSET, pp->sectorsize, NULL);
65248484Sneel	if (fs == NULL)
66248484Sneel		return;
67248484Sneel
68248484Sneel	/* Check for magic and versio n*/
69248484Sneel	if (fs->s_magic == EXT2_SUPER_MAGIC &&
70248484Sneel	    fs->s_rev_level == EXT2_DYNAMIC_REV) {
71248484Sneel		G_LABEL_DEBUG(1, "ext2fs file system detected on %s.",
72248484Sneel		    pp->name);
73248484Sneel	} else {
74248484Sneel		goto exit_free;
75248840Sneel	}
76248484Sneel
77248840Sneel	/* Check for volume label */
78248484Sneel	if (fs->s_volume_name[0] == '\0')
79248484Sneel		goto exit_free;
80248484Sneel
81248484Sneel	/* Terminate label */
82248484Sneel	fs->s_volume_name[sizeof(fs->s_volume_name) - 1] = '\0';
83248484Sneel	strlcpy(label, fs->s_volume_name, size);
84248484Sneel
85248484Sneelexit_free:
86248484Sneel	g_free(fs);
87248484Sneel}
88248840Sneel
89248840Sneelconst struct g_label_desc g_label_ext2fs = {
90248484Sneel	.ld_taste = g_label_ext2fs_taste,
91248484Sneel	.ld_dir = "ext2fs"
92248484Sneel};
93248484Sneel