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