xref: /freebsd/lib/libufs/sblock.c (revision d06955f9bdb1416d9196043ed781f9b36dae9adc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002 Juli Mallett.  All rights reserved.
5  *
6  * This software was written by Juli Mallett <jmallett@FreeBSD.org> for the
7  * FreeBSD project.  Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  * 1. Redistribution of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  * 2. Redistribution 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/mount.h>
35 #include <sys/disklabel.h>
36 #include <sys/stat.h>
37 
38 #include <ufs/ufs/ufsmount.h>
39 #include <ufs/ufs/dinode.h>
40 #include <ufs/ffs/fs.h>
41 
42 #include <errno.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 
48 #include <libufs.h>
49 
50 static int superblocks[] = SBLOCKSEARCH;
51 
52 int
53 sbread(struct uufsd *disk)
54 {
55 	uint8_t block[MAXBSIZE];
56 	struct fs *fs;
57 	int sb, superblock;
58 	int i, size, blks;
59 	uint8_t *space;
60 
61 	ERROR(disk, NULL);
62 
63 	fs = &disk->d_fs;
64 	superblock = superblocks[0];
65 
66 	for (sb = 0; (superblock = superblocks[sb]) != -1; sb++) {
67 		if (bread(disk, superblock, disk->d_sb, SBLOCKSIZE) == -1) {
68 			ERROR(disk, "non-existent or truncated superblock");
69 			return (-1);
70 		}
71 		if (fs->fs_magic == FS_UFS1_MAGIC)
72 			disk->d_ufs = 1;
73 		if (fs->fs_magic == FS_UFS2_MAGIC &&
74 		    fs->fs_sblockloc == superblock)
75 			disk->d_ufs = 2;
76 		if (fs->fs_bsize <= MAXBSIZE &&
77 		    (size_t)fs->fs_bsize >= sizeof(*fs)) {
78 			if (disk->d_ufs)
79 				break;
80 		}
81 		disk->d_ufs = 0;
82 	}
83 	if (superblock == -1 || disk->d_ufs == 0) {
84 		/*
85 		 * Other error cases will result in errno being set, here we
86 		 * must set it to indicate no superblock could be found with
87 		 * which to associate this disk/filesystem.
88 		 */
89 		ERROR(disk, "no usable known superblock found");
90 		errno = ENOENT;
91 		return (-1);
92 	}
93 	disk->d_bsize = fs->fs_fsize / fsbtodb(fs, 1);
94 	disk->d_sblock = superblock / disk->d_bsize;
95 	/*
96 	 * Read in the superblock summary information.
97 	 */
98 	size = fs->fs_cssize;
99 	blks = howmany(size, fs->fs_fsize);
100 	size += fs->fs_ncg * sizeof(int32_t);
101 	space = malloc(size);
102 	if (space == NULL) {
103 		ERROR(disk, "failed to allocate space for summary information");
104 		return (-1);
105 	}
106 	fs->fs_csp = (struct csum *)space;
107 	for (i = 0; i < blks; i += fs->fs_frag) {
108 		size = fs->fs_bsize;
109 		if (i + fs->fs_frag > blks)
110 			size = (blks - i) * fs->fs_fsize;
111 		if (bread(disk, fsbtodb(fs, fs->fs_csaddr + i), block, size)
112 		    == -1) {
113 			ERROR(disk, "Failed to read sb summary information");
114 			free(fs->fs_csp);
115 			return (-1);
116 		}
117 		bcopy(block, space, size);
118 		space += size;
119 	}
120 	fs->fs_maxcluster = (uint32_t *)space;
121 	disk->d_sbcsum = fs->fs_csp;
122 
123 	return (0);
124 }
125 
126 int
127 sbwrite(struct uufsd *disk, int all)
128 {
129 	struct fs *fs;
130 	int blks, size;
131 	uint8_t *space;
132 	unsigned i;
133 
134 	ERROR(disk, NULL);
135 
136 	fs = &disk->d_fs;
137 
138 	if (!disk->d_sblock) {
139 		disk->d_sblock = disk->d_fs.fs_sblockloc / disk->d_bsize;
140 	}
141 
142 	if (bwrite(disk, disk->d_sblock, fs, SBLOCKSIZE) == -1) {
143 		ERROR(disk, "failed to write superblock");
144 		return (-1);
145 	}
146 	/*
147 	 * Write superblock summary information.
148 	 */
149 	blks = howmany(fs->fs_cssize, fs->fs_fsize);
150 	space = (uint8_t *)disk->d_sbcsum;
151 	for (i = 0; i < blks; i += fs->fs_frag) {
152 		size = fs->fs_bsize;
153 		if (i + fs->fs_frag > blks)
154 			size = (blks - i) * fs->fs_fsize;
155 		if (bwrite(disk, fsbtodb(fs, fs->fs_csaddr + i), space, size)
156 		    == -1) {
157 			ERROR(disk, "Failed to write sb summary information");
158 			return (-1);
159 		}
160 		space += size;
161 	}
162 	if (all) {
163 		for (i = 0; i < fs->fs_ncg; i++)
164 			if (bwrite(disk, fsbtodb(fs, cgsblock(fs, i)),
165 			    fs, SBLOCKSIZE) == -1) {
166 				ERROR(disk, "failed to update a superblock");
167 				return (-1);
168 			}
169 	}
170 	return (0);
171 }
172