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