xref: /freebsd/usr.sbin/makefs/ffs/ufs_bmap.c (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1 /*	$NetBSD: ufs_bmap.c,v 1.14 2004/06/20 22:20:18 jmc Exp $	*/
2 /* From: NetBSD: ufs_bmap.c,v 1.14 2001/11/08 05:00:51 chs Exp */
3 
4 /*-
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Copyright (c) 1989, 1991, 1993
8  *	The Regents of the University of California.  All rights reserved.
9  * (c) UNIX System Laboratories, Inc.
10  * All or some portions of this file are derived from material licensed
11  * to the University of California by American Telephone and Telegraph
12  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
13  * the permission of UNIX System Laboratories, Inc.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	@(#)ufs_bmap.c	8.8 (Berkeley) 8/11/95
40  */
41 
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44 
45 #include <sys/param.h>
46 #include <sys/time.h>
47 
48 #include <assert.h>
49 #include <errno.h>
50 #include <strings.h>
51 
52 #include "makefs.h"
53 
54 #include <ufs/ufs/dinode.h>
55 #include <ufs/ffs/fs.h>
56 
57 #include "ffs/ufs_bswap.h"
58 #include "ffs/ufs_inode.h"
59 #include "ffs/ffs_extern.h"
60 
61 /*
62  * Create an array of logical block number/offset pairs which represent the
63  * path of indirect blocks required to access a data block.  The first "pair"
64  * contains the logical block number of the appropriate single, double or
65  * triple indirect block and the offset into the inode indirect block array.
66  * Note, the logical block number of the inode single/double/triple indirect
67  * block appears twice in the array, once with the offset into the i_ffs_ib and
68  * once with the offset into the page itself.
69  */
70 int
71 ufs_getlbns(struct inode *ip, daddr_t bn, struct indir *ap, int *nump)
72 {
73 	daddr_t metalbn, realbn;
74 	int64_t blockcnt;
75 	int lbc;
76 	int i, numlevels, off;
77 	u_long lognindir;
78 
79 	lognindir = ffs(NINDIR(ip->i_fs)) - 1;
80 	if (nump)
81 		*nump = 0;
82 	numlevels = 0;
83 	realbn = bn;
84 	if ((long)bn < 0)
85 		bn = -(long)bn;
86 
87 	assert (bn >= UFS_NDADDR);
88 
89 	/*
90 	 * Determine the number of levels of indirection.  After this loop
91 	 * is done, blockcnt indicates the number of data blocks possible
92 	 * at the given level of indirection, and UFS_NIADDR - i is the number
93 	 * of levels of indirection needed to locate the requested block.
94 	 */
95 
96 	bn -= UFS_NDADDR;
97 	for (lbc = 0, i = UFS_NIADDR;; i--, bn -= blockcnt) {
98 		if (i == 0)
99 			return (EFBIG);
100 
101 		lbc += lognindir;
102 		blockcnt = (int64_t)1 << lbc;
103 
104 		if (bn < blockcnt)
105 			break;
106 	}
107 
108 	/* Calculate the address of the first meta-block. */
109 	if (realbn >= 0)
110 		metalbn = -(realbn - bn + UFS_NIADDR - i);
111 	else
112 		metalbn = -(-realbn - bn + UFS_NIADDR - i);
113 
114 	/*
115 	 * At each iteration, off is the offset into the bap array which is
116 	 * an array of disk addresses at the current level of indirection.
117 	 * The logical block number and the offset in that block are stored
118 	 * into the argument array.
119 	 */
120 	ap->in_lbn = metalbn;
121 	ap->in_off = off = UFS_NIADDR - i;
122 	ap++;
123 	for (++numlevels; i <= UFS_NIADDR; i++) {
124 		/* If searching for a meta-data block, quit when found. */
125 		if (metalbn == realbn)
126 			break;
127 
128 		lbc -= lognindir;
129 		blockcnt = (int64_t)1 << lbc;
130 		off = (bn >> lbc) & (NINDIR(ip->i_fs) - 1);
131 
132 		++numlevels;
133 		ap->in_lbn = metalbn;
134 		ap->in_off = off;
135 		++ap;
136 
137 		metalbn -= -1 + (off << lbc);
138 	}
139 	if (nump)
140 		*nump = numlevels;
141 	return (0);
142 }
143