xref: /freebsd/lib/libufs/sblock.c (revision e1d31d0685f0b430f385023b7de49f47be6c7de0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
31 #include <sys/mount.h>
32 #include <sys/disklabel.h>
33 #include <sys/stat.h>
34 
35 #include <ufs/ufs/extattr.h>
36 #include <ufs/ufs/quota.h>
37 #include <ufs/ufs/ufsmount.h>
38 #include <ufs/ufs/dinode.h>
39 #include <ufs/ffs/fs.h>
40 
41 #include <errno.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 
47 #include <libufs.h>
48 
49 static int handle_disk_read(struct uufsd *, struct fs *, int);
50 
51 /*
52  * Read the standard superblock.
53  *
54  * The following option flags can be or'ed into disk->d_lookupflags:
55  *
56  * UFS_NOMSG indicates that superblock inconsistency error messages
57  *    should not be printed.
58  *
59  * UFS_NOCSUM causes only the superblock itself to be returned, but does
60  *    not read in any auxillary data structures like the cylinder group
61  *    summary information.
62  */
63 int
64 sbread(struct uufsd *disk)
65 {
66 	struct fs *fs;
67 	int error;
68 
69 	error = sbget(disk->d_fd, &fs, disk->d_sblockloc, disk->d_lookupflags);
70 	return (handle_disk_read(disk, fs, error));
71 }
72 
73 /*
74  * Make an extensive search to find a superblock. If the superblock
75  * in the standard place cannot be used, try looking for one of the
76  * backup superblocks.
77  *
78  * The flags parameter is made up of the following or'ed together options:
79  *
80  * UFS_NOMSG indicates that superblock inconsistency error messages
81  *    should not be printed.
82  *
83  * UFS_NOCSUM causes only the superblock itself to be returned, but does
84  *    not read in any auxillary data structures like the cylinder group
85  *    summary information.
86  */
87 int
88 sbfind(struct uufsd *disk, int flags)
89 {
90 	struct fs *fs;
91 	int error;
92 
93 	error = sbsearch(disk->d_fd, &fs, flags);
94 	return (handle_disk_read(disk, fs, error));
95 }
96 
97 static int
98 handle_disk_read(struct uufsd *disk, struct fs *fs, int error)
99 {
100 
101 	ERROR(disk, NULL);
102 	if (error != 0) {
103 		switch (error) {
104 		case EIO:
105 			ERROR(disk, "non-existent or truncated superblock");
106 			break;
107 		case ENOENT:
108 			ERROR(disk, "no usable known superblock found");
109 			break;
110 		case EINTEGRITY:
111 			ERROR(disk, "superblock check-hash failure");
112 			break;
113 		case ENOSPC:
114 			ERROR(disk, "failed to allocate space for superblock "
115 			    "information");
116 			break;
117 		case EINVAL:
118 			ERROR(disk, "The previous newfs operation on this "
119 			    "volume did not complete.\nYou must complete "
120 			    "newfs before using this volume.");
121 			break;
122 		default:
123 			ERROR(disk, "unknown superblock read error");
124 			errno = EIO;
125 			break;
126 		}
127 		disk->d_ufs = 0;
128 		return (-1);
129 	}
130 	memcpy(&disk->d_fs, fs, fs->fs_sbsize);
131 	free(fs);
132 	fs = &disk->d_fs;
133 	if (fs->fs_magic == FS_UFS1_MAGIC)
134 		disk->d_ufs = 1;
135 	if (fs->fs_magic == FS_UFS2_MAGIC)
136 		disk->d_ufs = 2;
137 	disk->d_bsize = fs->fs_fsize / fsbtodb(fs, 1);
138 	disk->d_sblock = fs->fs_sblockloc / disk->d_bsize;
139 	disk->d_si = fs->fs_si;
140 	return (0);
141 }
142 
143 int
144 sbwrite(struct uufsd *disk, int all)
145 {
146 	struct fs *fs;
147 	int rv;
148 
149 	ERROR(disk, NULL);
150 
151 	rv = ufs_disk_write(disk);
152 	if (rv == -1) {
153 		ERROR(disk, "failed to open disk for writing");
154 		return (-1);
155 	}
156 
157 	fs = &disk->d_fs;
158 	if ((errno = sbput(disk->d_fd, fs, all ? fs->fs_ncg : 0)) != 0) {
159 		switch (errno) {
160 		case EIO:
161 			ERROR(disk, "failed to write superblock");
162 			break;
163 		default:
164 			ERROR(disk, "unknown superblock write error");
165 			errno = EIO;
166 			break;
167 		}
168 		return (-1);
169 	}
170 	return (0);
171 }
172 
173 /*
174  * These are the low-level functions that actually read and write
175  * the superblock and its associated data. The actual work is done by
176  * the functions ffs_sbget and ffs_sbput in /sys/ufs/ffs/ffs_subr.c.
177  */
178 static int use_pread(void *devfd, off_t loc, void **bufp, int size);
179 static int use_pwrite(void *devfd, off_t loc, void *buf, int size);
180 
181 /*
182  * The following two functions read a superblock. Their flags
183  * parameter are made up of the following or'ed together options:
184  *
185  * UFS_NOMSG indicates that superblock inconsistency error messages
186  *    should not be printed.
187  *
188  * UFS_NOCSUM causes only the superblock itself to be returned, but does
189  *    not read in any auxillary data structures like the cylinder group
190  *    summary information.
191  *
192  * Read a superblock from the devfd device allocating memory returned
193  * in fsp.
194  */
195 int
196 sbget(int devfd, struct fs **fsp, off_t sblockloc, int flags)
197 {
198 	int error;
199 
200 	error = ffs_sbget(&devfd, fsp, sblockloc, flags, "user", use_pread);
201 	fflush(NULL); /* flush any messages */
202 	return (error);
203 }
204 
205 /*
206  * Make an extensive search of the devfd device to find a superblock.
207  * If the superblock in the standard place cannot be used, try looking
208  * for one of the backup superblocks. If found, memory is allocated and
209  * returned in fsp.
210  */
211 int
212 sbsearch(int devfd, struct fs **fsp, int flags)
213 {
214 	int error;
215 
216 	error = ffs_sbsearch(&devfd, fsp, flags, "user", use_pread);
217 	fflush(NULL); /* flush any messages */
218 	return (error);
219 }
220 
221 /*
222  * A read function for use by user-level programs using libufs.
223  */
224 static int
225 use_pread(void *devfd, off_t loc, void **bufp, int size)
226 {
227 	int fd;
228 
229 	fd = *(int *)devfd;
230 	BUF_MALLOC(bufp, NULL, size);
231 	if (*bufp == NULL)
232 		return (ENOSPC);
233 	if (pread(fd, *bufp, size, loc) != size)
234 		return (EIO);
235 	return (0);
236 }
237 
238 /*
239  * Write a superblock to the devfd device from the memory pointed to by fs.
240  * Also write out the superblock summary information but do not free the
241  * summary information memory.
242  *
243  * Additionally write out numaltwrite of the alternate superblocks. Use
244  * fs->fs_ncg to write out all of the alternate superblocks.
245  */
246 int
247 sbput(int devfd, struct fs *fs, int numaltwrite)
248 {
249 	struct csum *savedcsp;
250 	off_t savedactualloc;
251 	int i, error;
252 
253 	error = ffs_sbput(&devfd, fs, fs->fs_sblockactualloc, use_pwrite);
254 	fflush(NULL); /* flush any messages */
255 	if (error != 0 || numaltwrite == 0)
256 		return (error);
257 	savedactualloc = fs->fs_sblockactualloc;
258 	if (fs->fs_si != NULL) {
259 		savedcsp = fs->fs_csp;
260 		fs->fs_csp = NULL;
261 	}
262 	for (i = 0; i < numaltwrite; i++) {
263 		fs->fs_sblockactualloc = dbtob(fsbtodb(fs, cgsblock(fs, i)));
264 		if ((error = ffs_sbput(&devfd, fs, fs->fs_sblockactualloc,
265 		     use_pwrite)) != 0) {
266 			fflush(NULL); /* flush any messages */
267 			fs->fs_sblockactualloc = savedactualloc;
268 			fs->fs_csp = savedcsp;
269 			return (error);
270 		}
271 	}
272 	fs->fs_sblockactualloc = savedactualloc;
273 	if (fs->fs_si != NULL)
274 		fs->fs_csp = savedcsp;
275 	fflush(NULL); /* flush any messages */
276 	return (0);
277 }
278 
279 /*
280  * A write function for use by user-level programs using sbput in libufs.
281  */
282 static int
283 use_pwrite(void *devfd, off_t loc, void *buf, int size)
284 {
285 	int fd;
286 
287 	fd = *(int *)devfd;
288 	if (pwrite(fd, buf, size, loc) != size)
289 		return (EIO);
290 	return (0);
291 }
292