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