lfs_inode.c revision 1.7
1/* $NetBSD: lfs_inode.c,v 1.7 2003/04/02 10:39:25 fvdl Exp $ */ 2 3/*- 4 * Copyright (c) 1980, 1991, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36#include <sys/cdefs.h> 37#ifndef lint 38__COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40#endif /* not lint */ 41 42#ifndef lint 43#if 0 44static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/1/95"; 45#else 46__RCSID("$NetBSD: lfs_inode.c,v 1.7 2003/04/02 10:39:25 fvdl Exp $"); 47#endif 48#endif /* not lint */ 49 50#include <sys/param.h> 51#include <sys/time.h> 52#include <sys/stat.h> 53#include <ufs/ufs/dir.h> 54#include <ufs/ufs/dinode.h> 55#include <sys/mount.h> 56#include <ufs/lfs/lfs.h> 57 58#include <protocols/dumprestore.h> 59 60#include <ctype.h> 61#include <errno.h> 62#include <fts.h> 63#include <stdio.h> 64#include <string.h> 65#include <unistd.h> 66 67#include "dump.h" 68 69#define MAXIFPB (MAXBSIZE / sizeof(IFILE)) 70 71#define HASDUMPEDFILE 0x1 72#define HASSUBDIRS 0x2 73 74struct lfs *sblock; 75 76int is_ufs2 = 0; 77 78/* 79 * Read the superblock from disk, and check its magic number. 80 * Determine whether byte-swapping needs to be done on this filesystem. 81 */ 82int 83fs_read_sblock(char *superblock) 84{ 85 char tbuf[LFS_SBPAD]; 86 int ns = 0; 87 off_t sboff = LFS_LABELPAD; 88 89 sblock = (struct lfs *)superblock; 90 while(1) { 91 rawread(sboff, (char *) sblock, LFS_SBPAD); 92 if (sblock->lfs_magic != LFS_MAGIC) { 93#ifdef notyet 94 if (sblock->lfs_magic == bswap32(LFS_MAGIC)) { 95 lfs_sb_swap(sblock, sblock, 0); 96 ns = 1; 97 } else 98#endif 99 quit("bad sblock magic number\n"); 100 } 101 if (fsbtob(sblock, sblock->lfs_sboffs[0]) != sboff) { 102 sboff = fsbtob(sblock, sblock->lfs_sboffs[0]); 103 continue; 104 } 105 break; 106 } 107 108 /* 109 * Read the secondary and take the older of the two 110 */ 111 rawread(fsbtob(sblock, sblock->lfs_sboffs[1]), tbuf, LFS_SBPAD); 112#ifdef notyet 113 if (ns) 114 lfs_sb_swap(tbuf, tbuf, 0); 115#endif 116 if (((struct lfs *)tbuf)->lfs_magic != LFS_MAGIC) { 117 msg("Warning: secondary superblock at 0x%x bad magic\n", 118 fsbtodb(sblock, sblock->lfs_sboffs[1])); 119 } else { 120 if (sblock->lfs_version > 1) { 121 if (((struct lfs *)tbuf)->lfs_serial < sblock->lfs_serial) { 122 memcpy(sblock, tbuf, LFS_SBPAD); 123 sboff = fsbtob(sblock, sblock->lfs_sboffs[1]); 124 } 125 } else { 126 if (((struct lfs *)tbuf)->lfs_otstamp < sblock->lfs_otstamp) { 127 memcpy(sblock, tbuf, LFS_SBPAD); 128 sboff = fsbtob(sblock, sblock->lfs_sboffs[1]); 129 } 130 } 131 } 132 if (sboff != LFS_SBPAD) { 133 msg("Using superblock at alternate location 0x%lx\n", 134 (unsigned long)(btodb(sboff))); 135 } 136 137 return ns; 138} 139 140/* 141 * Fill in the ufsi struct, as well as the maxino and dev_bsize global 142 * variables. 143 */ 144struct ufsi * 145fs_parametrize(void) 146{ 147 static struct ufsi ufsi; 148 149 spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWINODEFMT); 150 151 ufsi.ufs_dsize = fsbtodb(sblock,sblock->lfs_size); 152 if (sblock->lfs_version == 1) 153 ufsi.ufs_dsize = sblock->lfs_size >> sblock->lfs_blktodb; 154 ufsi.ufs_bsize = sblock->lfs_bsize; 155 ufsi.ufs_bshift = sblock->lfs_bshift; 156 ufsi.ufs_fsize = sblock->lfs_fsize; 157 ufsi.ufs_frag = sblock->lfs_frag; 158 ufsi.ufs_fsatoda = sblock->lfs_fsbtodb; 159 if (sblock->lfs_version == 1) 160 ufsi.ufs_fsatoda = 0; 161 ufsi.ufs_nindir = sblock->lfs_nindir; 162 ufsi.ufs_inopb = sblock->lfs_inopb; 163 ufsi.ufs_maxsymlinklen = sblock->lfs_maxsymlinklen; 164 ufsi.ufs_bmask = ~(sblock->lfs_bmask); 165 ufsi.ufs_qbmask = sblock->lfs_bmask; 166 ufsi.ufs_fmask = ~(sblock->lfs_ffmask); 167 ufsi.ufs_qfmask = sblock->lfs_ffmask; 168 169 dev_bsize = sblock->lfs_bsize >> sblock->lfs_blktodb; 170 171 return &ufsi; 172} 173 174ino_t 175fs_maxino(void) 176{ 177 return ((getino(sblock->lfs_ifile)->dp1.di_size 178 - (sblock->lfs_cleansz + sblock->lfs_segtabsz) 179 * sblock->lfs_bsize) 180 / sblock->lfs_bsize) * sblock->lfs_ifpb - 1; 181} 182 183void 184fs_mapinodes(ino_t maxino, u_int64_t *tapesz, int *anydirskipped) 185{ 186 ino_t ino; 187 188 for (ino = ROOTINO; ino < maxino; ino++) 189 mapfileino(ino, tapesz, anydirskipped); 190} 191 192/* 193 * XXX KS - I know there's a better way to do this. 194 */ 195#define BASE_SINDIR (NDADDR) 196#define BASE_DINDIR (NDADDR+NINDIR(fs)) 197#define BASE_TINDIR (NDADDR+NINDIR(fs)+NINDIR(fs)*NINDIR(fs)) 198 199#define D_UNITS (NINDIR(fs)) 200#define T_UNITS (NINDIR(fs)*NINDIR(fs)) 201 202static daddr_t 203lfs_bmap(struct lfs *fs, struct ufs1_dinode *idinode, daddr_t lbn) 204{ 205 daddr_t residue, up; 206 int off=0; 207 char bp[MAXBSIZE]; 208 209 if(lbn > 0 && lbn > lblkno(fs, idinode->di_size)) { 210 return UNASSIGNED; 211 } 212 /* 213 * Indirect blocks: if it is a first-level indirect, pull its 214 * address from the inode; otherwise, call ourselves to find the 215 * address of the parent indirect block, and load that to find 216 * the desired address. 217 */ 218 if(lbn < 0) { 219 lbn *= -1; 220 if(lbn == NDADDR) { 221 /* printf("lbn %d: single indir base\n", -lbn); */ 222 return idinode->di_ib[0]; /* single indirect */ 223 } else if(lbn == BASE_DINDIR+1) { 224 /* printf("lbn %d: double indir base\n", -lbn); */ 225 return idinode->di_ib[1]; /* double indirect */ 226 } else if(lbn == BASE_TINDIR+2) { 227 /* printf("lbn %d: triple indir base\n", -lbn); */ 228 return idinode->di_ib[2]; /* triple indirect */ 229 } 230 231 /* 232 * Find the immediate parent. This is essentially finding the 233 * residue of modulus, and then rounding accordingly. 234 */ 235 residue = (lbn-NDADDR) % NINDIR(fs); 236 if(residue == 1) { 237 /* Double indirect. Parent is the triple. */ 238 up = idinode->di_ib[2]; 239 off = (lbn-2-BASE_TINDIR)/(NINDIR(fs)*NINDIR(fs)); 240 if(up == UNASSIGNED || up == LFS_UNUSED_DADDR) 241 return UNASSIGNED; 242 /* printf("lbn %d: parent is the triple\n", -lbn); */ 243 bread(fsbtodb(sblock, up), bp, sblock->lfs_bsize); 244 /* XXX ondisk32 */ 245 return (daddr_t)((int32_t *)bp)[off]; 246 } else /* residue == 0 */ { 247 /* Single indirect. Two cases. */ 248 if(lbn < BASE_TINDIR) { 249 /* Parent is the double, simple */ 250 up = -(BASE_DINDIR) - 1; 251 off = (lbn-BASE_DINDIR) / D_UNITS; 252 /* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */ 253 } else { 254 /* Ancestor is the triple, more complex */ 255 up = ((lbn-BASE_TINDIR) / T_UNITS) 256 * T_UNITS + BASE_TINDIR + 1; 257 off = (lbn/D_UNITS) - (up/D_UNITS); 258 up = -up; 259 /* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */ 260 } 261 } 262 } else { 263 /* Direct block. Its parent must be a single indirect. */ 264 if(lbn < NDADDR) 265 return idinode->di_db[lbn]; 266 else { 267 /* Parent is an indirect block. */ 268 up = -(((lbn-NDADDR) / D_UNITS) * D_UNITS + NDADDR); 269 off = (lbn-NDADDR) % D_UNITS; 270 /* printf("lbn %d: parent is %d/%d\n", lbn,up,off); */ 271 } 272 } 273 up = lfs_bmap(fs,idinode,up); 274 if(up == UNASSIGNED || up == LFS_UNUSED_DADDR) 275 return UNASSIGNED; 276 bread(fsbtodb(sblock, up), bp, sblock->lfs_bsize); 277 /* XXX ondisk32 */ 278 return (daddr_t)((int32_t *)bp)[off]; 279} 280 281static struct ifile * 282lfs_ientry(ino_t ino) 283{ 284 static struct ifile ifileblock[MAXIFPB]; 285 static daddr_t ifblkno; 286 daddr_t lbn; 287 daddr_t blkno; 288 union dinode *dp; 289 290 lbn = ino/sblock->lfs_ifpb + sblock->lfs_cleansz + sblock->lfs_segtabsz; 291 dp = getino(sblock->lfs_ifile); 292 blkno = lfs_bmap(sblock, &dp->dp1 ,lbn); 293 if (blkno != ifblkno) 294 bread(fsbtodb(sblock, blkno), (char *)ifileblock, 295 sblock->lfs_bsize); 296 return ifileblock + (ino % sblock->lfs_ifpb); 297} 298 299/* Search a block for a specific dinode. */ 300static struct ufs1_dinode * 301lfs_ifind(struct lfs *fs, ino_t ino, struct ufs1_dinode *dip) 302{ 303 register int cnt; 304 305 for(cnt=0;cnt<INOPB(fs);cnt++) 306 if(dip[cnt].di_inumber == ino) 307 return &(dip[cnt]); 308 return NULL; 309} 310 311union dinode * 312getino(inum) 313 ino_t inum; 314{ 315 static daddr_t inoblkno; 316 daddr_t blkno; 317 static struct ufs1_dinode inoblock[MAXBSIZE / sizeof (struct ufs1_dinode)]; 318 static struct ufs1_dinode ifile_dinode; /* XXX fill this in */ 319 static struct ufs1_dinode empty_dinode; /* Always stays zeroed */ 320 struct ufs1_dinode *dp; 321 322 if(inum == sblock->lfs_ifile) { 323 /* Load the ifile inode if not already */ 324 if(ifile_dinode.di_u.inumber == 0) { 325 blkno = sblock->lfs_idaddr; 326 bread(fsbtodb(sblock, blkno), (char *)inoblock, 327 (int)sblock->lfs_bsize); 328 dp = lfs_ifind(sblock, inum, inoblock); 329 ifile_dinode = *dp; /* Structure copy */ 330 } 331 return (union dinode *)&ifile_dinode; 332 } 333 334 curino = inum; 335 blkno = lfs_ientry(inum)->if_daddr; 336 if(blkno == LFS_UNUSED_DADDR) 337 return (union dinode *)&empty_dinode; 338 339 if(blkno != inoblkno) { 340 bread(fsbtodb(sblock, blkno), (char *)inoblock, 341 (int)sblock->lfs_bsize); 342#ifdef notyet 343 if (needswap) 344 for (i = 0; i < MAXINOPB; i++) 345 ffs_dinode_swap(&inoblock[i], &inoblock[i]); 346#endif 347 } 348 return (union dinode *)lfs_ifind(sblock, inum, inoblock); 349} 350