1 /*
2 * Copyright (c) 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)ufs_bmap.c 8.7 (Berkeley) 3/21/95
35 * $FreeBSD: src/sys/ufs/ufs/ufs_bmap.c,v 1.34.2.1 2000/03/17 10:12:14 ps Exp $
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/buf.h>
41 #include <sys/proc.h>
42 #include <sys/vnode.h>
43 #include <sys/mount.h>
44 #include <sys/resourcevar.h>
45 #include <sys/conf.h>
46
47 #include "quota.h"
48 #include "inode.h"
49 #include "ufsmount.h"
50 #include "ufs_extern.h"
51 #include "fs.h"
52
53 /*
54 * Bmap converts the logical block number of a file to its physical block
55 * number on the disk. The conversion is done by using the logical block
56 * number to index into the array of block pointers described by the dinode.
57 *
58 * BMAP must return the contiguous before and after run in bytes, inclusive
59 * of the returned block.
60 *
61 * ufs_bmap(struct vnode *a_vp, off_t a_loffset,
62 * off_t *a_doffsetp, int *a_runp, int *a_runb)
63 */
64 int
ufs_bmap(struct vop_bmap_args * ap)65 ufs_bmap(struct vop_bmap_args *ap)
66 {
67 struct fs *fs;
68 ufs_daddr_t lbn;
69 ufs_daddr_t dbn;
70 int error;
71
72 /*
73 * Check for underlying vnode requests and ensure that logical
74 * to physical mapping is requested.
75 */
76 if (ap->a_doffsetp == NULL)
77 return (0);
78
79 fs = VTOI(ap->a_vp)->i_fs;
80 KKASSERT(((int)ap->a_loffset & ((1 << fs->fs_bshift) - 1)) == 0);
81 lbn = ap->a_loffset >> fs->fs_bshift;
82
83 error = ufs_bmaparray(ap->a_vp, lbn, &dbn, NULL, NULL,
84 ap->a_runp, ap->a_runb);
85
86 if (error || dbn == (ufs_daddr_t)-1) {
87 *ap->a_doffsetp = NOOFFSET;
88 } else {
89 *ap->a_doffsetp = dbtodoff(fs, dbn);
90 if (ap->a_runp)
91 *ap->a_runp = (*ap->a_runp + 1) << fs->fs_bshift;
92 if (ap->a_runb)
93 *ap->a_runb = *ap->a_runb << fs->fs_bshift;
94 }
95 return (error);
96 }
97
98 /*
99 * Indirect blocks are now on the vnode for the file. They are given negative
100 * logical block numbers. Indirect blocks are addressed by the negative
101 * address of the first data block to which they point. Double indirect blocks
102 * are addressed by one less than the address of the first indirect block to
103 * which they point. Triple indirect blocks are addressed by one less than
104 * the address of the first double indirect block to which they point.
105 *
106 * ufs_bmaparray does the bmap conversion, and if requested returns the
107 * array of logical blocks which must be traversed to get to a block.
108 * Each entry contains the offset into that block that gets you to the
109 * next block and the disk address of the block (if it is assigned).
110 */
111 int
ufs_bmaparray(struct vnode * vp,ufs_daddr_t bn,ufs_daddr_t * bnp,struct indir * ap,int * nump,int * runp,int * runb)112 ufs_bmaparray(struct vnode *vp, ufs_daddr_t bn, ufs_daddr_t *bnp,
113 struct indir *ap, int *nump, int *runp, int *runb)
114 {
115 struct inode *ip;
116 struct buf *bp;
117 struct ufsmount *ump;
118 struct mount *mp;
119 struct fs *fs;
120 struct indir a[UFS_NIADDR+1], *xap;
121 ufs_daddr_t daddr;
122 long metalbn;
123 int error, maxrun, num;
124
125 ip = VTOI(vp);
126 mp = vp->v_mount;
127 ump = VFSTOUFS(mp);
128 fs = ip->i_fs;
129 #ifdef DIAGNOSTIC
130 if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
131 panic("ufs_bmaparray: invalid arguments");
132 #endif
133
134 if (runp) {
135 *runp = 0;
136 }
137
138 if (runb) {
139 *runb = 0;
140 }
141
142 maxrun = mp->mnt_iosize_max / mp->mnt_stat.f_iosize - 1;
143
144 xap = ap == NULL ? a : ap;
145 if (!nump)
146 nump = #
147 error = ufs_getlbns(vp, bn, xap, nump);
148 if (error)
149 return (error);
150
151 num = *nump;
152 if (num == 0) {
153 *bnp = blkptrtodb(ump, ip->i_db[bn]);
154 if (*bnp == 0)
155 *bnp = -1;
156 else if (runp) {
157 daddr_t bnb = bn;
158 for (++bn; bn < UFS_NDADDR && *runp < maxrun &&
159 is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
160 ++bn, ++*runp);
161 bn = bnb;
162 if (runb && (bn > 0)) {
163 for (--bn; (bn >= 0) && (*runb < maxrun) &&
164 is_sequential(ump, ip->i_db[bn],
165 ip->i_db[bn+1]);
166 --bn, ++*runb);
167 }
168 }
169 return (0);
170 }
171
172
173 /* Get disk address out of indirect block array */
174 daddr = ip->i_ib[xap->in_off];
175
176 for (bp = NULL, ++xap; --num; ++xap) {
177 /*
178 * Exit the loop if there is no disk address assigned yet and
179 * the indirect block isn't in the cache, or if we were
180 * looking for an indirect block and we've found it.
181 */
182 metalbn = xap->in_lbn;
183 if ((daddr == 0 &&
184 !findblk(vp, dbtodoff(fs, metalbn), FINDBLK_TEST)) ||
185 metalbn == bn) {
186 break;
187 }
188 /*
189 * If we get here, we've either got the block in the cache
190 * or we have a disk address for it, go fetch it.
191 */
192 if (bp)
193 bqrelse(bp);
194
195 bp = getblk(vp, lblktodoff(fs, metalbn),
196 mp->mnt_stat.f_iosize, 0, 0);
197 if ((bp->b_flags & B_CACHE) == 0) {
198 #ifdef DIAGNOSTIC
199 if (!daddr)
200 panic("ufs_bmaparray: indirect block not in cache");
201 #endif
202 /*
203 * cached disk addr in bio2, do I/O on bio1. It
204 * will probably hit the vfs's strategy function
205 * which will then use the cached offset in bio2.
206 */
207 bp->b_bio1.bio_done = biodone_sync;
208 bp->b_bio1.bio_flags |= BIO_SYNC;
209 bp->b_bio2.bio_offset = fsbtodoff(fs, daddr);
210 bp->b_flags &= ~(B_INVAL|B_ERROR);
211 bp->b_cmd = BUF_CMD_READ;
212 vfs_busy_pages(bp->b_vp, bp);
213 vn_strategy(bp->b_vp, &bp->b_bio1);
214 error = biowait(&bp->b_bio1, "biord");
215 if (error) {
216 brelse(bp);
217 return (error);
218 }
219 }
220
221 daddr = ((ufs_daddr_t *)bp->b_data)[xap->in_off];
222 if (num == 1 && daddr && runp) {
223 for (bn = xap->in_off + 1;
224 bn < MNINDIR(ump) && *runp < maxrun &&
225 is_sequential(ump,
226 ((ufs_daddr_t *)bp->b_data)[bn - 1],
227 ((ufs_daddr_t *)bp->b_data)[bn]);
228 ++bn, ++*runp);
229 bn = xap->in_off;
230 if (runb && bn) {
231 for(--bn; bn >= 0 && *runb < maxrun &&
232 is_sequential(ump, ((daddr_t *)bp->b_data)[bn],
233 ((daddr_t *)bp->b_data)[bn+1]);
234 --bn, ++*runb);
235 }
236 }
237 }
238 if (bp)
239 bqrelse(bp);
240
241 daddr = blkptrtodb(ump, daddr);
242 *bnp = daddr == 0 ? -1 : daddr;
243 return (0);
244 }
245
246 /*
247 * Create an array of logical block number/offset pairs which represent the
248 * path of indirect blocks required to access a data block. The first "pair"
249 * contains the logical block number of the appropriate single, double or
250 * triple indirect block and the offset into the inode indirect block array.
251 * Note, the logical block number of the inode single/double/triple indirect
252 * block appears twice in the array, once with the offset into the i_ib and
253 * once with the offset into the page itself.
254 */
255 int
ufs_getlbns(struct vnode * vp,ufs_daddr_t bn,struct indir * ap,int * nump)256 ufs_getlbns(struct vnode *vp, ufs_daddr_t bn, struct indir *ap, int *nump)
257 {
258 long blockcnt, metalbn, realbn;
259 struct ufsmount *ump;
260 int i, numlevels, off;
261 int64_t qblockcnt;
262
263 ump = VFSTOUFS(vp->v_mount);
264 if (nump)
265 *nump = 0;
266 numlevels = 0;
267 realbn = bn;
268 if ((long)bn < 0)
269 bn = -(long)bn;
270
271 /* The first UFS_NDADDR blocks are direct blocks. */
272 if (bn < UFS_NDADDR)
273 return (0);
274
275 /*
276 * Determine the number of levels of indirection. After this loop
277 * is done, blockcnt indicates the number of data blocks possible
278 * at the previous level of indirection, and UFS_NIADDR-i is the number
279 * of levels of indirection needed to locate the requested block.
280 */
281 for (blockcnt = 1, i = UFS_NIADDR, bn -= UFS_NDADDR;; i--, bn -= blockcnt) {
282 if (i == 0)
283 return (EFBIG);
284 /*
285 * Use int64_t's here to avoid overflow for triple indirect
286 * blocks when longs have 32 bits and the block size is more
287 * than 4K.
288 */
289 qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
290 if (bn < qblockcnt)
291 break;
292 blockcnt = qblockcnt;
293 }
294
295 /* Calculate the address of the first meta-block. */
296 if (realbn >= 0)
297 metalbn = -(realbn - bn + UFS_NIADDR - i);
298 else
299 metalbn = -(-realbn - bn + UFS_NIADDR - i);
300
301 /*
302 * At each iteration, off is the offset into the bap array which is
303 * an array of disk addresses at the current level of indirection.
304 * The logical block number and the offset in that block are stored
305 * into the argument array.
306 */
307 ap->in_lbn = metalbn;
308 ap->in_off = off = UFS_NIADDR - i;
309 ap++;
310 for (++numlevels; i <= UFS_NIADDR; i++) {
311 /* If searching for a meta-data block, quit when found. */
312 if (metalbn == realbn)
313 break;
314
315 off = (bn / blockcnt) % MNINDIR(ump);
316
317 ++numlevels;
318 ap->in_lbn = metalbn;
319 ap->in_off = off;
320 ++ap;
321
322 metalbn -= -1 + off * blockcnt;
323 blockcnt /= MNINDIR(ump);
324 }
325 if (nump)
326 *nump = numlevels;
327 return (0);
328 }
329