xref: /openbsd-src/sys/ufs/ext2fs/ext2fs_bmap.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: ext2fs_bmap.c,v 1.25 2014/07/14 08:54:13 pelikan Exp $	*/
2 /*	$NetBSD: ext2fs_bmap.c,v 1.5 2000/03/30 12:41:11 augustss Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Manuel Bouyer.
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.6 (Berkeley) 1/21/94
39  * Modified for ext2fs by Manuel Bouyer.
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/buf.h>
45 #include <sys/proc.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/resourcevar.h>
49 #include <sys/specdev.h>
50 
51 #include <ufs/ufs/quota.h>
52 #include <ufs/ufs/inode.h>
53 #include <ufs/ufs/ufsmount.h>
54 #include <ufs/ufs/ufs_extern.h>
55 #include <ufs/ext2fs/ext2fs.h>
56 #include <ufs/ext2fs/ext2fs_extents.h>
57 #include <ufs/ext2fs/ext2fs_extern.h>
58 
59 static int	ext4_bmapext(struct vnode *, daddr_t, daddr_t *, struct indir *,
60     int *, int *);
61 static int	ext2fs_bmaparray(struct vnode *, daddr_t, daddr_t *, struct indir *,
62     int *, int *);
63 
64 /*
65  * Bmap converts a the logical block number of a file to its physical block
66  * number on the disk. The conversion is done by using the logical block
67  * number to index into the array of block pointers described by the dinode.
68  */
69 int
70 ext2fs_bmap(void *v)
71 {
72 	struct vop_bmap_args *ap = v;
73 
74 	/*
75 	 * Check for underlying vnode requests and ensure that logical
76 	 * to physical mapping is requested.
77 	 */
78 	if (ap->a_vpp != NULL)
79 		*ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
80 	if (ap->a_bnp == NULL)
81 		return (0);
82 
83 	if (VTOI(ap->a_vp)->i_e2din->e2di_flags & EXT4_EXTENTS) {
84 		return (ext4_bmapext(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL,
85 		    ap->a_runp));
86 	}
87 	return (ext2fs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL,
88 		ap->a_runp));
89 }
90 
91 /*
92  * Logical block number of a file -> physical block number on disk within ext4 extents.
93  */
94 int
95 ext4_bmapext(struct vnode *vp, daddr_t bn, daddr_t *bnp, struct indir *ap, int *nump, int *runp)
96 {
97 	struct inode *ip;
98 	struct m_ext2fs *fs;
99 	struct ext4_extent *ep;
100 	struct ext4_extent_path path;
101 	daddr_t pos;
102 
103 	ip = VTOI(vp);
104 	fs = ip->i_e2fs;
105 
106 	if (runp != NULL)
107 		*runp = 0;
108 	if (nump != NULL)
109 		*nump = 0;
110 
111 	ext4_ext_find_extent(fs, ip, bn, &path);
112 	if ((ep = path.ep_ext) == NULL)
113 		return (EIO);
114 
115 	pos = bn - ep->e_blk + (((daddr_t)ep->e_start_hi << 32) | ep->e_start_lo);
116 	if ((*bnp = fsbtodb(fs, pos)) == 0)
117 		*bnp = -1;
118 	return (0);
119 }
120 
121 /*
122  * Indirect blocks are now on the vnode for the file.  They are given negative
123  * logical block numbers.  Indirect blocks are addressed by the negative
124  * address of the first data block to which they point.  Double indirect blocks
125  * are addressed by one less than the address of the first indirect block to
126  * which they point.  Triple indirect blocks are addressed by one less than
127  * the address of the first double indirect block to which they point.
128  *
129  * ext2fs_bmaparray does the bmap conversion, and if requested returns the
130  * array of logical blocks which must be traversed to get to a block.
131  * Each entry contains the offset into that block that gets you to the
132  * next block and the disk address of the block (if it is assigned).
133  */
134 
135 int
136 ext2fs_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp,
137     struct indir *ap, int *nump, int *runp)
138 {
139 	struct inode *ip;
140 	struct buf *bp;
141 	struct ufsmount *ump;
142 	struct mount *mp;
143 	struct vnode *devvp;
144 	struct indir a[NIADDR+1], *xap;
145 	int32_t daddr;
146 	long metalbn;
147 	int error, maxrun = 0, num;
148 
149 	ip = VTOI(vp);
150 	mp = vp->v_mount;
151 	ump = VFSTOUFS(mp);
152 #ifdef DIAGNOSTIC
153 	if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
154 		panic("ext2fs_bmaparray: invalid arguments");
155 #endif
156 
157 	if (runp) {
158 		/*
159 		 * XXX
160 		 * If MAXBSIZE is the largest transfer the disks can handle,
161 		 * we probably want maxrun to be 1 block less so that we
162 		 * don't create a block larger than the device can handle.
163 		 */
164 		*runp = 0;
165 		maxrun = MAXBSIZE / mp->mnt_stat.f_iosize - 1;
166 	}
167 
168 	xap = ap == NULL ? a : ap;
169 	if (!nump)
170 		nump = &num;
171 	if ((error = ufs_getlbns(vp, bn, xap, nump)) != 0)
172 		return (error);
173 
174 	num = *nump;
175 	if (num == 0) {
176 		*bnp = blkptrtodb(ump, letoh32(ip->i_e2fs_blocks[bn]));
177 		if (*bnp == 0)
178 			*bnp = -1;
179 		else if (runp)
180 			for (++bn; bn < NDADDR && *runp < maxrun &&
181 			    is_sequential(ump,
182 			    letoh32(ip->i_e2fs_blocks[bn - 1]),
183 			    letoh32(ip->i_e2fs_blocks[bn]));
184 			    ++bn, ++*runp)
185 				/* nothing */;
186 		return (0);
187 	}
188 
189 
190 	/* Get disk address out of indirect block array */
191 	daddr = letoh32(ip->i_e2fs_blocks[NDADDR + xap->in_off]);
192 
193 	devvp = VFSTOUFS(vp->v_mount)->um_devvp;
194 
195 #ifdef DIAGNOSTIC
196     if (num > NIADDR + 1 || num < 1) {
197 		printf("ext2fs_bmaparray: num=%d\n", num);
198 		panic("ext2fs_bmaparray: num");
199 	}
200 #endif
201 	for (bp = NULL, ++xap; --num; ++xap) {
202 		/*
203 		 * Exit the loop if there is no disk address assigned yet and
204 		 * the indirect block isn't in the cache, or if we were
205 		 * looking for an indirect block and we've found it.
206 		 */
207 
208 		metalbn = xap->in_lbn;
209 		if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn)
210 			break;
211 		/*
212 		 * If we get here, we've either got the block in the cache
213 		 * or we have a disk address for it, go fetch it.
214 		 */
215 		if (bp)
216 			brelse(bp);
217 
218 		xap->in_exists = 1;
219 		bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0);
220 		if (bp->b_flags & (B_DONE | B_DELWRI)) {
221 			;
222 		}
223 #ifdef DIAGNOSTIC
224 		else if (!daddr)
225 			panic("ext2fs_bmaparry: indirect block not in cache");
226 #endif
227 		else {
228 			bp->b_blkno = blkptrtodb(ump, daddr);
229 			bp->b_flags |= B_READ;
230 			VOP_STRATEGY(bp);
231 			curproc->p_ru.ru_inblock++;		/* XXX */
232 			bcstats.pendingreads++;
233 			if ((error = biowait(bp)) != 0) {
234 				brelse(bp);
235 				return (error);
236 			}
237 		}
238 
239 		daddr = letoh32(((int32_t *)bp->b_data)[xap->in_off]);
240 		if (num == 1 && daddr && runp)
241 			for (bn = xap->in_off + 1;
242 			    bn < MNINDIR(ump) && *runp < maxrun &&
243 			    is_sequential(ump, ((u_int32_t *)bp->b_data)[bn - 1],
244 			    ((u_int32_t *)bp->b_data)[bn]);
245 			    ++bn, ++*runp)
246 				/* nothing */;
247 	}
248 	if (bp)
249 		brelse(bp);
250 
251 	daddr = blkptrtodb(ump, daddr);
252 	*bnp = daddr == 0 ? -1 : daddr;
253 	return (0);
254 }
255