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