Correct BSD License clause numbering from 1-2-4 to 1-2-3.
[dragonfly.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. 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
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
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[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 = &num;
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 < 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                 xap->in_exists = 1;
196                 bp = getblk(vp, lblktodoff(fs, metalbn),
197                             mp->mnt_stat.f_iosize, 0, 0);
198                 if ((bp->b_flags & B_CACHE) == 0) {
199 #ifdef DIAGNOSTIC
200                         if (!daddr)
201                                 panic("ufs_bmaparray: indirect block not in cache");
202 #endif
203                         /*
204                          * cached disk addr in bio2, do I/O on bio1.  It
205                          * will probably hit the vfs's strategy function
206                          * which will then use the cached offset in bio2.
207                          */
208                         bp->b_bio1.bio_done = biodone_sync;
209                         bp->b_bio1.bio_flags |= BIO_SYNC;
210                         bp->b_bio2.bio_offset = fsbtodoff(fs, daddr);
211                         bp->b_flags &= ~(B_INVAL|B_ERROR);
212                         bp->b_cmd = BUF_CMD_READ;
213                         vfs_busy_pages(bp->b_vp, bp);
214                         vn_strategy(bp->b_vp, &bp->b_bio1);
215                         error = biowait(&bp->b_bio1, "biord");
216                         if (error) {
217                                 brelse(bp);
218                                 return (error);
219                         }
220                 }
221
222                 daddr = ((ufs_daddr_t *)bp->b_data)[xap->in_off];
223                 if (num == 1 && daddr && runp) {
224                         for (bn = xap->in_off + 1;
225                             bn < MNINDIR(ump) && *runp < maxrun &&
226                             is_sequential(ump,
227                             ((ufs_daddr_t *)bp->b_data)[bn - 1],
228                             ((ufs_daddr_t *)bp->b_data)[bn]);
229                             ++bn, ++*runp);
230                         bn = xap->in_off;
231                         if (runb && bn) {
232                                 for(--bn; bn >= 0 && *runb < maxrun &&
233                                         is_sequential(ump, ((daddr_t *)bp->b_data)[bn],
234                                             ((daddr_t *)bp->b_data)[bn+1]);
235                                         --bn, ++*runb);
236                         }
237                 }
238         }
239         if (bp)
240                 bqrelse(bp);
241
242         daddr = blkptrtodb(ump, daddr);
243         *bnp = daddr == 0 ? -1 : daddr;
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 ufs_getlbns(struct vnode *vp, ufs_daddr_t bn, struct indir *ap, int *nump)
258 {
259         long blockcnt, metalbn, realbn;
260         struct ufsmount *ump;
261         int i, numlevels, off;
262         int64_t qblockcnt;
263
264         ump = VFSTOUFS(vp->v_mount);
265         if (nump)
266                 *nump = 0;
267         numlevels = 0;
268         realbn = bn;
269         if ((long)bn < 0)
270                 bn = -(long)bn;
271
272         /* The first NDADDR blocks are direct blocks. */
273         if (bn < NDADDR)
274                 return (0);
275
276         /*
277          * Determine the number of levels of indirection.  After this loop
278          * is done, blockcnt indicates the number of data blocks possible
279          * at the previous level of indirection, and NIADDR - i is the number
280          * of levels of indirection needed to locate the requested block.
281          */
282         for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
283                 if (i == 0)
284                         return (EFBIG);
285                 /*
286                  * Use int64_t's here to avoid overflow for triple indirect
287                  * blocks when longs have 32 bits and the block size is more
288                  * than 4K.
289                  */
290                 qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
291                 if (bn < qblockcnt)
292                         break;
293                 blockcnt = qblockcnt;
294         }
295
296         /* Calculate the address of the first meta-block. */
297         if (realbn >= 0)
298                 metalbn = -(realbn - bn + NIADDR - i);
299         else
300                 metalbn = -(-realbn - bn + NIADDR - i);
301
302         /*
303          * At each iteration, off is the offset into the bap array which is
304          * an array of disk addresses at the current level of indirection.
305          * The logical block number and the offset in that block are stored
306          * into the argument array.
307          */
308         ap->in_lbn = metalbn;
309         ap->in_off = off = NIADDR - i;
310         ap->in_exists = 0;
311         ap++;
312         for (++numlevels; i <= NIADDR; i++) {
313                 /* If searching for a meta-data block, quit when found. */
314                 if (metalbn == realbn)
315                         break;
316
317                 off = (bn / blockcnt) % MNINDIR(ump);
318
319                 ++numlevels;
320                 ap->in_lbn = metalbn;
321                 ap->in_off = off;
322                 ap->in_exists = 0;
323                 ++ap;
324
325                 metalbn -= -1 + off * blockcnt;
326                 blockcnt /= MNINDIR(ump);
327         }
328         if (nump)
329                 *nump = numlevels;
330         return (0);
331 }