Remove now unnecessary messing with PCI command register.
[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.7 2004/05/18 00:16:46 cpressey Exp $
36  */
37
38 #include <sys/param.h>
39 #include "fs.h"
40
41 #ifndef _KERNEL
42 #include "dinode.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 "ffs_extern.h"
55
56 #ifdef DDB
57 void    ffs_checkoverlap (struct buf *, struct inode *);
58 #endif
59
60 /*
61  * Return buffer with the contents of block "offset" from the beginning of
62  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
63  * remaining space in the directory.
64  */
65 int
66 ffs_blkatoff(struct vnode *vp, off_t offset, 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, offset);
77         bsize = blksize(fs, ip, lbn);
78
79         *bpp = NULL;
80         error = bread(vp, lbn, bsize, &bp);
81         if (error) {
82                 brelse(bp);
83                 return (error);
84         }
85         if (res)
86                 *res = (char *)bp->b_data + blkoff(fs, offset);
87         *bpp = bp;
88         return (0);
89 }
90 #endif
91
92 /*
93  * Update the frsum fields to reflect addition or deletion
94  * of some frags.
95  */
96 void
97 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
98 {
99         int inblk;
100         int field, subfield;
101         int siz, pos;
102
103         inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
104         fragmap <<= 1;
105         for (siz = 1; siz < fs->fs_frag; siz++) {
106                 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
107                         continue;
108                 field = around[siz];
109                 subfield = inside[siz];
110                 for (pos = siz; pos <= fs->fs_frag; pos++) {
111                         if ((fragmap & field) == subfield) {
112                                 fraglist[siz] += cnt;
113                                 pos += siz;
114                                 field <<= siz;
115                                 subfield <<= siz;
116                         }
117                         field <<= 1;
118                         subfield <<= 1;
119                 }
120         }
121 }
122
123 #ifdef DDB
124 void
125 ffs_checkoverlap(struct buf *bp, struct inode *ip)
126 {
127         struct buf *ebp, *ep;
128         ufs_daddr_t start, last;
129         struct vnode *vp;
130
131         ebp = &buf[nbuf];
132         start = bp->b_blkno;
133         last = start + btodb(bp->b_bcount) - 1;
134         for (ep = buf; ep < ebp; ep++) {
135                 if (ep == bp || (ep->b_flags & B_INVAL) ||
136                     ep->b_vp == NULLVP)
137                         continue;
138                 if (VOP_BMAP(ep->b_vp, (ufs_daddr_t)0, &vp, (ufs_daddr_t *)NULL,
139                     (int *)NULL, (int *)NULL))
140                         continue;
141                 if (vp != ip->i_devvp)
142                         continue;
143                 /* look for overlap */
144                 if (ep->b_bcount == 0 || ep->b_blkno > last ||
145                     ep->b_blkno + btodb(ep->b_bcount) <= start)
146                         continue;
147                 vprint("Disk overlap", vp);
148                 (void)printf("\tstart %lu, end %lu overlap start %lu, end %lu\n",
149                         (u_long)start, (u_long)last, (u_long)ep->b_blkno,
150                         (u_long)(ep->b_blkno + btodb(ep->b_bcount) - 1));
151                 panic("ffs_checkoverlap: Disk buffer overlap");
152         }
153 }
154 #endif /* DDB */
155
156 /*
157  * block operations
158  *
159  * check if a block is available
160  */
161 int
162 ffs_isblock(struct fs *fs, unsigned char *cp, ufs_daddr_t h)
163 {
164         unsigned char mask;
165
166         switch ((int)fs->fs_frag) {
167         case 8:
168                 return (cp[h] == 0xff);
169         case 4:
170                 mask = 0x0f << ((h & 0x1) << 2);
171                 return ((cp[h >> 1] & mask) == mask);
172         case 2:
173                 mask = 0x03 << ((h & 0x3) << 1);
174                 return ((cp[h >> 2] & mask) == mask);
175         case 1:
176                 mask = 0x01 << (h & 0x7);
177                 return ((cp[h >> 3] & mask) == mask);
178         default:
179                 panic("ffs_isblock");
180         }
181 }
182
183 /*
184  * check if a block is free
185  */
186 int
187 ffs_isfreeblock(struct fs *fs, unsigned char *cp, ufs_daddr_t h)
188 {
189         switch ((int)fs->fs_frag) {
190         case 8:
191                 return (cp[h] == 0);
192         case 4:
193                 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
194         case 2:
195                 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
196         case 1:
197                 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
198         default:
199                 panic("ffs_isfreeblock");
200         }
201 }
202
203 /*
204  * take a block out of the map
205  */
206 void
207 ffs_clrblock(struct fs *fs, u_char *cp, ufs_daddr_t h)
208 {
209         switch ((int)fs->fs_frag) {
210         case 8:
211                 cp[h] = 0;
212                 return;
213         case 4:
214                 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
215                 return;
216         case 2:
217                 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
218                 return;
219         case 1:
220                 cp[h >> 3] &= ~(0x01 << (h & 0x7));
221                 return;
222         default:
223                 panic("ffs_clrblock");
224         }
225 }
226
227 /*
228  * put a block into the map
229  */
230 void
231 ffs_setblock(struct fs *fs, unsigned char *cp, ufs_daddr_t h)
232 {
233         switch ((int)fs->fs_frag) {
234         case 8:
235                 cp[h] = 0xff;
236                 return;
237         case 4:
238                 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
239                 return;
240         case 2:
241                 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
242                 return;
243         case 1:
244                 cp[h >> 3] |= (0x01 << (h & 0x7));
245                 return;
246         default:
247                 panic("ffs_setblock");
248         }
249 }