Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / sys / vfs / ufs / ffs_subr.c
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)ffs_subr.c  8.5 (Berkeley) 3/21/95
34  * $FreeBSD: src/sys/ufs/ffs/ffs_subr.c,v 1.25 1999/12/29 04:55:04 peter Exp $
35  */
36
37 #include <sys/param.h>
38
39 #ifndef _KERNEL
40 #include "dinode.h"
41 #include "fs.h"
42 extern void panic(const char *, ...);
43 #else
44 #include "opt_ddb.h"
45
46 #include <sys/systm.h>
47 #include <sys/lock.h>
48 #include <sys/vnode.h>
49 #include <sys/buf.h>
50 #include <sys/ucred.h>
51 #include <sys/mount.h>
52
53 #include <sys/buf2.h>
54
55 #include "quota.h"
56 #include "inode.h"
57 #include "fs.h"
58 #include "ffs_extern.h"
59
60 /*
61  * Return buffer with the contents of block "offset" from the beginning of
62  * vnode "vp".  If "res" is non-zero, fill it in with a pointer to the
63  * remaining space in the vnode.
64  */
65 int
66 ffs_blkatoff(struct vnode *vp, off_t uoffset, char **res, struct buf **bpp)
67 {
68         struct inode *ip;
69         struct fs *fs;
70         struct buf *bp;
71         ufs_daddr_t lbn;
72         int bsize, error;
73
74         ip = VTOI(vp);
75         fs = ip->i_fs;
76         lbn = lblkno(fs, uoffset);
77         bsize = blksize(fs, ip, lbn);
78
79         *bpp = NULL;
80         error = bread(vp, lblktodoff(fs, lbn), bsize, &bp);
81         if (error) {
82                 brelse(bp);
83                 return (error);
84         }
85         if (res)
86                 *res = (char *)bp->b_data + blkoff(fs, uoffset);
87         *bpp = bp;
88         return (0);
89 }
90
91 /*
92  * Return buffer with the contents of block "offset" from the beginning of
93  * vnode "vp".  If "res" is non-zero, fill it in with a pointer to the
94  * remaining space in the vnode.
95  *
96  * This version includes a read-ahead optimization.
97  */
98 int
99 ffs_blkatoff_ra(struct vnode *vp, off_t uoffset, char **res, struct buf **bpp,
100                 int seqcount)
101 {
102         struct inode *ip;
103         struct fs *fs;
104         struct buf *bp;
105         ufs_daddr_t lbn;
106         ufs_daddr_t nextlbn;
107         off_t base_loffset;
108         off_t next_loffset;
109         int bsize, error;
110         int nextbsize;
111
112         ip = VTOI(vp);
113         fs = ip->i_fs;
114         lbn = lblkno(fs, uoffset);
115         base_loffset = lblktodoff(fs, lbn);
116         bsize = blksize(fs, ip, lbn);
117
118         nextlbn = lbn + 1;
119         next_loffset = lblktodoff(fs, nextlbn);
120
121
122         *bpp = NULL;
123
124         if (next_loffset >= ip->i_size) {
125                 /*
126                  * Do not do readahead if this is the last block,
127                  * bsize might represent a fragment.
128                  */
129                 error = bread(vp, base_loffset, bsize, &bp);
130         } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
131                 /*
132                  * Try to cluster if we allowed to.
133                  */
134                 error = cluster_read(vp, (off_t)ip->i_size,
135                                      base_loffset, bsize,
136                                      bsize, seqcount * BKVASIZE, &bp);
137         } else if (seqcount > 1) {
138                 /*
139                  * Faked read ahead
140                  */
141                 nextbsize = blksize(fs, ip, nextlbn);
142                 error = breadn(vp, base_loffset, bsize,
143                                &next_loffset, &nextbsize, 1, &bp);
144         } else {
145                 /*
146                  * Failing all of the above, just read what the
147                  * user asked for. Interestingly, the same as
148                  * the first option above.
149                  */
150                 error = bread(vp, base_loffset, bsize, &bp);
151         }
152         if (error) {
153                 brelse(bp);
154                 return (error);
155         }
156         if (res)
157                 *res = (char *)bp->b_data + (int)(uoffset - base_loffset);
158         *bpp = bp;
159         return (0);
160 }
161
162 #endif
163
164 /*
165  * Update the frsum fields to reflect addition or deletion
166  * of some frags.
167  */
168 void
169 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
170 {
171         int inblk;
172         int field, subfield;
173         int siz, pos;
174
175         /*
176          * inblk represents a bitmap of fragment sizes which may be
177          * contained in the data 'fragmap'.  e.g. if a fragment of size
178          * 1 is available, bit 0 would be set.  inblk is shifted left
179          * by one so we do not have to calculate (1 << (siz - 1)).
180          *
181          * fragment represents the data pattern we are trying to decipher,
182          * we shift it left by one to align it with the 'around' and 'inside'
183          * masks.
184          *
185          * around represents the bits around the subfield and is a mask.
186          * inside represents what we must match within the mask, it is
187          * basically the mask with the first and last bit set to 0, allowing
188          * us to represent a whole fragment.
189          *
190          * When we find a match we bump our position by the size of the
191          * matching fragment, then bump the position again:
192          *
193          * 010101010 fragmap (shifted left by 1)
194          *       111 around mask
195          *       010 inside mask
196          *      111     (shifted by siz)
197          *      010
198          *     111      (shifted again)
199          *     010
200          */
201         inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
202         fragmap <<= 1;
203         for (siz = 1; siz < fs->fs_frag; siz++) {
204                 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
205                         continue;
206                 field = around[siz];
207                 subfield = inside[siz];
208                 for (pos = siz; pos <= fs->fs_frag; pos++) {
209                         if ((fragmap & field) == subfield) {
210                                 fraglist[siz] += cnt;
211                                 pos += siz;
212                                 field <<= siz;
213                                 subfield <<= siz;
214                         }
215                         field <<= 1;
216                         subfield <<= 1;
217                 }
218         }
219 }
220
221 /*
222  * block operations
223  *
224  * check if a block is available
225  */
226 int
227 ffs_isblock(struct fs *fs, unsigned char *cp, ufs_daddr_t h)
228 {
229         unsigned char mask;
230
231         switch ((int)fs->fs_frag) {
232         case 8:
233                 return (cp[h] == 0xff);
234         case 4:
235                 mask = 0x0f << ((h & 0x1) << 2);
236                 return ((cp[h >> 1] & mask) == mask);
237         case 2:
238                 mask = 0x03 << ((h & 0x3) << 1);
239                 return ((cp[h >> 2] & mask) == mask);
240         case 1:
241                 mask = 0x01 << (h & 0x7);
242                 return ((cp[h >> 3] & mask) == mask);
243         default:
244                 panic("ffs_isblock");
245         }
246 }
247
248 /*
249  * check if a block is free
250  */
251 int
252 ffs_isfreeblock(struct fs *fs, unsigned char *cp, ufs_daddr_t h)
253 {
254         switch ((int)fs->fs_frag) {
255         case 8:
256                 return (cp[h] == 0);
257         case 4:
258                 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
259         case 2:
260                 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
261         case 1:
262                 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
263         default:
264                 panic("ffs_isfreeblock");
265         }
266 }
267
268 /*
269  * take a block out of the map
270  */
271 void
272 ffs_clrblock(struct fs *fs, u_char *cp, ufs_daddr_t h)
273 {
274         switch ((int)fs->fs_frag) {
275         case 8:
276                 cp[h] = 0;
277                 return;
278         case 4:
279                 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
280                 return;
281         case 2:
282                 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
283                 return;
284         case 1:
285                 cp[h >> 3] &= ~(0x01 << (h & 0x7));
286                 return;
287         default:
288                 panic("ffs_clrblock");
289         }
290 }
291
292 /*
293  * put a block into the map
294  */
295 void
296 ffs_setblock(struct fs *fs, unsigned char *cp, ufs_daddr_t h)
297 {
298         switch ((int)fs->fs_frag) {
299         case 8:
300                 cp[h] = 0xff;
301                 return;
302         case 4:
303                 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
304                 return;
305         case 2:
306                 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
307                 return;
308         case 1:
309                 cp[h >> 3] |= (0x01 << (h & 0x7));
310                 return;
311         default:
312                 panic("ffs_setblock");
313         }
314 }