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