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/cdefs.h> 31 #include <sys/param.h> 32 #include <sys/mount.h> 33 #include <sys/disklabel.h> 34 #include <sys/stat.h> 35 36 #include <ufs/ufs/extattr.h> 37 #include <ufs/ufs/quota.h> 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 handle_disk_read(struct uufsd *, struct fs *, int); 51 52 /* 53 * Read the standard superblock. 54 * 55 * The following option flags can be or'ed into disk->d_lookupflags: 56 * 57 * UFS_NOMSG indicates that superblock inconsistency error messages 58 * should not be printed. 59 * 60 * UFS_NOCSUM causes only the superblock itself to be returned, but does 61 * not read in any auxillary data structures like the cylinder group 62 * summary information. 63 */ 64 int 65 sbread(struct uufsd *disk) 66 { 67 struct fs *fs; 68 int error; 69 70 error = sbget(disk->d_fd, &fs, disk->d_sblockloc, disk->d_lookupflags); 71 return (handle_disk_read(disk, fs, error)); 72 } 73 74 /* 75 * Make an extensive search to find a superblock. If the superblock 76 * in the standard place cannot be used, try looking for one of the 77 * backup superblocks. 78 * 79 * The flags parameter is made up of the following or'ed together options: 80 * 81 * UFS_NOMSG indicates that superblock inconsistency error messages 82 * should not be printed. 83 * 84 * UFS_NOCSUM causes only the superblock itself to be returned, but does 85 * not read in any auxillary data structures like the cylinder group 86 * summary information. 87 */ 88 int 89 sbfind(struct uufsd *disk, int flags) 90 { 91 struct fs *fs; 92 int error; 93 94 error = sbsearch(disk->d_fd, &fs, flags); 95 return (handle_disk_read(disk, fs, error)); 96 } 97 98 static int 99 handle_disk_read(struct uufsd *disk, struct fs *fs, int error) 100 { 101 102 ERROR(disk, NULL); 103 if (error != 0) { 104 switch (error) { 105 case EIO: 106 ERROR(disk, "non-existent or truncated superblock"); 107 break; 108 case ENOENT: 109 ERROR(disk, "no usable known superblock found"); 110 break; 111 case EINTEGRITY: 112 ERROR(disk, "superblock check-hash failure"); 113 break; 114 case ENOSPC: 115 ERROR(disk, "failed to allocate space for superblock " 116 "information"); 117 break; 118 case EINVAL: 119 ERROR(disk, "The previous newfs operation on this " 120 "volume did not complete.\nYou must complete " 121 "newfs before using this volume."); 122 break; 123 default: 124 ERROR(disk, "unknown superblock read error"); 125 errno = EIO; 126 break; 127 } 128 disk->d_ufs = 0; 129 return (-1); 130 } 131 memcpy(&disk->d_fs, fs, fs->fs_sbsize); 132 free(fs); 133 fs = &disk->d_fs; 134 if (fs->fs_magic == FS_UFS1_MAGIC) 135 disk->d_ufs = 1; 136 if (fs->fs_magic == FS_UFS2_MAGIC) 137 disk->d_ufs = 2; 138 disk->d_bsize = fs->fs_fsize / fsbtodb(fs, 1); 139 disk->d_sblock = fs->fs_sblockloc / disk->d_bsize; 140 disk->d_si = fs->fs_si; 141 return (0); 142 } 143 144 int 145 sbwrite(struct uufsd *disk, int all) 146 { 147 struct fs *fs; 148 int rv; 149 150 ERROR(disk, NULL); 151 152 rv = ufs_disk_write(disk); 153 if (rv == -1) { 154 ERROR(disk, "failed to open disk for writing"); 155 return (-1); 156 } 157 158 fs = &disk->d_fs; 159 if ((errno = sbput(disk->d_fd, fs, all ? fs->fs_ncg : 0)) != 0) { 160 switch (errno) { 161 case EIO: 162 ERROR(disk, "failed to write superblock"); 163 break; 164 default: 165 ERROR(disk, "unknown superblock write error"); 166 errno = EIO; 167 break; 168 } 169 return (-1); 170 } 171 return (0); 172 } 173 174 /* 175 * These are the low-level functions that actually read and write 176 * the superblock and its associated data. The actual work is done by 177 * the functions ffs_sbget and ffs_sbput in /sys/ufs/ffs/ffs_subr.c. 178 */ 179 static int use_pread(void *devfd, off_t loc, void **bufp, int size); 180 static int use_pwrite(void *devfd, off_t loc, void *buf, int size); 181 182 /* 183 * The following two functions read a superblock. Their flags 184 * parameter are made up of the following or'ed together options: 185 * 186 * UFS_NOMSG indicates that superblock inconsistency error messages 187 * should not be printed. 188 * 189 * UFS_NOCSUM causes only the superblock itself to be returned, but does 190 * not read in any auxillary data structures like the cylinder group 191 * summary information. 192 * 193 * Read a superblock from the devfd device allocating memory returned 194 * in fsp. 195 */ 196 int 197 sbget(int devfd, struct fs **fsp, off_t sblockloc, int flags) 198 { 199 int error; 200 201 error = ffs_sbget(&devfd, fsp, sblockloc, flags, "user", use_pread); 202 fflush(NULL); /* flush any messages */ 203 return (error); 204 } 205 206 /* 207 * Make an extensive search of the devfd device to find a superblock. 208 * If the superblock in the standard place cannot be used, try looking 209 * for one of the backup superblocks. If found, memory is allocated and 210 * returned in fsp. 211 */ 212 int 213 sbsearch(int devfd, struct fs **fsp, int flags) 214 { 215 int error; 216 217 error = ffs_sbsearch(&devfd, fsp, flags, "user", use_pread); 218 fflush(NULL); /* flush any messages */ 219 return (error); 220 } 221 222 /* 223 * A read function for use by user-level programs using libufs. 224 */ 225 static int 226 use_pread(void *devfd, off_t loc, void **bufp, int size) 227 { 228 int fd; 229 230 fd = *(int *)devfd; 231 if ((*bufp = malloc(size)) == 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