1 /*-
2 * Copyright (c) 2002, 2003 Gordon Tetlow
3 * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD: releng/11.1/sys/geom/label/g_label_ufs.c 265351 2014-05-05 09:20:30Z trasz $");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35
36 #include <ufs/ufs/dinode.h>
37 #include <ufs/ffs/fs.h>
38
39 #include <geom/geom.h>
40 #include <geom/label/g_label.h>
41
42 #define G_LABEL_UFS_VOLUME_DIR "ufs"
43 #define G_LABEL_UFS_ID_DIR "ufsid"
44
45 #define G_LABEL_UFS_VOLUME 0
46 #define G_LABEL_UFS_ID 1
47
48 static const int superblocks[] = SBLOCKSEARCH;
49
50 static void
51 g_label_ufs_taste_common(struct g_consumer *cp, char *label, size_t size, int what)
52 {
53 struct g_provider *pp;
54 int sb, superblock;
55 struct fs *fs;
56
57 g_topology_assert_not();
58 pp = cp->provider;
59 label[0] = '\0';
60
61 if (SBLOCKSIZE % cp->provider->sectorsize != 0)
62 return;
63
64 /*
65 * Walk through the standard places that superblocks hide and look
66 * for UFS magic. If we find magic, then check that the size in the
67 * superblock corresponds to the size of the underlying provider.
68 * Finally, look for a volume label and create an appropriate
69 * provider based on that.
70 */
71 for (sb = 0; (superblock = superblocks[sb]) != -1; sb++) {
72 /*
73 * Take care not to issue an invalid I/O request. The offset of
74 * the superblock candidate must be multiples of the provider's
75 * sector size, otherwise an FFS can't exist on the provider
76 * anyway.
77 */
78 if (superblock % cp->provider->sectorsize != 0)
79 continue;
80
81 fs = (struct fs *)g_read_data(cp, superblock, SBLOCKSIZE, NULL);
82 if (fs == NULL)
83 continue;
84 /*
85 * Check for magic. We also need to check if file system size is equal
86 * to providers size, because sysinstall(8) used to bogusly put first
87 * partition at offset 0 instead of 16, and glabel/ufs would find file
88 * system on slice instead of partition.
89 */
90 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_fsize > 0 &&
91 ((pp->mediasize / fs->fs_fsize == fs->fs_old_size) ||
92 (pp->mediasize / fs->fs_fsize == fs->fs_providersize))) {
93 /* Valid UFS1. */
94 } else if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_fsize > 0 &&
95 ((pp->mediasize / fs->fs_fsize == fs->fs_size) ||
96 (pp->mediasize / fs->fs_fsize == fs->fs_providersize))) {
97 /* Valid UFS2. */
98 } else {
99 g_free(fs);
100 continue;
101 }
102 if (fs->fs_sblockloc != superblock || fs->fs_ncg < 1 ||
103 fs->fs_bsize < MINBSIZE ||
104 fs->fs_bsize < sizeof(struct fs)) {
105 g_free(fs);
106 continue;
107 }
108 G_LABEL_DEBUG(1, "%s file system detected on %s.",
109 fs->fs_magic == FS_UFS1_MAGIC ? "UFS1" : "UFS2", pp->name);
110 switch (what) {
111 case G_LABEL_UFS_VOLUME:
112 /* Check for volume label */
113 if (fs->fs_volname[0] == '\0') {
114 g_free(fs);
115 continue;
116 }
117 strlcpy(label, fs->fs_volname, size);
118 break;
119 case G_LABEL_UFS_ID:
120 if (fs->fs_id[0] == 0 && fs->fs_id[1] == 0) {
121 g_free(fs);
122 continue;
123 }
124 snprintf(label, size, "%08x%08x", fs->fs_id[0],
125 fs->fs_id[1]);
126 break;
127 }
128 g_free(fs);
129 break;
130 }
131 }
132
133 static void
134 g_label_ufs_volume_taste(struct g_consumer *cp, char *label, size_t size)
135 {
136
137 g_label_ufs_taste_common(cp, label, size, G_LABEL_UFS_VOLUME);
138 }
139
140 static void
141 g_label_ufs_id_taste(struct g_consumer *cp, char *label, size_t size)
142 {
143
144 g_label_ufs_taste_common(cp, label, size, G_LABEL_UFS_ID);
145 }
146
147 struct g_label_desc g_label_ufs_volume = {
148 .ld_taste = g_label_ufs_volume_taste,
149 .ld_dir = G_LABEL_UFS_VOLUME_DIR,
150 .ld_enabled = 1
151 };
152
153 struct g_label_desc g_label_ufs_id = {
154 .ld_taste = g_label_ufs_id_taste,
155 .ld_dir = G_LABEL_UFS_ID_DIR,
156 .ld_enabled = 1
157 };
158
159 G_LABEL_INIT(ufsid, g_label_ufs_id, "Create device nodes for UFS file system IDs");
160 G_LABEL_INIT(ufs, g_label_ufs_volume, "Create device nodes for UFS volume names");
Cache object: eb32a80e307738f9da82b8677930a936
|