xref: /freebsd/lib/libufs/sblock.c (revision 18dd077da64274bdc038698f96ce25e022af0d55)
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 int
51 sbread(struct uufsd *disk)
52 {
53 	struct fs *fs;
54 
55 	ERROR(disk, NULL);
56 
57 	if ((errno = sbget(disk->d_fd, &fs, -1)) != 0) {
58 		switch (errno) {
59 		case EIO:
60 			ERROR(disk, "non-existent or truncated superblock");
61 			break;
62 		case ENOENT:
63 			ERROR(disk, "no usable known superblock found");
64 			break;
65 		case ENOSPC:
66 			ERROR(disk, "failed to allocate space for superblock "
67 			    "information");
68 			break;
69 		case EINVAL:
70 			ERROR(disk, "The previous newfs operation on this "
71 			    "volume did not complete.\nYou must complete "
72 			    "newfs before using this volume.");
73 			break;
74 		default:
75 			ERROR(disk, "unknown superblock read error");
76 			errno = EIO;
77 			break;
78 		}
79 		disk->d_ufs = 0;
80 		return (-1);
81 	}
82 	memcpy(&disk->d_fs, fs, fs->fs_sbsize);
83 	free(fs);
84 	fs = &disk->d_fs;
85 	if (fs->fs_magic == FS_UFS1_MAGIC)
86 		disk->d_ufs = 1;
87 	if (fs->fs_magic == FS_UFS2_MAGIC)
88 		disk->d_ufs = 2;
89 	disk->d_bsize = fs->fs_fsize / fsbtodb(fs, 1);
90 	disk->d_sblock = fs->fs_sblockloc / disk->d_bsize;
91 	disk->d_sbcsum = fs->fs_csp;
92 	return (0);
93 }
94 
95 int
96 sbwrite(struct uufsd *disk, int all)
97 {
98 	struct fs *fs;
99 
100 	ERROR(disk, NULL);
101 
102 	fs = &disk->d_fs;
103 	if ((errno = sbput(disk->d_fd, fs, all ? fs->fs_ncg : 0)) != 0) {
104 		switch (errno) {
105 		case EIO:
106 			ERROR(disk, "failed to write superblock");
107 			break;
108 		default:
109 			ERROR(disk, "unknown superblock write error");
110 			errno = EIO;
111 			break;
112 		}
113 		return (-1);
114 	}
115 	return (0);
116 }
117 
118 /*
119  * These are the low-level functions that actually read and write
120  * the superblock and its associated data. The actual work is done by
121  * the functions ffs_sbget and ffs_sbput in /sys/ufs/ffs/ffs_subr.c.
122  */
123 static int use_pread(void *devfd, off_t loc, void **bufp, int size);
124 static int use_pwrite(void *devfd, off_t loc, void *buf, int size);
125 
126 /*
127  * Read a superblock from the devfd device allocating memory returned
128  * in fsp. Also read the superblock summary information.
129  */
130 int
131 sbget(int devfd, struct fs **fsp, off_t sblockloc)
132 {
133 
134 	return (ffs_sbget(&devfd, fsp, sblockloc, "user", use_pread));
135 }
136 
137 /*
138  * A read function for use by user-level programs using libufs.
139  */
140 static int
141 use_pread(void *devfd, off_t loc, void **bufp, int size)
142 {
143 	int fd;
144 
145 	fd = *(int *)devfd;
146 	if ((*bufp = malloc(size)) == NULL)
147 		return (ENOSPC);
148 	if (pread(fd, *bufp, size, loc) != size)
149 		return (EIO);
150 	return (0);
151 }
152 
153 /*
154  * Write a superblock to the devfd device from the memory pointed to by fs.
155  * Also write out the superblock summary information but do not free the
156  * summary information memory.
157  *
158  * Additionally write out numaltwrite of the alternate superblocks. Use
159  * fs->fs_ncg to write out all of the alternate superblocks.
160  */
161 int
162 sbput(int devfd, struct fs *fs, int numaltwrite)
163 {
164 	struct csum *savedcsp;
165 	off_t savedactualloc;
166 	int i, error;
167 
168 	if ((error = ffs_sbput(&devfd, fs, fs->fs_sblockactualloc,
169 	     use_pwrite)) != 0)
170 		return (error);
171 	if (numaltwrite == 0)
172 		return (0);
173 	savedactualloc = fs->fs_sblockactualloc;
174 	savedcsp = fs->fs_csp;
175 	fs->fs_csp = NULL;
176 	for (i = 0; i < numaltwrite; i++) {
177 		fs->fs_sblockactualloc = dbtob(fsbtodb(fs, cgsblock(fs, i)));
178 		if ((error = ffs_sbput(&devfd, fs, fs->fs_sblockactualloc,
179 		     use_pwrite)) != 0) {
180 			fs->fs_sblockactualloc = savedactualloc;
181 			fs->fs_csp = savedcsp;
182 			return (-1);
183 		}
184 	}
185 	fs->fs_sblockactualloc = savedactualloc;
186 	fs->fs_csp = savedcsp;
187 	return (0);
188 }
189 
190 /*
191  * A write function for use by user-level programs using sbput in libufs.
192  */
193 static int
194 use_pwrite(void *devfd, off_t loc, void *buf, int size)
195 {
196 	int fd;
197 
198 	fd = *(int *)devfd;
199 	if (pwrite(fd, buf, size, loc) != size)
200 		return (EIO);
201 	return (0);
202 }
203