xref: /freebsd/sys/geom/label/g_label_ufs.c (revision 51e235148a4becba94e824a44bd69687644a7f56)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2002, 2003 Gordon Tetlow
5  * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/disklabel.h>
34 #include <sys/malloc.h>
35 #include <sys/vnode.h>
36 
37 #include <ufs/ufs/dinode.h>
38 #include <ufs/ffs/fs.h>
39 #include <ufs/ufs/quota.h>
40 #include <ufs/ufs/extattr.h>
41 #include <ufs/ffs/ffs_extern.h>
42 
43 #include <geom/geom.h>
44 #include <geom/geom_dbg.h>
45 #include <geom/label/g_label.h>
46 
47 #define	G_LABEL_UFS_VOLUME	0
48 #define	G_LABEL_UFS_ID		1
49 
50 /*
51  * G_LABEL_UFS_CMP returns true if difference between provider mediasize
52  * and filesystem size is less than G_LABEL_UFS_MAXDIFF sectors
53  */
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
58 
59 /*
60  * For providers that look like disklabels we need to check if the file system
61  * size is almost equal to the provider's size, because sysinstall(8) used to
62  * bogusly put the first partition at offset 0 instead of 16, and glabel/ufs
63  * would find a file system on the slice instead of the partition.
64  *
65  * In addition, media size can be a bit bigger than file system size. For
66  * instance, mkuzip can append bytes to align data to large sector size (it
67  * improves compression rates).
68  */
69 static bool
70 g_label_ufs_ignore_bsdlabel_slice(struct g_consumer *cp,
71     struct fs *fs)
72 {
73 	struct g_provider *pp;
74 	u_char *buf;
75 	uint32_t magic1, magic2;
76 	int error;
77 
78 	pp = cp->provider;
79 
80 	/*
81 	 * If the expected provider size for the filesystem matches the
82 	 * real provider size then don't ignore this filesystem.
83 	 */
84 	if (G_LABEL_UFS_CMP(pp, fs, fs_providersize))
85 		return (false);
86 
87 	/*
88 	 * If the filesystem size matches the real provider size then
89 	 * don't ignore this filesystem.
90 	 */
91 	if (fs->fs_magic == FS_UFS1_MAGIC ?
92 	    G_LABEL_UFS_CMP(pp, fs, fs_old_size) :
93 	    G_LABEL_UFS_CMP(pp, fs, fs_size))
94 		return (false);
95 
96 	/*
97 	 * Provider is bigger than expected; probe to see if there's a
98 	 * disklabel. Adapted from g_part_bsd_probe.
99 	 */
100 
101 	/* Check if the superblock overlaps where the disklabel lives. */
102 	if (fs->fs_sblockloc < pp->sectorsize * 2)
103 		return (false);
104 
105 	/* Sanity-check the provider. */
106 	if (pp->sectorsize < sizeof(struct disklabel) ||
107 	    pp->mediasize < BBSIZE)
108 		return (false);
109 	if (BBSIZE % pp->sectorsize)
110 		return (false);
111 
112 	/* Check that there's a disklabel. */
113 	buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
114 	if (buf == NULL)
115 		return (false);
116 	magic1 = le32dec(buf + 0);
117 	magic2 = le32dec(buf + 132);
118 	g_free(buf);
119 	if (magic1 == DISKMAGIC && magic2 == DISKMAGIC)
120 		return (true);
121 
122 	return (false);
123 }
124 
125 /*
126  * Try to find a superblock on the provider. If successful, look for a volume
127  * label and create an appropriate provider based on that.
128  */
129 static void
130 g_label_ufs_taste_common(struct g_consumer *cp, char *label, size_t size, int what)
131 {
132 	struct g_provider *pp;
133 	struct fs *fs;
134 
135 	g_topology_assert_not();
136 	pp = cp->provider;
137 	label[0] = '\0';
138 
139 	fs = NULL;
140 	KASSERT(pp->sectorsize != 0, ("Tasting a disk with 0 sectorsize"));
141 	if (SBLOCKSIZE % pp->sectorsize != 0 || ffs_sbget(cp, &fs, UFS_STDSB,
142 	    UFS_NOHASHFAIL | UFS_NOCSUM | UFS_NOMSG, M_GEOM, g_use_g_read_data)
143 	    != 0) {
144 		KASSERT(fs == NULL,
145 		    ("g_label_ufs_taste_common: non-NULL fs %p\n", fs));
146 		return;
147 	}
148 
149 	/* Check for magic. */
150 	if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_fsize > 0) {
151 		/* Valid UFS1. */
152 	} else if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_fsize > 0) {
153 		/* Valid UFS2. */
154 	} else {
155 		goto out;
156 	}
157 	/* Check if this should be ignored for compatibility. */
158 	if (g_label_ufs_ignore_bsdlabel_slice(cp, fs))
159 		goto out;
160 	G_LABEL_DEBUG(1, "%s file system detected on %s.",
161 	    fs->fs_magic == FS_UFS1_MAGIC ? "UFS1" : "UFS2", pp->name);
162 	switch (what) {
163 	case G_LABEL_UFS_VOLUME:
164 		/* Check for volume label */
165 		if (fs->fs_volname[0] != '\0')
166 			strlcpy(label, fs->fs_volname, size);
167 		break;
168 	case G_LABEL_UFS_ID:
169 		if (fs->fs_id[0] != 0 || fs->fs_id[1] != 0)
170 			snprintf(label, size, "%08x%08x", fs->fs_id[0],
171 			    fs->fs_id[1]);
172 		break;
173 	}
174 out:
175 	g_free(fs);
176 }
177 
178 static void
179 g_label_ufs_volume_taste(struct g_consumer *cp, char *label, size_t size)
180 {
181 
182 	g_label_ufs_taste_common(cp, label, size, G_LABEL_UFS_VOLUME);
183 }
184 
185 static void
186 g_label_ufs_id_taste(struct g_consumer *cp, char *label, size_t size)
187 {
188 
189 	g_label_ufs_taste_common(cp, label, size, G_LABEL_UFS_ID);
190 }
191 
192 struct g_label_desc g_label_ufs_volume = {
193 	.ld_taste = g_label_ufs_volume_taste,
194 	.ld_dirprefix = "ufs/",
195 	.ld_enabled = 1
196 };
197 
198 struct g_label_desc g_label_ufs_id = {
199 	.ld_taste = g_label_ufs_id_taste,
200 	.ld_dirprefix = "ufsid/",
201 	.ld_enabled = 1
202 };
203 
204 G_LABEL_INIT(ufsid, g_label_ufs_id, "Create device nodes for UFS file system IDs");
205 G_LABEL_INIT(ufs, g_label_ufs_volume, "Create device nodes for UFS volume names");
206 
207 MODULE_DEPEND(g_label, ufs, 1, 1, 1);
208