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