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