2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002 Juli Mallett. All rights reserved.
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
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.
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.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/mount.h>
35 #include <sys/disklabel.h>
38 #include <ufs/ufs/ufsmount.h>
39 #include <ufs/ufs/dinode.h>
40 #include <ufs/ffs/fs.h>
50 static int superblocks[] = SBLOCKSEARCH;
53 sbread(struct uufsd *disk)
55 uint8_t block[MAXBSIZE];
64 superblock = superblocks[0];
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");
71 if (fs->fs_magic == FS_UFS1_MAGIC)
73 if (fs->fs_magic == FS_UFS2_MAGIC &&
74 fs->fs_sblockloc == superblock)
76 if (fs->fs_bsize <= MAXBSIZE &&
77 (size_t)fs->fs_bsize >= sizeof(*fs)) {
83 if (superblock == -1 || disk->d_ufs == 0) {
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.
89 ERROR(disk, "no usable known superblock found");
93 disk->d_bsize = fs->fs_fsize / fsbtodb(fs, 1);
94 disk->d_sblock = superblock / disk->d_bsize;
96 * Read in the superblock summary information.
99 blks = howmany(size, fs->fs_fsize);
100 size += fs->fs_ncg * sizeof(int32_t);
101 space = malloc(size);
103 ERROR(disk, "failed to allocate space for summary information");
106 fs->fs_csp = (struct csum *)space;
107 for (i = 0; i < blks; i += fs->fs_frag) {
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)
113 ERROR(disk, "Failed to read sb summary information");
117 bcopy(block, space, size);
120 fs->fs_maxcluster = (uint32_t *)space;
121 disk->d_sbcsum = fs->fs_csp;
127 sbwrite(struct uufsd *disk, int all)
138 if (!disk->d_sblock) {
139 disk->d_sblock = disk->d_fs.fs_sblockloc / disk->d_bsize;
142 if (bwrite(disk, disk->d_sblock, fs, SBLOCKSIZE) == -1) {
143 ERROR(disk, "failed to write superblock");
147 * Write superblock summary information.
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) {
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)
157 ERROR(disk, "Failed to write sb summary information");
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");