xref: /freebsd/sys/geom/label/g_label_ufs.c (revision 97cb52fa9aefd90fad38790fded50905aeeb9b9e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 
38 #include <ufs/ufs/dinode.h>
39 #include <ufs/ffs/fs.h>
40 
41 #include <geom/geom.h>
42 #include <geom/label/g_label.h>
43 
44 #define G_LABEL_UFS_VOLUME_DIR	"ufs"
45 #define G_LABEL_UFS_ID_DIR	"ufsid"
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 static const int superblocks[] = SBLOCKSEARCH;
60 
61 static void
62 g_label_ufs_taste_common(struct g_consumer *cp, char *label, size_t size, int what)
63 {
64 	struct g_provider *pp;
65 	int sb, superblock;
66 	struct fs *fs;
67 
68 	g_topology_assert_not();
69 	pp = cp->provider;
70 	label[0] = '\0';
71 
72 	if (SBLOCKSIZE % cp->provider->sectorsize != 0)
73 		return;
74 
75 	/*
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.
81 	 */
82 	for (sb = 0; (superblock = superblocks[sb]) != -1; sb++) {
83 		/*
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
87 		 * anyway.
88 		 */
89 		if (superblock % cp->provider->sectorsize != 0)
90 			continue;
91 
92 		fs = (struct fs *)g_read_data(cp, superblock, SBLOCKSIZE, NULL);
93 		if (fs == NULL)
94 			continue;
95 		/*
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.
101 		 *
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).
105 		 */
106 		switch (fs->fs_magic){
107 		case FS_UFS1_MAGIC:
108 		case FS_UFS2_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);
113 			break;
114 		default:
115 			break;
116 		}
117 
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))) {
121 		    	/* Valid UFS1. */
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))) {
125 		    	/* Valid UFS2. */
126 		} else {
127 			g_free(fs);
128 			continue;
129 		}
130 		if (fs->fs_sblockloc != superblock || fs->fs_ncg < 1 ||
131 		    fs->fs_bsize < MINBSIZE ||
132 		    fs->fs_bsize < sizeof(struct fs)) {
133 			g_free(fs);
134 			continue;
135 		}
136 		G_LABEL_DEBUG(1, "%s file system detected on %s.",
137 		    fs->fs_magic == FS_UFS1_MAGIC ? "UFS1" : "UFS2", pp->name);
138 		switch (what) {
139 		case G_LABEL_UFS_VOLUME:
140 			/* Check for volume label */
141 			if (fs->fs_volname[0] == '\0') {
142 				g_free(fs);
143 				continue;
144 			}
145 			strlcpy(label, fs->fs_volname, size);
146 			break;
147 		case G_LABEL_UFS_ID:
148 			if (fs->fs_id[0] == 0 && fs->fs_id[1] == 0) {
149 				g_free(fs);
150 				continue;
151 			}
152 			snprintf(label, size, "%08x%08x", fs->fs_id[0],
153 			    fs->fs_id[1]);
154 			break;
155 		}
156 		g_free(fs);
157 		break;
158 	}
159 }
160 
161 static void
162 g_label_ufs_volume_taste(struct g_consumer *cp, char *label, size_t size)
163 {
164 
165 	g_label_ufs_taste_common(cp, label, size, G_LABEL_UFS_VOLUME);
166 }
167 
168 static void
169 g_label_ufs_id_taste(struct g_consumer *cp, char *label, size_t size)
170 {
171 
172 	g_label_ufs_taste_common(cp, label, size, G_LABEL_UFS_ID);
173 }
174 
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,
178 	.ld_enabled = 1
179 };
180 
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,
184 	.ld_enabled = 1
185 };
186 
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");
189