2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002, 2003 Gordon Tetlow
5 * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
38 #include <ufs/ufs/dinode.h>
39 #include <ufs/ffs/fs.h>
41 #include <geom/geom.h>
42 #include <geom/label/g_label.h>
44 #define G_LABEL_UFS_VOLUME_DIR "ufs"
45 #define G_LABEL_UFS_ID_DIR "ufsid"
47 #define G_LABEL_UFS_VOLUME 0
48 #define G_LABEL_UFS_ID 1
51 * G_LABEL_UFS_CMP returns true if difference between provider mediasize
52 * and filesystem size is less than G_LABEL_UFS_MAXDIFF sectors
54 #define G_LABEL_UFS_CMP(prov, fsys, size) \
55 ( abs( ((fsys)->size) - ( (prov)->mediasize / (fsys)->fs_fsize )) \
56 < G_LABEL_UFS_MAXDIFF )
57 #define G_LABEL_UFS_MAXDIFF 0x100
59 static const int superblocks[] = SBLOCKSEARCH;
62 g_label_ufs_taste_common(struct g_consumer *cp, char *label, size_t size, int what)
64 struct g_provider *pp;
68 g_topology_assert_not();
72 if (SBLOCKSIZE % cp->provider->sectorsize != 0)
76 * Walk through the standard places that superblocks hide and look
77 * for UFS magic. If we find magic, then check that the size in the
78 * superblock corresponds to the size of the underlying provider.
79 * Finally, look for a volume label and create an appropriate
80 * provider based on that.
82 for (sb = 0; (superblock = superblocks[sb]) != -1; sb++) {
84 * Take care not to issue an invalid I/O request. The offset of
85 * the superblock candidate must be multiples of the provider's
86 * sector size, otherwise an FFS can't exist on the provider
89 if (superblock % cp->provider->sectorsize != 0)
92 fs = (struct fs *)g_read_data(cp, superblock, SBLOCKSIZE, NULL);
96 * Check for magic. We also need to check if file system size
97 * is almost equal to providers size, because sysinstall(8)
98 * used to bogusly put first partition at offset 0
99 * instead of 16, and glabel/ufs would find file system on slice
100 * instead of partition.
102 * In addition, media size can be a bit bigger than file system
103 * size. For instance, mkuzip can append bytes to align data
104 * to large sector size (it improves compression rates).
106 switch (fs->fs_magic){
109 G_LABEL_DEBUG(1, "%s %s params: %jd, %d, %d, %jd\n",
110 fs->fs_magic == FS_UFS1_MAGIC ? "UFS1" : "UFS2",
111 pp->name, pp->mediasize, fs->fs_fsize,
112 fs->fs_old_size, fs->fs_providersize);
118 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_fsize > 0 &&
119 ( G_LABEL_UFS_CMP(pp, fs, fs_old_size)
120 || G_LABEL_UFS_CMP(pp, fs, fs_providersize))) {
122 } else if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_fsize > 0 &&
123 ( G_LABEL_UFS_CMP(pp, fs, fs_size)
124 || G_LABEL_UFS_CMP(pp, fs, fs_providersize))) {
130 if (fs->fs_sblockloc != superblock || fs->fs_ncg < 1 ||
131 fs->fs_bsize < MINBSIZE ||
132 fs->fs_bsize < sizeof(struct fs)) {
136 G_LABEL_DEBUG(1, "%s file system detected on %s.",
137 fs->fs_magic == FS_UFS1_MAGIC ? "UFS1" : "UFS2", pp->name);
139 case G_LABEL_UFS_VOLUME:
140 /* Check for volume label */
141 if (fs->fs_volname[0] == '\0') {
145 strlcpy(label, fs->fs_volname, size);
148 if (fs->fs_id[0] == 0 && fs->fs_id[1] == 0) {
152 snprintf(label, size, "%08x%08x", fs->fs_id[0],
162 g_label_ufs_volume_taste(struct g_consumer *cp, char *label, size_t size)
165 g_label_ufs_taste_common(cp, label, size, G_LABEL_UFS_VOLUME);
169 g_label_ufs_id_taste(struct g_consumer *cp, char *label, size_t size)
172 g_label_ufs_taste_common(cp, label, size, G_LABEL_UFS_ID);
175 struct g_label_desc g_label_ufs_volume = {
176 .ld_taste = g_label_ufs_volume_taste,
177 .ld_dir = G_LABEL_UFS_VOLUME_DIR,
181 struct g_label_desc g_label_ufs_id = {
182 .ld_taste = g_label_ufs_id_taste,
183 .ld_dir = G_LABEL_UFS_ID_DIR,
187 G_LABEL_INIT(ufsid, g_label_ufs_id, "Create device nodes for UFS file system IDs");
188 G_LABEL_INIT(ufs, g_label_ufs_volume, "Create device nodes for UFS volume names");