cb98a75475d44cd9577d27cf7316949b8ab8ca5d
[dragonfly.git] / sys / vfs / gnu / 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  * 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  * $DragonFly: src/sys/vfs/gnu/ext2fs/ext2_bmap.c,v 1.4 2007/08/13 17:31:56 dillon Exp $
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/buf.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <sys/resourcevar.h>
50 #include <sys/conf.h>
51
52 #include "quota.h"
53 #include "dinode.h"
54 #include "inode.h"
55 #include "ext2_fs_sb.h"
56 #include "ext2mount.h"
57 #include "ext2_extern.h"
58 #include "fs.h"
59
60 static int ext2_bmaparray(struct vnode *vp, ext2_daddr_t bn,
61                           ext2_daddr_t *bnp, struct indir *ap, int *nump,
62                           int *runp, int *runb);
63
64 /*
65  * Bmap converts the logical block number of a file to its physical block
66  * number on the disk. The conversion is done by using the logical block
67  * number to index into the array of block pointers described by the dinode.
68  *
69  * BMAP must return the contiguous before and after run in bytes, inclusive
70  * of the returned block.
71  *
72  * ext2_bmap(struct vnode *a_vp, off_t a_loffset,
73  *          off_t *a_doffsetp, int *a_runp, int *a_runb)
74  */
75 int
76 ext2_bmap(struct vop_bmap_args *ap)
77 {
78         struct ext2_sb_info *fs;
79         ext2_daddr_t lbn;
80         ext2_daddr_t dbn;
81         int error;
82
83         /*
84          * Check for underlying vnode requests and ensure that logical
85          * to physical mapping is requested.
86          */
87         if (ap->a_doffsetp == NULL)
88                 return (0);
89
90         fs = VTOI(ap->a_vp)->i_e2fs;
91         KKASSERT(((int)ap->a_loffset & ((1 << fs->s_bshift) - 1)) == 0);
92         lbn = ap->a_loffset >> fs->s_bshift;
93
94         error = ext2_bmaparray(ap->a_vp, lbn, &dbn, NULL, NULL,
95                               ap->a_runp, ap->a_runb);
96
97         if (error || dbn == (ext2_daddr_t)-1) {
98                 *ap->a_doffsetp = NOOFFSET;
99         } else {
100                 *ap->a_doffsetp = dbtodoff(fs, dbn);
101                 if (ap->a_runp)
102                         *ap->a_runp = (*ap->a_runp + 1) << fs->s_bshift;
103                 if (ap->a_runb)
104                         *ap->a_runb = *ap->a_runb << fs->s_bshift;
105         }
106         return (error);
107 }
108
109 /*
110  * Indirect blocks are now on the vnode for the file.  They are given negative
111  * logical block numbers.  Indirect blocks are addressed by the negative
112  * address of the first data block to which they point.  Double indirect blocks
113  * are addressed by one less than the address of the first indirect block to
114  * which they point.  Triple indirect blocks are addressed by one less than
115  * the address of the first double indirect block to which they point.
116  *
117  * ext2_bmaparray does the bmap conversion, and if requested returns the
118  * array of logical blocks which must be traversed to get to a block.
119  * Each entry contains the offset into that block that gets you to the
120  * next block and the disk address of the block (if it is assigned).
121  */
122 static
123 int
124 ext2_bmaparray(struct vnode *vp, ext2_daddr_t bn, ext2_daddr_t *bnp,
125               struct indir *ap, int *nump, int *runp, int *runb)
126 {
127         struct inode *ip;
128         struct buf *bp;
129         struct ext2mount *ump;
130         struct mount *mp;
131         struct ext2_sb_info *fs;
132         struct indir a[NIADDR+1], *xap;
133         ext2_daddr_t daddr;
134         long metalbn;
135         int error, maxrun, num;
136
137         ip = VTOI(vp);
138         mp = vp->v_mount;
139         ump = VFSTOEXT2(mp);
140         fs = ip->i_e2fs;
141 #ifdef DIAGNOSTIC
142         if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
143                 panic("ext2_bmaparray: invalid arguments");
144 #endif
145
146         if (runp) {
147                 *runp = 0;
148         }
149
150         if (runb) {
151                 *runb = 0;
152         }
153
154         maxrun = mp->mnt_iosize_max / mp->mnt_stat.f_iosize - 1;
155
156         xap = ap == NULL ? a : ap;
157         if (!nump)
158                 nump = &num;
159         error = ext2_getlbns(vp, bn, xap, nump);
160         if (error)
161                 return (error);
162
163         num = *nump;
164         if (num == 0) {
165                 *bnp = blkptrtodb(ump, ip->i_db[bn]);
166                 if (*bnp == 0)
167                         *bnp = -1;
168                 else if (runp) {
169                         daddr_t bnb = bn;
170                         for (++bn; bn < NDADDR && *runp < maxrun &&
171                             is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
172                             ++bn, ++*runp);
173                         bn = bnb;
174                         if (runb && (bn > 0)) {
175                                 for (--bn; (bn >= 0) && (*runb < maxrun) &&
176                                         is_sequential(ump, ip->i_db[bn],
177                                                 ip->i_db[bn+1]);
178                                                 --bn, ++*runb);
179                         }
180                 }
181                 return (0);
182         }
183
184
185         /* Get disk address out of indirect block array */
186         daddr = ip->i_ib[xap->in_off];
187
188         for (bp = NULL, ++xap; --num; ++xap) {
189                 /*
190                  * Exit the loop if there is no disk address assigned yet and
191                  * the indirect block isn't in the cache, or if we were
192                  * looking for an indirect block and we've found it.
193                  */
194
195                 metalbn = xap->in_lbn;
196                 if ((daddr == 0 &&
197                      !findblk(vp, dbtodoff(fs, metalbn), FINDBLK_TEST)) ||
198                     metalbn == bn) {
199                         break;
200                 }
201                 /*
202                  * If we get here, we've either got the block in the cache
203                  * or we have a disk address for it, go fetch it.
204                  */
205                 if (bp)
206                         bqrelse(bp);
207
208                 xap->in_exists = 1;
209                 bp = getblk(vp, lblktodoff(fs, metalbn),
210                             mp->mnt_stat.f_iosize, 0, 0);
211                 if ((bp->b_flags & B_CACHE) == 0) {
212 #ifdef DIAGNOSTIC
213                         if (!daddr)
214                                 panic("ext2_bmaparray: indirect block not in cache");
215 #endif
216                         /*
217                          * This runs through ext2_strategy using bio2 to
218                          * cache the disk offset, then comes back through
219                          * bio1.  So we want to wait on bio1
220                          */
221                         bp->b_bio1.bio_done = biodone_sync;
222                         bp->b_bio1.bio_flags |= BIO_SYNC;
223                         bp->b_bio2.bio_offset = fsbtodoff(fs, daddr);
224                         bp->b_flags &= ~(B_INVAL|B_ERROR);
225                         bp->b_cmd = BUF_CMD_READ;
226                         vfs_busy_pages(bp->b_vp, bp);
227                         vn_strategy(bp->b_vp, &bp->b_bio1);
228                         error = biowait(&bp->b_bio1, "biord");
229                         if (error) {
230                                 brelse(bp);
231                                 return (error);
232                         }
233                 }
234
235                 daddr = ((ext2_daddr_t *)bp->b_data)[xap->in_off];
236                 if (num == 1 && daddr && runp) {
237                         for (bn = xap->in_off + 1;
238                             bn < MNINDIR(ump) && *runp < maxrun &&
239                             is_sequential(ump,
240                             ((ext2_daddr_t *)bp->b_data)[bn - 1],
241                             ((ext2_daddr_t *)bp->b_data)[bn]);
242                             ++bn, ++*runp);
243                         bn = xap->in_off;
244                         if (runb && bn) {
245                                 for(--bn; bn >= 0 && *runb < maxrun &&
246                                         is_sequential(ump, ((daddr_t *)bp->b_data)[bn],
247                                             ((daddr_t *)bp->b_data)[bn+1]);
248                                         --bn, ++*runb);
249                         }
250                 }
251         }
252         if (bp)
253                 bqrelse(bp);
254
255         daddr = blkptrtodb(ump, daddr);
256         *bnp = daddr == 0 ? -1 : daddr;
257         return (0);
258 }
259
260 /*
261  * Create an array of logical block number/offset pairs which represent the
262  * path of indirect blocks required to access a data block.  The first "pair"
263  * contains the logical block number of the appropriate single, double or
264  * triple indirect block and the offset into the inode indirect block array.
265  * Note, the logical block number of the inode single/double/triple indirect
266  * block appears twice in the array, once with the offset into the i_ib and
267  * once with the offset into the page itself.
268  */
269 int
270 ext2_getlbns(struct vnode *vp, ext2_daddr_t bn, struct indir *ap, int *nump)
271 {
272         long blockcnt, metalbn, realbn;
273         struct ext2mount *ump;
274         int i, numlevels, off;
275         int64_t qblockcnt;
276
277         ump = VFSTOEXT2(vp->v_mount);
278         if (nump)
279                 *nump = 0;
280         numlevels = 0;
281         realbn = bn;
282         if ((long)bn < 0)
283                 bn = -(long)bn;
284
285         /* The first NDADDR blocks are direct blocks. */
286         if (bn < NDADDR)
287                 return (0);
288
289         /*
290          * Determine the number of levels of indirection.  After this loop
291          * is done, blockcnt indicates the number of data blocks possible
292          * at the previous level of indirection, and NIADDR - i is the number
293          * of levels of indirection needed to locate the requested block.
294          */
295         for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
296                 if (i == 0)
297                         return (EFBIG);
298                 /*
299                  * Use int64_t's here to avoid overflow for triple indirect
300                  * blocks when longs have 32 bits and the block size is more
301                  * than 4K.
302                  */
303                 qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
304                 if (bn < qblockcnt)
305                         break;
306                 blockcnt = qblockcnt;
307         }
308
309         /* Calculate the address of the first meta-block. */
310         if (realbn >= 0)
311                 metalbn = -(realbn - bn + NIADDR - i);
312         else
313                 metalbn = -(-realbn - bn + NIADDR - i);
314
315         /*
316          * At each iteration, off is the offset into the bap array which is
317          * an array of disk addresses at the current level of indirection.
318          * The logical block number and the offset in that block are stored
319          * into the argument array.
320          */
321         ap->in_lbn = metalbn;
322         ap->in_off = off = NIADDR - i;
323         ap->in_exists = 0;
324         ap++;
325         for (++numlevels; i <= NIADDR; i++) {
326                 /* If searching for a meta-data block, quit when found. */
327                 if (metalbn == realbn)
328                         break;
329
330                 off = (bn / blockcnt) % MNINDIR(ump);
331
332                 ++numlevels;
333                 ap->in_lbn = metalbn;
334                 ap->in_off = off;
335                 ap->in_exists = 0;
336                 ++ap;
337
338                 metalbn -= -1 + off * blockcnt;
339                 blockcnt /= MNINDIR(ump);
340         }
341         if (nump)
342                 *nump = numlevels;
343         return (0);
344 }