Due to continuing issues with VOP_READ/VOP_WRITE ops being called without
[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  * $DragonFly: src/sys/vfs/ufs/ffs_subr.c,v 1.11 2006/04/03 02:02:37 dillon Exp $
36  */
37
38 #include <sys/param.h>
39
40 #ifndef _KERNEL
41 #include "dinode.h"
42 #include "fs.h"
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
52 #include "quota.h"
53 #include "inode.h"
54 #include "fs.h"
55 #include "ffs_extern.h"
56
57 #ifdef DDB
58 void    ffs_checkoverlap (struct buf *, struct inode *);
59 #endif
60
61 /*
62  * Return buffer with the contents of block "offset" from the beginning of
63  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
64  * remaining space in the directory.
65  */
66 int
67 ffs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp)
68 {
69         struct inode *ip;
70         struct fs *fs;
71         struct buf *bp;
72         ufs_daddr_t lbn;
73         int bsize, error;
74
75         ip = VTOI(vp);
76         fs = ip->i_fs;
77         lbn = lblkno(fs, offset);
78         bsize = blksize(fs, ip, lbn);
79
80         *bpp = NULL;
81         error = bread(vp, lblktodoff(fs, lbn), bsize, &bp);
82         if (error) {
83                 brelse(bp);
84                 return (error);
85         }
86         if (res)
87                 *res = (char *)bp->b_data + blkoff(fs, offset);
88         *bpp = bp;
89         return (0);
90 }
91 #endif
92
93 /*
94  * Update the frsum fields to reflect addition or deletion
95  * of some frags.
96  */
97 void
98 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
99 {
100         int inblk;
101         int field, subfield;
102         int siz, pos;
103
104         /*
105          * inblk represents a bitmap of fragment sizes which may be
106          * contained in the data 'fragmap'.  e.g. if a fragment of size
107          * 1 is available, bit 0 would be set.  inblk is shifted left
108          * by one so we do not have to calculate (1 << (siz - 1)).
109          *
110          * fragment represents the data pattern we are trying to decipher,
111          * we shift it left by one to align it with the 'around' and 'inside'
112          * masks.
113          *
114          * around represents the bits around the subfield and is a mask.
115          * inside represents what we must match within the mask, it is
116          * basically the mask with the first and last bit set to 0, allowing
117          * us to represent a whole fragment.
118          *
119          * When we find a match we bump our position by the size of the
120          * matching fragment, then bump the position again:
121          *
122          * 010101010 fragmap (shifted left by 1)
123          *       111 around mask
124          *       010 inside mask
125          *      111     (shifted by siz)
126          *      010
127          *     111      (shifted again)
128          *     010
129          */
130         inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
131         fragmap <<= 1;
132         for (siz = 1; siz < fs->fs_frag; siz++) {
133                 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
134                         continue;
135                 field = around[siz];
136                 subfield = inside[siz];
137                 for (pos = siz; pos <= fs->fs_frag; pos++) {
138                         if ((fragmap & field) == subfield) {
139                                 fraglist[siz] += cnt;
140                                 pos += siz;
141                                 field <<= siz;
142                                 subfield <<= siz;
143                         }
144                         field <<= 1;
145                         subfield <<= 1;
146                 }
147         }
148 }
149
150 #ifdef DDB
151 void
152 ffs_checkoverlap(struct buf *bp, struct inode *ip)
153 {
154         struct buf *ebp, *ep;
155         off_t start, last;
156         struct vnode *vp;
157
158         ebp = &buf[nbuf];
159         start = bp->b_bio2.bio_offset;
160         last = start + bp->b_bcount - 1;
161         for (ep = buf; ep < ebp; ep++) {
162                 if (ep == bp || (ep->b_flags & B_INVAL) ||
163                     ep->b_vp == NULLVP)
164                         continue;
165                 if (VOP_BMAP(ep->b_vp, (off_t)0, &vp, NULL, NULL, NULL))
166                         continue;
167                 if (vp != ip->i_devvp)
168                         continue;
169                 /* look for overlap */
170                 if (ep->b_bcount == 0 || ep->b_bio2.bio_offset == NOOFFSET ||
171                     ep->b_bio2.bio_offset > last ||
172                     ep->b_bio2.bio_offset + ep->b_bcount <= start)
173                         continue;
174                 vprint("Disk overlap", vp);
175                 printf("\tstart %lld, end %lld overlap start %lld, end %lld\n",
176                         start, last, 
177                         ep->b_bio2.bio_offset,
178                         ep->b_bio2.bio_offset + ep->b_bcount - 1);
179                 panic("ffs_checkoverlap: Disk buffer overlap");
180         }
181 }
182 #endif /* DDB */
183
184 /*
185  * block operations
186  *
187  * check if a block is available
188  */
189 int
190 ffs_isblock(struct fs *fs, unsigned char *cp, ufs_daddr_t h)
191 {
192         unsigned char mask;
193
194         switch ((int)fs->fs_frag) {
195         case 8:
196                 return (cp[h] == 0xff);
197         case 4:
198                 mask = 0x0f << ((h & 0x1) << 2);
199                 return ((cp[h >> 1] & mask) == mask);
200         case 2:
201                 mask = 0x03 << ((h & 0x3) << 1);
202                 return ((cp[h >> 2] & mask) == mask);
203         case 1:
204                 mask = 0x01 << (h & 0x7);
205                 return ((cp[h >> 3] & mask) == mask);
206         default:
207                 panic("ffs_isblock");
208         }
209 }
210
211 /*
212  * check if a block is free
213  */
214 int
215 ffs_isfreeblock(struct fs *fs, unsigned char *cp, ufs_daddr_t h)
216 {
217         switch ((int)fs->fs_frag) {
218         case 8:
219                 return (cp[h] == 0);
220         case 4:
221                 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
222         case 2:
223                 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
224         case 1:
225                 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
226         default:
227                 panic("ffs_isfreeblock");
228         }
229 }
230
231 /*
232  * take a block out of the map
233  */
234 void
235 ffs_clrblock(struct fs *fs, u_char *cp, ufs_daddr_t h)
236 {
237         switch ((int)fs->fs_frag) {
238         case 8:
239                 cp[h] = 0;
240                 return;
241         case 4:
242                 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
243                 return;
244         case 2:
245                 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
246                 return;
247         case 1:
248                 cp[h >> 3] &= ~(0x01 << (h & 0x7));
249                 return;
250         default:
251                 panic("ffs_clrblock");
252         }
253 }
254
255 /*
256  * put a block into the map
257  */
258 void
259 ffs_setblock(struct fs *fs, unsigned char *cp, ufs_daddr_t h)
260 {
261         switch ((int)fs->fs_frag) {
262         case 8:
263                 cp[h] = 0xff;
264                 return;
265         case 4:
266                 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
267                 return;
268         case 2:
269                 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
270                 return;
271         case 1:
272                 cp[h >> 3] |= (0x01 << (h & 0x7));
273                 return;
274         default:
275                 panic("ffs_setblock");
276         }
277 }