xref: /freebsd/usr.sbin/fstyp/ufs.c (revision be3a49ee62b6af938565ef159b535aea59a92f16)
1*be3a49eeSEdward Tomasz Napierala /*-
2*be3a49eeSEdward Tomasz Napierala  * Copyright (c) 2002, 2003 Gordon Tetlow
3*be3a49eeSEdward Tomasz Napierala  * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4*be3a49eeSEdward Tomasz Napierala  * Copyright (c) 2014 The FreeBSD Foundation
5*be3a49eeSEdward Tomasz Napierala  * All rights reserved.
6*be3a49eeSEdward Tomasz Napierala  *
7*be3a49eeSEdward Tomasz Napierala  * This software was developed by Edward Tomasz Napierala under sponsorship
8*be3a49eeSEdward Tomasz Napierala  * from the FreeBSD Foundation.
9*be3a49eeSEdward Tomasz Napierala  *
10*be3a49eeSEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
11*be3a49eeSEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
12*be3a49eeSEdward Tomasz Napierala  * are met:
13*be3a49eeSEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
14*be3a49eeSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
15*be3a49eeSEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
16*be3a49eeSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
17*be3a49eeSEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
18*be3a49eeSEdward Tomasz Napierala  *
19*be3a49eeSEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
20*be3a49eeSEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*be3a49eeSEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*be3a49eeSEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
23*be3a49eeSEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*be3a49eeSEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*be3a49eeSEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*be3a49eeSEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*be3a49eeSEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*be3a49eeSEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*be3a49eeSEdward Tomasz Napierala  * SUCH DAMAGE.
30*be3a49eeSEdward Tomasz Napierala  */
31*be3a49eeSEdward Tomasz Napierala 
32*be3a49eeSEdward Tomasz Napierala #include <sys/cdefs.h>
33*be3a49eeSEdward Tomasz Napierala __FBSDID("$FreeBSD$");
34*be3a49eeSEdward Tomasz Napierala 
35*be3a49eeSEdward Tomasz Napierala #include <sys/types.h>
36*be3a49eeSEdward Tomasz Napierala #include <stdint.h>
37*be3a49eeSEdward Tomasz Napierala #include <stdio.h>
38*be3a49eeSEdward Tomasz Napierala #include <stdlib.h>
39*be3a49eeSEdward Tomasz Napierala #include <string.h>
40*be3a49eeSEdward Tomasz Napierala 
41*be3a49eeSEdward Tomasz Napierala #include <ufs/ufs/dinode.h>
42*be3a49eeSEdward Tomasz Napierala #include <ufs/ffs/fs.h>
43*be3a49eeSEdward Tomasz Napierala 
44*be3a49eeSEdward Tomasz Napierala #include "fstyp.h"
45*be3a49eeSEdward Tomasz Napierala 
46*be3a49eeSEdward Tomasz Napierala static const int superblocks[] = SBLOCKSEARCH;
47*be3a49eeSEdward Tomasz Napierala 
48*be3a49eeSEdward Tomasz Napierala int
49*be3a49eeSEdward Tomasz Napierala fstyp_ufs(FILE *fp, char *label, size_t labelsize)
50*be3a49eeSEdward Tomasz Napierala {
51*be3a49eeSEdward Tomasz Napierala 	int sb, superblock;
52*be3a49eeSEdward Tomasz Napierala 	struct fs *fs;
53*be3a49eeSEdward Tomasz Napierala 
54*be3a49eeSEdward Tomasz Napierala 	/*
55*be3a49eeSEdward Tomasz Napierala 	 * Walk through the standard places that superblocks hide and look
56*be3a49eeSEdward Tomasz Napierala 	 * for UFS magic. If we find magic, then check that the size in the
57*be3a49eeSEdward Tomasz Napierala 	 * superblock corresponds to the size of the underlying provider.
58*be3a49eeSEdward Tomasz Napierala 	 * Finally, look for a volume label and create an appropriate
59*be3a49eeSEdward Tomasz Napierala 	 * provider based on that.
60*be3a49eeSEdward Tomasz Napierala 	 */
61*be3a49eeSEdward Tomasz Napierala 	for (sb = 0; (superblock = superblocks[sb]) != -1; sb++) {
62*be3a49eeSEdward Tomasz Napierala 		fs = (struct fs *)read_buf(fp, superblock, SBLOCKSIZE);
63*be3a49eeSEdward Tomasz Napierala 		if (fs == NULL)
64*be3a49eeSEdward Tomasz Napierala 			continue;
65*be3a49eeSEdward Tomasz Napierala 		/*
66*be3a49eeSEdward Tomasz Napierala 		 * Check for magic. We also need to check if file system size is equal
67*be3a49eeSEdward Tomasz Napierala 		 * to providers size, because sysinstall(8) used to bogusly put first
68*be3a49eeSEdward Tomasz Napierala 		 * partition at offset 0 instead of 16, and glabel/ufs would find file
69*be3a49eeSEdward Tomasz Napierala 		 * system on slice instead of partition.
70*be3a49eeSEdward Tomasz Napierala 		 */
71*be3a49eeSEdward Tomasz Napierala #ifdef notyet
72*be3a49eeSEdward Tomasz Napierala 		if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_fsize > 0 &&
73*be3a49eeSEdward Tomasz Napierala 		    ((pp->mediasize / fs->fs_fsize == fs->fs_old_size) ||
74*be3a49eeSEdward Tomasz Napierala 		    (pp->mediasize / fs->fs_fsize == fs->fs_providersize))) {
75*be3a49eeSEdward Tomasz Napierala 		    	/* Valid UFS1. */
76*be3a49eeSEdward Tomasz Napierala 		} else if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_fsize > 0 &&
77*be3a49eeSEdward Tomasz Napierala 		    ((pp->mediasize / fs->fs_fsize == fs->fs_size) ||
78*be3a49eeSEdward Tomasz Napierala 		    (pp->mediasize / fs->fs_fsize == fs->fs_providersize))) {
79*be3a49eeSEdward Tomasz Napierala 		    	/* Valid UFS2. */
80*be3a49eeSEdward Tomasz Napierala 		} else {
81*be3a49eeSEdward Tomasz Napierala 			g_free(fs);
82*be3a49eeSEdward Tomasz Napierala 			continue;
83*be3a49eeSEdward Tomasz Napierala 		}
84*be3a49eeSEdward Tomasz Napierala #else
85*be3a49eeSEdward Tomasz Napierala 		if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_fsize > 0) {
86*be3a49eeSEdward Tomasz Napierala 		    	/* Valid UFS1. */
87*be3a49eeSEdward Tomasz Napierala 		} else if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_fsize > 0) {
88*be3a49eeSEdward Tomasz Napierala 		    	/* Valid UFS2. */
89*be3a49eeSEdward Tomasz Napierala 		} else {
90*be3a49eeSEdward Tomasz Napierala 			free(fs);
91*be3a49eeSEdward Tomasz Napierala 			continue;
92*be3a49eeSEdward Tomasz Napierala 		}
93*be3a49eeSEdward Tomasz Napierala #endif
94*be3a49eeSEdward Tomasz Napierala 
95*be3a49eeSEdward Tomasz Napierala 		if (fs->fs_sblockloc != superblock || fs->fs_ncg < 1 ||
96*be3a49eeSEdward Tomasz Napierala 		    fs->fs_bsize < MINBSIZE ||
97*be3a49eeSEdward Tomasz Napierala 		    (size_t)fs->fs_bsize < sizeof(struct fs)) {
98*be3a49eeSEdward Tomasz Napierala 			free(fs);
99*be3a49eeSEdward Tomasz Napierala 			continue;
100*be3a49eeSEdward Tomasz Napierala 		}
101*be3a49eeSEdward Tomasz Napierala 
102*be3a49eeSEdward Tomasz Napierala 		strlcpy(label, fs->fs_volname, labelsize);
103*be3a49eeSEdward Tomasz Napierala 
104*be3a49eeSEdward Tomasz Napierala 		free(fs);
105*be3a49eeSEdward Tomasz Napierala 		return (0);
106*be3a49eeSEdward Tomasz Napierala 	}
107*be3a49eeSEdward Tomasz Napierala 
108*be3a49eeSEdward Tomasz Napierala 	return (1);
109*be3a49eeSEdward Tomasz Napierala }
110