1 1.24 chs /* $NetBSD: ffs_inode.c,v 1.24 2022/11/17 06:40:38 chs Exp $ */ 2 1.1 perseant 3 1.1 perseant /*- 4 1.1 perseant * Copyright (c) 1980, 1991, 1993, 1994 5 1.9 lukem * The Regents of the University of California. All rights reserved. 6 1.1 perseant * 7 1.1 perseant * Redistribution and use in source and binary forms, with or without 8 1.1 perseant * modification, are permitted provided that the following conditions 9 1.1 perseant * are met: 10 1.1 perseant * 1. Redistributions of source code must retain the above copyright 11 1.1 perseant * notice, this list of conditions and the following disclaimer. 12 1.1 perseant * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 perseant * notice, this list of conditions and the following disclaimer in the 14 1.1 perseant * documentation and/or other materials provided with the distribution. 15 1.13 agc * 3. Neither the name of the University nor the names of its contributors 16 1.1 perseant * may be used to endorse or promote products derived from this software 17 1.1 perseant * without specific prior written permission. 18 1.1 perseant * 19 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 1.1 perseant * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 1.1 perseant * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 1.1 perseant * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 1.1 perseant * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 1.1 perseant * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 1.1 perseant * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 1.1 perseant * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 1.1 perseant * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 1.1 perseant * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 1.1 perseant * SUCH DAMAGE. 30 1.1 perseant */ 31 1.1 perseant 32 1.1 perseant #include <sys/cdefs.h> 33 1.1 perseant #ifndef lint 34 1.18 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\ 35 1.18 lukem The Regents of the University of California. All rights reserved."); 36 1.1 perseant #endif /* not lint */ 37 1.1 perseant 38 1.1 perseant #ifndef lint 39 1.24 chs __RCSID("$NetBSD: ffs_inode.c,v 1.24 2022/11/17 06:40:38 chs Exp $"); 40 1.1 perseant #endif /* not lint */ 41 1.1 perseant 42 1.1 perseant #include <sys/param.h> 43 1.1 perseant #include <sys/time.h> 44 1.1 perseant #include <sys/stat.h> 45 1.1 perseant #include <ufs/ufs/dir.h> 46 1.1 perseant #include <ufs/ufs/dinode.h> 47 1.1 perseant #include <ufs/ffs/fs.h> 48 1.1 perseant #include <ufs/ffs/ffs_extern.h> 49 1.12 fvdl #include <ufs/ufs/ufs_bswap.h> 50 1.1 perseant 51 1.1 perseant #include <protocols/dumprestore.h> 52 1.1 perseant 53 1.1 perseant #include <ctype.h> 54 1.1 perseant #include <errno.h> 55 1.1 perseant #include <fts.h> 56 1.1 perseant #include <stdio.h> 57 1.12 fvdl #include <stdlib.h> 58 1.1 perseant #include <string.h> 59 1.1 perseant #include <unistd.h> 60 1.1 perseant 61 1.1 perseant #include "dump.h" 62 1.1 perseant 63 1.19 dholland static struct fs *sblock; 64 1.12 fvdl 65 1.12 fvdl static off_t sblock_try[] = SBLOCKSEARCH; 66 1.1 perseant 67 1.12 fvdl int is_ufs2; 68 1.1 perseant 69 1.1 perseant /* 70 1.1 perseant * Read the superblock from disk, and check its magic number. 71 1.6 lukem * Determine whether byte-swapping needs to be done on this file system. 72 1.1 perseant */ 73 1.1 perseant int 74 1.8 lukem fs_read_sblock(char *superblock) 75 1.1 perseant { 76 1.12 fvdl int i; 77 1.8 lukem int ns = 0; 78 1.1 perseant 79 1.8 lukem sblock = (struct fs *)superblock; 80 1.14 dsl for (i = 0; ; i++) { 81 1.14 dsl if (sblock_try[i] == -1) 82 1.23 christos quit("can't find superblock"); 83 1.12 fvdl rawread(sblock_try[i], (char *)superblock, MAXBSIZE); 84 1.12 fvdl 85 1.12 fvdl switch(sblock->fs_magic) { 86 1.24 chs case FS_UFS2EA_MAGIC: 87 1.24 chs sblock->fs_magic = FS_UFS2_MAGIC; 88 1.24 chs /*FALLTHROUGH*/ 89 1.12 fvdl case FS_UFS2_MAGIC: 90 1.12 fvdl is_ufs2 = 1; 91 1.12 fvdl /*FALLTHROUGH*/ 92 1.12 fvdl case FS_UFS1_MAGIC: 93 1.14 dsl break; 94 1.24 chs case FS_UFS2EA_MAGIC_SWAPPED: 95 1.24 chs sblock->fs_magic = FS_UFS2_MAGIC_SWAPPED; 96 1.24 chs /*FALLTHROUGH*/ 97 1.12 fvdl case FS_UFS2_MAGIC_SWAPPED: 98 1.12 fvdl is_ufs2 = 1; 99 1.12 fvdl /*FALLTHROUGH*/ 100 1.12 fvdl case FS_UFS1_MAGIC_SWAPPED: 101 1.12 fvdl ns = 1; 102 1.7 lukem ffs_sb_swap(sblock, sblock); 103 1.14 dsl break; 104 1.12 fvdl default: 105 1.12 fvdl continue; 106 1.12 fvdl } 107 1.14 dsl if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2) 108 1.14 dsl continue; 109 1.16 dsl if ((is_ufs2 || sblock->fs_old_flags & FS_FLAGS_UPDATED) 110 1.14 dsl && sblock_try[i] != sblock->fs_sblockloc) 111 1.14 dsl continue; 112 1.14 dsl break; 113 1.12 fvdl } 114 1.15 ws return ns; 115 1.1 perseant } 116 1.1 perseant 117 1.1 perseant /* 118 1.1 perseant * Fill in the ufsi struct, as well as the maxino and dev_bsize global 119 1.1 perseant * variables. 120 1.1 perseant */ 121 1.1 perseant struct ufsi * 122 1.1 perseant fs_parametrize(void) 123 1.1 perseant { 124 1.1 perseant static struct ufsi ufsi; 125 1.1 perseant 126 1.1 perseant #ifdef FS_44INODEFMT 127 1.12 fvdl if (is_ufs2 || sblock->fs_old_inodefmt >= FS_44INODEFMT) { 128 1.1 perseant spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWINODEFMT); 129 1.1 perseant } else { 130 1.1 perseant /* 131 1.6 lukem * Determine parameters for older file systems. From 132 1.1 perseant * /sys/ufs/ffs/ffs_vfsops.c::ffs_oldfscompat() 133 1.1 perseant * 134 1.1 perseant * XXX: not sure if other variables (fs_npsect, fs_interleave, 135 1.1 perseant * fs_nrpos, fs_maxfilesize) need to be fudged too. 136 1.1 perseant */ 137 1.1 perseant sblock->fs_qbmask = ~sblock->fs_bmask; 138 1.1 perseant sblock->fs_qfmask = ~sblock->fs_fmask; 139 1.1 perseant } 140 1.1 perseant #endif 141 1.1 perseant 142 1.1 perseant /* Fill out ufsi struct */ 143 1.22 dholland ufsi.ufs_dsize = FFS_FSBTODB(sblock,sblock->fs_size); 144 1.1 perseant ufsi.ufs_bsize = sblock->fs_bsize; 145 1.1 perseant ufsi.ufs_bshift = sblock->fs_bshift; 146 1.1 perseant ufsi.ufs_fsize = sblock->fs_fsize; 147 1.1 perseant ufsi.ufs_frag = sblock->fs_frag; 148 1.1 perseant ufsi.ufs_fsatoda = sblock->fs_fsbtodb; 149 1.1 perseant ufsi.ufs_nindir = sblock->fs_nindir; 150 1.1 perseant ufsi.ufs_inopb = sblock->fs_inopb; 151 1.1 perseant ufsi.ufs_maxsymlinklen = sblock->fs_maxsymlinklen; 152 1.1 perseant ufsi.ufs_bmask = sblock->fs_bmask; 153 1.1 perseant ufsi.ufs_fmask = sblock->fs_fmask; 154 1.1 perseant ufsi.ufs_qbmask = sblock->fs_qbmask; 155 1.1 perseant ufsi.ufs_qfmask = sblock->fs_qfmask; 156 1.1 perseant 157 1.22 dholland dev_bsize = sblock->fs_fsize / FFS_FSBTODB(sblock, 1); 158 1.1 perseant 159 1.1 perseant return &ufsi; 160 1.3 perseant } 161 1.3 perseant 162 1.3 perseant ino_t 163 1.3 perseant fs_maxino(void) 164 1.3 perseant { 165 1.4 lukem 166 1.3 perseant return sblock->fs_ipg * sblock->fs_ncg; 167 1.1 perseant } 168 1.1 perseant 169 1.12 fvdl void 170 1.17 christos fs_mapinodes(ino_t maxino __unused, u_int64_t *tape_size, int *anydirskipped) 171 1.12 fvdl { 172 1.12 fvdl int i, cg, inosused; 173 1.12 fvdl struct cg *cgp; 174 1.12 fvdl ino_t ino; 175 1.12 fvdl char *cp; 176 1.12 fvdl 177 1.12 fvdl if ((cgp = malloc(sblock->fs_cgsize)) == NULL) 178 1.23 christos quite(errno, "fs_mapinodes: cannot allocate memory."); 179 1.12 fvdl 180 1.12 fvdl for (cg = 0; cg < sblock->fs_ncg; cg++) { 181 1.12 fvdl ino = cg * sblock->fs_ipg; 182 1.22 dholland bread(FFS_FSBTODB(sblock, cgtod(sblock, cg)), (char *)cgp, 183 1.12 fvdl sblock->fs_cgsize); 184 1.12 fvdl if (needswap) 185 1.12 fvdl ffs_cg_swap(cgp, cgp, sblock); 186 1.12 fvdl if (sblock->fs_magic == FS_UFS2_MAGIC) 187 1.12 fvdl inosused = cgp->cg_initediblk; 188 1.12 fvdl else 189 1.12 fvdl inosused = sblock->fs_ipg; 190 1.12 fvdl /* 191 1.12 fvdl * If we are using soft updates, then we can trust the 192 1.12 fvdl * cylinder group inode allocation maps to tell us which 193 1.12 fvdl * inodes are allocated. We will scan the used inode map 194 1.12 fvdl * to find the inodes that are really in use, and then 195 1.12 fvdl * read only those inodes in from disk. 196 1.12 fvdl */ 197 1.12 fvdl if (sblock->fs_flags & FS_DOSOFTDEP) { 198 1.12 fvdl if (!cg_chkmagic(cgp, 0)) 199 1.23 christos quit("%s: cg %d: bad magic number\n", 200 1.23 christos __func__, cg); 201 1.12 fvdl cp = &cg_inosused(cgp, 0)[(inosused - 1) / CHAR_BIT]; 202 1.12 fvdl for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) { 203 1.12 fvdl if (*cp == 0) 204 1.12 fvdl continue; 205 1.12 fvdl for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) { 206 1.12 fvdl if (*cp & i) 207 1.12 fvdl break; 208 1.12 fvdl inosused--; 209 1.12 fvdl } 210 1.12 fvdl break; 211 1.12 fvdl } 212 1.12 fvdl if (inosused <= 0) 213 1.12 fvdl continue; 214 1.12 fvdl } 215 1.12 fvdl for (i = 0; i < inosused; i++, ino++) { 216 1.20 dholland if (ino < UFS_ROOTINO) 217 1.12 fvdl continue; 218 1.12 fvdl mapfileino(ino, tape_size, anydirskipped); 219 1.12 fvdl } 220 1.12 fvdl } 221 1.12 fvdl 222 1.12 fvdl free(cgp); 223 1.12 fvdl } 224 1.12 fvdl 225 1.12 fvdl union dinode * 226 1.4 lukem getino(ino_t inum) 227 1.1 perseant { 228 1.11 fvdl static ino_t minino, maxino; 229 1.12 fvdl static caddr_t inoblock; 230 1.12 fvdl int ntoswap, i; 231 1.12 fvdl struct ufs1_dinode *dp1; 232 1.12 fvdl struct ufs2_dinode *dp2; 233 1.1 perseant 234 1.12 fvdl if (inoblock == NULL && (inoblock = malloc(ufsib->ufs_bsize)) == NULL) 235 1.23 christos quite(errno, "cannot allocate inode memory."); 236 1.1 perseant curino = inum; 237 1.1 perseant if (inum >= minino && inum < maxino) 238 1.12 fvdl goto gotit; 239 1.1 perseant bread(fsatoda(ufsib, ino_to_fsba(sblock, inum)), (char *)inoblock, 240 1.1 perseant (int)ufsib->ufs_bsize); 241 1.21 dholland minino = inum - (inum % FFS_INOPB(sblock)); 242 1.21 dholland maxino = minino + FFS_INOPB(sblock); 243 1.12 fvdl if (needswap) { 244 1.12 fvdl if (is_ufs2) { 245 1.12 fvdl dp2 = (struct ufs2_dinode *)inoblock; 246 1.12 fvdl ntoswap = ufsib->ufs_bsize / sizeof(struct ufs2_dinode); 247 1.12 fvdl for (i = 0; i < ntoswap; i++) 248 1.12 fvdl ffs_dinode2_swap(&dp2[i], &dp2[i]); 249 1.12 fvdl } else { 250 1.12 fvdl dp1 = (struct ufs1_dinode *)inoblock; 251 1.12 fvdl ntoswap = ufsib->ufs_bsize / sizeof(struct ufs1_dinode); 252 1.12 fvdl for (i = 0; i < ntoswap; i++) 253 1.12 fvdl ffs_dinode1_swap(&dp1[i], &dp1[i]); 254 1.12 fvdl } 255 1.12 fvdl } 256 1.12 fvdl gotit: 257 1.12 fvdl if (is_ufs2) { 258 1.12 fvdl dp2 = &((struct ufs2_dinode *)inoblock)[inum - minino]; 259 1.12 fvdl return (union dinode *)dp2; 260 1.12 fvdl } 261 1.12 fvdl dp1 = &((struct ufs1_dinode *)inoblock)[inum - minino]; 262 1.12 fvdl return ((union dinode *)dp1); 263 1.1 perseant } 264