xref: /386bsd/usr/src/kernel/ufs/ufsboot_bmap.c (revision a2142627)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	$Id: ufsboot_bmap.c,v 1.1 94/10/20 10:56:48 root Exp $
34  */
35 
36 #include "sys/param.h"
37 #include "sys/errno.h"
38 #include "ufs_dinode.h"
39 #include "ufs.h"
40 
41 /*
42  * Bmap converts a the logical block number of a file
43  * to its physical block number on the disk. The conversion
44  * is done by using the logical block number to index into
45  * the array of block pointers described by the dinode.
46  */
47 extern struct fs *fs;
48 extern int bdev;
49 
50 static daddr_t bap[2*1024];
51 static daddr_t bnobap;
52 
bmap(dip,bn,bnp)53 bmap(dip, bn, bnp)
54 	register struct dinode *dip;
55 	register daddr_t bn;
56 	daddr_t	*bnp;
57 {
58 	register daddr_t nb;
59 	int i, j, sh;
60 	int error;
61 
62 /*fprintf(stderr, "bmap %d ", bn);*/
63 	if (bn < 0)
64 		return (EFBIG);
65 
66 	/*
67 	 * The first NDADDR blocks are direct blocks
68 	 */
69 	if (bn < NDADDR) {
70 		nb = dip->di_db[bn];
71 		if (nb == 0) {
72 			*bnp = (daddr_t)-1;
73 /*fprintf(stderr, "%d\n", *bnp);*/
74 			return (0);
75 		}
76 		*bnp = fsbtodb(fs, nb);
77 /*fprintf(stderr, "%d\n", *bnp);*/
78 		return (0);
79 	}
80 	/*
81 	 * Determine the number of levels of indirection.
82 	 */
83 	sh = 1;
84 	bn -= NDADDR;
85 	for (j = NIADDR; j > 0; j--) {
86 		sh *= NINDIR(fs);
87 		if (bn < sh)
88 			break;
89 		bn -= sh;
90 	}
91 	if (j == 0)
92 		return (EFBIG);
93 	/*
94 	 * Fetch through the indirect blocks.
95 	 */
96 	nb = dip->di_ib[NIADDR - j];
97 	if (nb == 0) {
98 		*bnp = (daddr_t)-1;
99 /*fprintf(stderr, "%d\n", *bnp);*/
100 		return (0);
101 	}
102 	for (; j <= NIADDR; j++) {
103 		daddr_t bno = fsbtodb(fs, nb);
104 
105 		if (bnobap != bno &&
106 (error = bread(bdev, bno, &bap,
107 			(int)fs->fs_bsize))) {
108 			return (error);
109 		}
110 		bnobap = bno;
111 		sh /= NINDIR(fs);
112 		i = (bn / sh) % NINDIR(fs);
113 		nb = bap[i];
114 		if (nb == 0) {
115 			*bnp = (daddr_t)-1;
116 /*fprintf(stderr, "%d\n", *bnp);*/
117 			return (0);
118 		}
119 	}
120 	*bnp = fsbtodb(fs, nb);
121 /*fprintf(stderr, "%d\n", *bnp);*/
122 	return (0);
123 }
124