forked from hrishikeshgoyal/ext2fs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext2fs_bmap.c
340 lines (300 loc) · 10.2 KB
/
ext2fs_bmap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* $NetBSD: ext2fs_bmap.c,v 1.27 2016/06/03 15:35:48 christos Exp $ */
/*
* Copyright (c) 1989, 1991, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)ufs_bmap.c 8.6 (Berkeley) 1/21/94
* Modified for ext2fs by Manuel Bouyer.
*/
/*
* Copyright (c) 1997 Manuel Bouyer.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @(#)ufs_bmap.c 8.6 (Berkeley) 1/21/94
* Modified for ext2fs by Manuel Bouyer.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ext2fs_bmap.c,v 1.27 2016/06/03 15:35:48 christos Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/proc.h>
#include <sys/vnode.h>
#include <sys/mount.h>
#include <sys/resourcevar.h>
#include <sys/trace.h>
#include <miscfs/specfs/specdev.h>
#include <ufs/ufs/inode.h>
#include <ufs/ufs/ufsmount.h>
#include <ufs/ufs/ufs_extern.h>
#include <ufs/ext2fs/ext2fs.h>
#include <ufs/ext2fs/ext2fs_extern.h>
static int ext4_bmapext(struct vnode *, int32_t, int64_t *, int *, int *);
static int ext2fs_bmaparray(struct vnode *, daddr_t, daddr_t *, struct indir *,
int *, int *);
#define is_sequential(ump, a, b) ((b) == (a) + ump->um_seqinc)
/*
* Bmap converts a the logical block number of a file to its physical block
* number on the disk. The conversion is done by using the logical block
* number to index into the array of block pointers described by the dinode.
*/
int
ext2fs_bmap(void *v)
{
struct vop_bmap_args /* {
struct vnode *a_vp;
daddr_t a_bn;
struct vnode **a_vpp;
daddr_t *a_bnp;
int *a_runp;
} */ *ap = v;
/*
* Check for underlying vnode requests and ensure that logical
* to physical mapping is requested.
*/
if (ap->a_vpp != NULL)
*ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
if (ap->a_bnp == NULL)
return (0);
printf("in bmap ,inode no: %lu, i_flag value %x \n",VTOI(ap->a_vp)->i_number ,VTOI(ap->a_vp)->i_din.e2fs_din->e2di_flags );
if (VTOI(ap->a_vp)->i_din.e2fs_din->e2di_flags & IN_E4EXTENTS)
return ext4_bmapext(ap->a_vp, ap->a_bn, ap->a_bnp,
ap->a_runp, NULL);
else
return ext2fs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL,
NULL, ap->a_runp);
}
/*
* Convert the logical block number of a file to its physical block number
* on the disk within ext4 extents.
*/
static int
ext4_bmapext(struct vnode *vp, int32_t bn, int64_t *bnp, int *runp, int *runb)
{
struct inode *ip;
struct m_ext2fs *fs;
struct ext4_extent *ep;
struct ext4_extent_path path = { .ep_bp = NULL };
daddr_t lbn;
int error = 0;
ip = VTOI(vp);
fs = ip->i_e2fs;
lbn = bn;
/* XXX: Should not initialize on error? */
if (runp != NULL)
*runp = 0;
if (runb != NULL)
*runb = 0;
ext4_ext_find_extent(fs, ip, lbn, &path);
if (path.ep_is_sparse) {
*bnp = -1;
if (runp != NULL)
*runp = path.ep_sparse_ext.e_len -
(lbn - path.ep_sparse_ext.e_blk) - 1;
if (runb != NULL)
*runb = lbn - path.ep_sparse_ext.e_blk;
} else {
if (path.ep_ext == NULL) {
error = EIO;
goto out;
}
ep = path.ep_ext;
*bnp = fsbtodb(fs, lbn - ep->e_blk
+ (ep->e_start_lo | (daddr_t)ep->e_start_hi << 32));
if (*bnp == 0)
*bnp = -1;
if (runp != NULL)
*runp = ep->e_len - (lbn - ep->e_blk) - 1;
if (runb != NULL)
*runb = lbn - ep->e_blk;
}
out:
if (path.ep_bp != NULL) {
brelse(path.ep_bp, 0);
}
return error;
}
/*
* Indirect blocks are now on the vnode for the file. They are given negative
* logical block numbers. Indirect blocks are addressed by the negative
* address of the first data block to which they point. Double indirect blocks
* are addressed by one less than the address of the first indirect block to
* which they point. Triple indirect blocks are addressed by one less than
* the address of the first double indirect block to which they point.
*
* ext2fs_bmaparray does the bmap conversion, and if requested returns the
* array of logical blocks which must be traversed to get to a block.
* Each entry contains the offset into that block that gets you to the
* next block and the disk address of the block (if it is assigned).
*/
int
ext2fs_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, struct indir *ap,
int *nump, int *runp)
{
struct inode *ip;
struct buf *bp, *cbp;
struct ufsmount *ump;
struct mount *mp;
struct indir a[EXT2FS_NIADDR+1], *xap;
daddr_t daddr;
daddr_t metalbn;
int error, maxrun = 0, num;
ip = VTOI(vp);
mp = vp->v_mount;
ump = ip->i_ump;
#ifdef DIAGNOSTIC
if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
panic("ext2fs_bmaparray: invalid arguments");
#endif
if (runp) {
/*
* XXX
* If MAXBSIZE is the largest transfer the disks can handle,
* we probably want maxrun to be 1 block less so that we
* don't create a block larger than the device can handle.
*/
*runp = 0;
maxrun = MAXBSIZE / mp->mnt_stat.f_iosize - 1;
}
if (bn >= 0 && bn < EXT2FS_NDADDR) {
/* XXX ondisk32 */
*bnp = blkptrtodb(ump, fs2h32(ip->i_e2fs_blocks[bn]));
if (*bnp == 0)
*bnp = -1;
else if (runp)
/* XXX ondisk32 */
for (++bn; bn < EXT2FS_NDADDR && *runp < maxrun &&
is_sequential(ump, (daddr_t)fs2h32(ip->i_e2fs_blocks[bn - 1]),
(daddr_t)fs2h32(ip->i_e2fs_blocks[bn]));
++bn, ++*runp);
return (0);
}
xap = ap == NULL ? a : ap;
if (!nump)
nump = #
if ((error = ufs_getlbns(vp, bn, xap, nump)) != 0)
return (error);
num = *nump;
/* Get disk address out of indirect block array */
/* XXX ondisk32 */
daddr = fs2h32(ip->i_e2fs_blocks[EXT2FS_NDADDR + xap->in_off]);
#ifdef DIAGNOSTIC
if (num > EXT2FS_NIADDR + 1 || num < 1) {
printf("ext2fs_bmaparray: num=%d\n", num);
panic("ext2fs_bmaparray: num");
}
#endif
for (bp = NULL, ++xap; --num; ++xap) {
/*
* Exit the loop if there is no disk address assigned yet and
* the indirect block isn't in the cache, or if we were
* looking for an indirect block and we've found it.
*/
metalbn = xap->in_lbn;
if (metalbn == bn)
break;
if (daddr == 0) {
mutex_enter(&bufcache_lock);
cbp = incore(vp, metalbn);
mutex_exit(&bufcache_lock);
if (cbp == NULL)
break;
}
/*
* If we get here, we've either got the block in the cache
* or we have a disk address for it, go fetch it.
*/
if (bp)
brelse(bp, 0);
xap->in_exists = 1;
bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0);
if (bp == NULL) {
/*
* getblk() above returns NULL only iff we are
* pagedaemon. See the implementation of getblk
* for detail.
*/
return (ENOMEM);
}
if (bp->b_oflags & (BO_DONE | BO_DELWRI)) {
trace(TR_BREADHIT, pack(vp, size), metalbn);
}
#ifdef DIAGNOSTIC
else if (!daddr)
panic("ext2fs_bmaparry: indirect block not in cache");
#endif
else {
trace(TR_BREADMISS, pack(vp, size), metalbn);
bp->b_blkno = blkptrtodb(ump, daddr);
bp->b_flags |= B_READ;
VOP_STRATEGY(vp, bp);
curlwp->l_ru.ru_inblock++; /* XXX */
if ((error = biowait(bp)) != 0) {
brelse(bp, 0);
return (error);
}
}
/* XXX ondisk32 */
daddr = fs2h32(((int32_t *)bp->b_data)[xap->in_off]);
if (num == 1 && daddr && runp)
/* XXX ondisk32 */
for (bn = xap->in_off + 1;
bn < MNINDIR(ump) && *runp < maxrun &&
is_sequential(ump, ((int32_t *)bp->b_data)[bn - 1],
((int32_t *)bp->b_data)[bn]);
++bn, ++*runp);
}
if (bp)
brelse(bp, 0);
daddr = blkptrtodb(ump, daddr);
*bnp = daddr == 0 ? -1 : daddr;
return (0);
}