OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [tags/] [eco32-0.22/] [disk/] [tools/] [fs-NetBSD/] [makefs/] [ufs_bmap.c] - Blame information for rev 185

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.