]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/gnu/fs/ext2fs/ext2_bmap.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / gnu / fs / ext2fs / ext2_bmap.c
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  * 4. 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$
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bio.h>
41 #include <sys/buf.h>
42 #include <sys/proc.h>
43 #include <sys/vnode.h>
44 #include <sys/mount.h>
45 #include <sys/resourcevar.h>
46 #include <sys/stat.h>
47
48 #include <gnu/fs/ext2fs/inode.h>
49 #include <gnu/fs/ext2fs/ext2_fs.h>
50 #include <gnu/fs/ext2fs/ext2_fs_sb.h>
51 #include <gnu/fs/ext2fs/ext2_mount.h>
52 #include <gnu/fs/ext2fs/ext2_extern.h>
53
54 /*
55  * Bmap converts a the logical block number of a file to its physical block
56  * number on the disk. The conversion is done by using the logical block
57  * number to index into the array of block pointers described by the dinode.
58  */
59 int
60 ext2_bmap(ap)
61         struct vop_bmap_args /* {
62                 struct vnode *a_vp;
63                 daddr_t a_bn;
64                 struct bufobj **a_bop;
65                 daddr_t *a_bnp;
66                 int *a_runp;
67                 int *a_runb;
68         } */ *ap;
69 {
70         int32_t blkno;
71         int error;
72
73         /*
74          * Check for underlying vnode requests and ensure that logical
75          * to physical mapping is requested.
76          */
77         if (ap->a_bop != NULL)
78                 *ap->a_bop = &VTOI(ap->a_vp)->i_devvp->v_bufobj;
79         if (ap->a_bnp == NULL)
80                 return (0);
81
82         error = ext2_bmaparray(ap->a_vp, ap->a_bn, &blkno,
83             ap->a_runp, ap->a_runb);
84         *ap->a_bnp = blkno;
85         return (error);
86 }
87
88 /*
89  * Indirect blocks are now on the vnode for the file.  They are given negative
90  * logical block numbers.  Indirect blocks are addressed by the negative
91  * address of the first data block to which they point.  Double indirect blocks
92  * are addressed by one less than the address of the first indirect block to
93  * which they point.  Triple indirect blocks are addressed by one less than
94  * the address of the first double indirect block to which they point.
95  *
96  * ufs_bmaparray does the bmap conversion, and if requested returns the
97  * array of logical blocks which must be traversed to get to a block.
98  * Each entry contains the offset into that block that gets you to the
99  * next block and the disk address of the block (if it is assigned).
100  */
101
102 int
103 ext2_bmaparray(vp, bn, bnp, runp, runb)
104         struct vnode *vp;
105         int32_t bn;
106         int32_t *bnp;
107         int *runp;
108         int *runb;
109 {
110         struct inode *ip;
111         struct buf *bp;
112         struct ext2mount *ump;
113         struct mount *mp;
114         struct vnode *devvp;
115         struct indir a[NIADDR+1], *ap;
116         int32_t daddr;
117         long metalbn;
118         int error, num, maxrun = 0, bsize;
119         int *nump;
120
121         ap = NULL;
122         ip = VTOI(vp);
123         mp = vp->v_mount;
124         ump = VFSTOEXT2(mp);
125         devvp = ump->um_devvp;
126
127         bsize = EXT2_BLOCK_SIZE(ump->um_e2fs);
128
129         if (runp) {
130                 maxrun = mp->mnt_iosize_max / bsize - 1;
131                 *runp = 0;
132         }
133
134         if (runb) {
135                 *runb = 0;
136         }
137
138
139         ap = a;
140         nump = &num;
141         error = ext2_getlbns(vp, bn, ap, nump);
142         if (error)
143                 return (error);
144
145         num = *nump;
146         if (num == 0) {
147                 *bnp = blkptrtodb(ump, ip->i_db[bn]);
148                 if (*bnp == 0) {
149                         *bnp = -1;
150                 } else if (runp) {
151                         int32_t bnb = bn;
152                         for (++bn; bn < NDADDR && *runp < maxrun &&
153                             is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
154                             ++bn, ++*runp);
155                         bn = bnb;
156                         if (runb && (bn > 0)) {
157                                 for (--bn; (bn >= 0) && (*runb < maxrun) &&
158                                         is_sequential(ump, ip->i_db[bn],
159                                                 ip->i_db[bn+1]);
160                                                 --bn, ++*runb);
161                         }
162                 }
163                 return (0);
164         }
165
166
167         /* Get disk address out of indirect block array */
168         daddr = ip->i_ib[ap->in_off];
169
170         for (bp = NULL, ++ap; --num; ++ap) {
171                 /*
172                  * Exit the loop if there is no disk address assigned yet and
173                  * the indirect block isn't in the cache, or if we were
174                  * looking for an indirect block and we've found it.
175                  */
176
177                 metalbn = ap->in_lbn;
178                 if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn)
179                         break;
180                 /*
181                  * If we get here, we've either got the block in the cache
182                  * or we have a disk address for it, go fetch it.
183                  */
184                 if (bp)
185                         bqrelse(bp);
186
187                 ap->in_exists = 1;
188                 bp = getblk(vp, metalbn, bsize, 0, 0, 0);
189                 if ((bp->b_flags & B_CACHE) == 0) {
190 #ifdef DIAGNOSTIC
191                         if (!daddr)
192                                 panic("ufs_bmaparray: indirect block not in cache");
193 #endif
194                         bp->b_blkno = blkptrtodb(ump, daddr);
195                         bp->b_iocmd = BIO_READ;
196                         bp->b_flags &= ~B_INVAL;
197                         bp->b_ioflags &= ~BIO_ERROR;
198                         vfs_busy_pages(bp, 0);
199                         bp->b_iooffset = dbtob(bp->b_blkno);
200                         bstrategy(bp);
201                         curthread->td_ru.ru_inblock++;
202                         error = bufwait(bp);
203                         if (error) {
204                                 brelse(bp);
205                                 return (error);
206                         }
207                 }
208
209                 daddr = ((int32_t *)bp->b_data)[ap->in_off];
210                 if (num == 1 && daddr && runp) {
211                         for (bn = ap->in_off + 1;
212                             bn < MNINDIR(ump) && *runp < maxrun &&
213                             is_sequential(ump,
214                             ((int32_t *)bp->b_data)[bn - 1],
215                             ((int32_t *)bp->b_data)[bn]);
216                             ++bn, ++*runp);
217                         bn = ap->in_off;
218                         if (runb && bn) {
219                                 for(--bn; bn >= 0 && *runb < maxrun &&
220                                         is_sequential(ump, ((int32_t *)bp->b_data)[bn],
221                                             ((int32_t *)bp->b_data)[bn+1]);
222                                         --bn, ++*runb);
223                         }
224                 }
225         }
226         if (bp)
227                 bqrelse(bp);
228
229         /*
230          * Since this is FFS independent code, we are out of scope for the
231          * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
232          * will fall in the range 1..um_seqinc, so we use that test and
233          * return a request for a zeroed out buffer if attempts are made
234          * to read a BLK_NOCOPY or BLK_SNAP block.
235          */
236         if ((ip->i_flags & SF_SNAPSHOT) && daddr > 0 && daddr < ump->um_seqinc){
237                 *bnp = -1;
238                 return (0);
239         }
240         *bnp = blkptrtodb(ump, daddr);
241         if (*bnp == 0) {
242                 *bnp = -1;
243         }
244         return (0);
245 }
246
247 /*
248  * Create an array of logical block number/offset pairs which represent the
249  * path of indirect blocks required to access a data block.  The first "pair"
250  * contains the logical block number of the appropriate single, double or
251  * triple indirect block and the offset into the inode indirect block array.
252  * Note, the logical block number of the inode single/double/triple indirect
253  * block appears twice in the array, once with the offset into the i_ib and
254  * once with the offset into the page itself.
255  */
256 int
257 ext2_getlbns(vp, bn, ap, nump)
258         struct vnode *vp;
259         int32_t bn;
260         struct indir *ap;
261         int *nump;
262 {
263         long blockcnt, metalbn, realbn;
264         struct ext2mount *ump;
265         int i, numlevels, off;
266         int64_t qblockcnt;
267
268         ump = VFSTOEXT2(vp->v_mount);
269         if (nump)
270                 *nump = 0;
271         numlevels = 0;
272         realbn = bn;
273         if ((long)bn < 0)
274                 bn = -(long)bn;
275
276         /* The first NDADDR blocks are direct blocks. */
277         if (bn < NDADDR)
278                 return (0);
279
280         /*
281          * Determine the number of levels of indirection.  After this loop
282          * is done, blockcnt indicates the number of data blocks possible
283          * at the previous level of indirection, and NIADDR - i is the number
284          * of levels of indirection needed to locate the requested block.
285          */
286         for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
287                 if (i == 0)
288                         return (EFBIG);
289                 /*
290                  * Use int64_t's here to avoid overflow for triple indirect
291                  * blocks when longs have 32 bits and the block size is more
292                  * than 4K.
293                  */
294                 qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
295                 if (bn < qblockcnt)
296                         break;
297                 blockcnt = qblockcnt;
298         }
299
300         /* Calculate the address of the first meta-block. */
301         if (realbn >= 0)
302                 metalbn = -(realbn - bn + NIADDR - i);
303         else
304                 metalbn = -(-realbn - bn + NIADDR - i);
305
306         /*
307          * At each iteration, off is the offset into the bap array which is
308          * an array of disk addresses at the current level of indirection.
309          * The logical block number and the offset in that block are stored
310          * into the argument array.
311          */
312         ap->in_lbn = metalbn;
313         ap->in_off = off = NIADDR - i;
314         ap->in_exists = 0;
315         ap++;
316         for (++numlevels; i <= NIADDR; i++) {
317                 /* If searching for a meta-data block, quit when found. */
318                 if (metalbn == realbn)
319                         break;
320
321                 off = (bn / blockcnt) % MNINDIR(ump);
322
323                 ++numlevels;
324                 ap->in_lbn = metalbn;
325                 ap->in_off = off;
326                 ap->in_exists = 0;
327                 ++ap;
328
329                 metalbn -= -1 + off * blockcnt;
330                 blockcnt /= MNINDIR(ump);
331         }
332         if (nump)
333                 *nump = numlevels;
334         return (0);
335 }