Initial import from FreeBSD RELENG_4:
[games.git] / sys / vfs / ufs / ufs_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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. 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.7 (Berkeley) 3/21/95
39  * $FreeBSD: src/sys/ufs/ufs/ufs_bmap.c,v 1.34.2.1 2000/03/17 10:12:14 ps Exp $
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/conf.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
56 /*
57  * Bmap converts a the logical block number of a file to its physical block
58  * number on the disk. The conversion is done by using the logical block
59  * number to index into the array of block pointers described by the dinode.
60  */
61 int
62 ufs_bmap(ap)
63         struct vop_bmap_args /* {
64                 struct vnode *a_vp;
65                 ufs_daddr_t a_bn;
66                 struct vnode **a_vpp;
67                 ufs_daddr_t *a_bnp;
68                 int *a_runp;
69                 int *a_runb;
70         } */ *ap;
71 {
72         /*
73          * Check for underlying vnode requests and ensure that logical
74          * to physical mapping is requested.
75          */
76         if (ap->a_vpp != NULL)
77                 *ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
78         if (ap->a_bnp == NULL)
79                 return (0);
80
81         return (ufs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL,
82             ap->a_runp, ap->a_runb));
83 }
84
85 /*
86  * Indirect blocks are now on the vnode for the file.  They are given negative
87  * logical block numbers.  Indirect blocks are addressed by the negative
88  * address of the first data block to which they point.  Double indirect blocks
89  * are addressed by one less than the address of the first indirect block to
90  * which they point.  Triple indirect blocks are addressed by one less than
91  * the address of the first double indirect block to which they point.
92  *
93  * ufs_bmaparray does the bmap conversion, and if requested returns the
94  * array of logical blocks which must be traversed to get to a block.
95  * Each entry contains the offset into that block that gets you to the
96  * next block and the disk address of the block (if it is assigned).
97  */
98
99 int
100 ufs_bmaparray(vp, bn, bnp, ap, nump, runp, runb)
101         struct vnode *vp;
102         ufs_daddr_t bn;
103         ufs_daddr_t *bnp;
104         struct indir *ap;
105         int *nump;
106         int *runp;
107         int *runb;
108 {
109         register struct inode *ip;
110         struct buf *bp;
111         struct ufsmount *ump;
112         struct mount *mp;
113         struct vnode *devvp;
114         struct indir a[NIADDR+1], *xap;
115         ufs_daddr_t daddr;
116         long metalbn;
117         int error, maxrun, num;
118
119         ip = VTOI(vp);
120         mp = vp->v_mount;
121         ump = VFSTOUFS(mp);
122         devvp = ump->um_devvp;
123 #ifdef DIAGNOSTIC
124         if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
125                 panic("ufs_bmaparray: invalid arguments");
126 #endif
127
128         if (runp) {
129                 *runp = 0;
130         }
131
132         if (runb) {
133                 *runb = 0;
134         }
135
136         maxrun = mp->mnt_iosize_max / mp->mnt_stat.f_iosize - 1;
137
138         xap = ap == NULL ? a : ap;
139         if (!nump)
140                 nump = &num;
141         error = ufs_getlbns(vp, bn, xap, 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                         daddr_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[xap->in_off];
169
170         for (bp = NULL, ++xap; --num; ++xap) {
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 = xap->in_lbn;
178                 if ((daddr == 0 && !incore(vp, 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                 xap->in_exists = 1;
188                 bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 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_flags |= B_READ;
196                         bp->b_flags &= ~(B_INVAL|B_ERROR);
197                         vfs_busy_pages(bp, 0);
198                         VOP_STRATEGY(bp->b_vp, bp);
199                         curproc->p_stats->p_ru.ru_inblock++;    /* XXX */
200                         error = biowait(bp);
201                         if (error) {
202                                 brelse(bp);
203                                 return (error);
204                         }
205                 }
206
207                 daddr = ((ufs_daddr_t *)bp->b_data)[xap->in_off];
208                 if (num == 1 && daddr && runp) {
209                         for (bn = xap->in_off + 1;
210                             bn < MNINDIR(ump) && *runp < maxrun &&
211                             is_sequential(ump,
212                             ((ufs_daddr_t *)bp->b_data)[bn - 1],
213                             ((ufs_daddr_t *)bp->b_data)[bn]);
214                             ++bn, ++*runp);
215                         bn = xap->in_off;
216                         if (runb && bn) {
217                                 for(--bn; bn >= 0 && *runb < maxrun &&
218                                         is_sequential(ump, ((daddr_t *)bp->b_data)[bn],
219                                             ((daddr_t *)bp->b_data)[bn+1]);
220                                         --bn, ++*runb);
221                         }
222                 }
223         }
224         if (bp)
225                 bqrelse(bp);
226
227         daddr = blkptrtodb(ump, daddr);
228         *bnp = daddr == 0 ? -1 : daddr;
229         return (0);
230 }
231
232 /*
233  * Create an array of logical block number/offset pairs which represent the
234  * path of indirect blocks required to access a data block.  The first "pair"
235  * contains the logical block number of the appropriate single, double or
236  * triple indirect block and the offset into the inode indirect block array.
237  * Note, the logical block number of the inode single/double/triple indirect
238  * block appears twice in the array, once with the offset into the i_ib and
239  * once with the offset into the page itself.
240  */
241 int
242 ufs_getlbns(vp, bn, ap, nump)
243         struct vnode *vp;
244         ufs_daddr_t bn;
245         struct indir *ap;
246         int *nump;
247 {
248         long blockcnt, metalbn, realbn;
249         struct ufsmount *ump;
250         int i, numlevels, off;
251         int64_t qblockcnt;
252
253         ump = VFSTOUFS(vp->v_mount);
254         if (nump)
255                 *nump = 0;
256         numlevels = 0;
257         realbn = bn;
258         if ((long)bn < 0)
259                 bn = -(long)bn;
260
261         /* The first NDADDR blocks are direct blocks. */
262         if (bn < NDADDR)
263                 return (0);
264
265         /*
266          * Determine the number of levels of indirection.  After this loop
267          * is done, blockcnt indicates the number of data blocks possible
268          * at the previous level of indirection, and NIADDR - i is the number
269          * of levels of indirection needed to locate the requested block.
270          */
271         for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
272                 if (i == 0)
273                         return (EFBIG);
274                 /*
275                  * Use int64_t's here to avoid overflow for triple indirect
276                  * blocks when longs have 32 bits and the block size is more
277                  * than 4K.
278                  */
279                 qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
280                 if (bn < qblockcnt)
281                         break;
282                 blockcnt = qblockcnt;
283         }
284
285         /* Calculate the address of the first meta-block. */
286         if (realbn >= 0)
287                 metalbn = -(realbn - bn + NIADDR - i);
288         else
289                 metalbn = -(-realbn - bn + NIADDR - i);
290
291         /*
292          * At each iteration, off is the offset into the bap array which is
293          * an array of disk addresses at the current level of indirection.
294          * The logical block number and the offset in that block are stored
295          * into the argument array.
296          */
297         ap->in_lbn = metalbn;
298         ap->in_off = off = NIADDR - i;
299         ap->in_exists = 0;
300         ap++;
301         for (++numlevels; i <= NIADDR; i++) {
302                 /* If searching for a meta-data block, quit when found. */
303                 if (metalbn == realbn)
304                         break;
305
306                 off = (bn / blockcnt) % MNINDIR(ump);
307
308                 ++numlevels;
309                 ap->in_lbn = metalbn;
310                 ap->in_off = off;
311                 ap->in_exists = 0;
312                 ++ap;
313
314                 metalbn -= -1 + off * blockcnt;
315                 blockcnt /= MNINDIR(ump);
316         }
317         if (nump)
318                 *nump = numlevels;
319         return (0);
320 }