]> git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ffs/ffs_subr.c
xnu-344.21.73.tar.gz
[apple/xnu.git] / bsd / ufs / ffs / ffs_subr.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
26 /*
27 * Copyright (c) 1982, 1986, 1989, 1993
28 * The Regents of the University of California. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * @(#)ffs_subr.c 8.5 (Berkeley) 3/21/95
59 */
60
61 #include <rev_endian_fs.h>
62 #include <sys/param.h>
63 #if REV_ENDIAN_FS
64 #include <sys/mount.h>
65 #endif /* REV_ENDIAN_FS */
66
67 #ifndef KERNEL
68 #include <ufs/ufs/dinode.h>
69 #include <ufs/ffs/fs.h>
70 #else
71
72 #include <sys/systm.h>
73 #include <sys/vnode.h>
74 #include <sys/buf.h>
75 #include <sys/quota.h>
76 #include <ufs/ufs/quota.h>
77 #include <ufs/ufs/inode.h>
78 #include <ufs/ffs/fs.h>
79 #include <ufs/ffs/ffs_extern.h>
80 #if REV_ENDIAN_FS
81 #include <ufs/ufs/ufs_byte_order.h>
82 #include <architecture/byte_order.h>
83 #endif /* REV_ENDIAN_FS */
84
85 /*
86 * Return buffer with the contents of block "offset" from the beginning of
87 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
88 * remaining space in the directory.
89 */
90 int
91 ffs_blkatoff(ap)
92 struct vop_blkatoff_args /* {
93 struct vnode *a_vp;
94 off_t a_offset;
95 char **a_res;
96 struct buf **a_bpp;
97 } */ *ap;
98 {
99 struct inode *ip;
100 register struct fs *fs;
101 struct buf *bp;
102 ufs_daddr_t lbn;
103 int bsize, error;
104 #if REV_ENDIAN_FS
105 struct mount *mp=(ap->a_vp)->v_mount;
106 int rev_endian=(mp->mnt_flag & MNT_REVEND);
107 #endif /* REV_ENDIAN_FS */
108
109 ip = VTOI(ap->a_vp);
110 fs = ip->i_fs;
111 lbn = lblkno(fs, ap->a_offset);
112 bsize = blksize(fs, ip, lbn);
113
114 *ap->a_bpp = NULL;
115 if (error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) {
116 brelse(bp);
117 return (error);
118 }
119 #if REV_ENDIAN_FS
120 if (rev_endian)
121 byte_swap_dir_block_in(bp->b_data, bp->b_bcount);
122 #endif /* REV_ENDIAN_FS */
123
124 if (ap->a_res)
125 *ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
126 *ap->a_bpp = bp;
127 return (0);
128 }
129 #endif
130
131 /*
132 * Update the frsum fields to reflect addition or deletion
133 * of some frags.
134 */
135 void
136 ffs_fragacct(fs, fragmap, fraglist, cnt)
137 struct fs *fs;
138 int fragmap;
139 int32_t fraglist[];
140 int cnt;
141 {
142 int inblk;
143 register int field, subfield;
144 register int siz, pos;
145
146 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
147 fragmap <<= 1;
148 for (siz = 1; siz < fs->fs_frag; siz++) {
149 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
150 continue;
151 field = around[siz];
152 subfield = inside[siz];
153 for (pos = siz; pos <= fs->fs_frag; pos++) {
154 if ((fragmap & field) == subfield) {
155 fraglist[siz] += cnt;
156 pos += siz;
157 field <<= siz;
158 subfield <<= siz;
159 }
160 field <<= 1;
161 subfield <<= 1;
162 }
163 }
164 }
165
166 #if defined(KERNEL) && DIAGNOSTIC
167 void
168 ffs_checkoverlap(bp, ip)
169 struct buf *bp;
170 struct inode *ip;
171 {
172 register struct buf *ebp, *ep;
173 register ufs_daddr_t start, last;
174 struct vnode *vp;
175 #ifdef NeXT
176 int devBlockSize=0;
177 #endif /* NeXT */
178
179 ebp = &buf[nbuf];
180 start = bp->b_blkno;
181 #ifdef NeXT
182 VOP_DEVBLOCKSIZE(ip->i_devvp,&devBlockSize);
183 last = start + btodb(bp->b_bcount, devBlockSize) - 1;
184 #else
185 last = start + btodb(bp->b_bcount) - 1;
186 #endif /* NeXT */
187 for (ep = buf; ep < ebp; ep++) {
188 if (ep == bp || (ep->b_flags & B_INVAL) ||
189 ep->b_vp == NULLVP)
190 continue;
191 if (VOP_BMAP(ep->b_vp, (ufs_daddr_t)0, &vp, (ufs_daddr_t)0,
192 NULL))
193 continue;
194 if (vp != ip->i_devvp)
195 continue;
196 /* look for overlap */
197 #ifdef NeXT
198 if (ep->b_bcount == 0 || ep->b_blkno > last ||
199 ep->b_blkno + btodb(ep->b_bcount, devBlockSize) <= start)
200 continue;
201 vprint("Disk overlap", vp);
202 (void)printf("\tstart %d, end %d overlap start %d, end %d\n",
203 start, last, ep->b_blkno,
204 ep->b_blkno + btodb(ep->b_bcount, devBlockSize) - 1);
205 #else
206 if (ep->b_bcount == 0 || ep->b_blkno > last ||
207 ep->b_blkno + btodb(ep->b_bcount) <= start)
208 continue;
209 vprint("Disk overlap", vp);
210 (void)printf("\tstart %d, end %d overlap start %d, end %d\n",
211 start, last, ep->b_blkno,
212 ep->b_blkno + btodb(ep->b_bcount) - 1);
213 #endif /* NeXT */
214 panic("Disk buffer overlap");
215 }
216 }
217 #endif /* DIAGNOSTIC */
218
219 /*
220 * block operations
221 *
222 * check if a block is available
223 */
224 int
225 ffs_isblock(fs, cp, h)
226 struct fs *fs;
227 unsigned char *cp;
228 ufs_daddr_t h;
229 {
230 unsigned char mask;
231
232 switch ((int)fs->fs_frag) {
233 case 8:
234 return (cp[h] == 0xff);
235 case 4:
236 mask = 0x0f << ((h & 0x1) << 2);
237 return ((cp[h >> 1] & mask) == mask);
238 case 2:
239 mask = 0x03 << ((h & 0x3) << 1);
240 return ((cp[h >> 2] & mask) == mask);
241 case 1:
242 mask = 0x01 << (h & 0x7);
243 return ((cp[h >> 3] & mask) == mask);
244 default:
245 panic("ffs_isblock");
246 }
247 }
248
249 /*
250 * take a block out of the map
251 */
252 void
253 ffs_clrblock(fs, cp, h)
254 struct fs *fs;
255 u_char *cp;
256 ufs_daddr_t h;
257 {
258
259 switch ((int)fs->fs_frag) {
260 case 8:
261 cp[h] = 0;
262 return;
263 case 4:
264 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
265 return;
266 case 2:
267 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
268 return;
269 case 1:
270 cp[h >> 3] &= ~(0x01 << (h & 0x7));
271 return;
272 default:
273 panic("ffs_clrblock");
274 }
275 }
276
277 /*
278 * put a block into the map
279 */
280 void
281 ffs_setblock(fs, cp, h)
282 struct fs *fs;
283 unsigned char *cp;
284 ufs_daddr_t h;
285 {
286
287 switch ((int)fs->fs_frag) {
288
289 case 8:
290 cp[h] = 0xff;
291 return;
292 case 4:
293 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
294 return;
295 case 2:
296 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
297 return;
298 case 1:
299 cp[h >> 3] |= (0x01 << (h & 0x7));
300 return;
301 default:
302 panic("ffs_setblock");
303 }
304 }