Annotation of src/sys/ufs/ffs/ffs_subr.c, Revision 1.35
1.35 ! xtraeme 1: /* $NetBSD: ffs_subr.c,v 1.34 2005/07/15 05:01:16 thorpej Exp $ */
1.2 cgd 2:
1.1 mycroft 3: /*
4: * Copyright (c) 1982, 1986, 1989, 1993
5: * The Regents of the University of California. All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
1.29 agc 15: * 3. Neither the name of the University nor the names of its contributors
1.1 mycroft 16: * may be used to endorse or promote products derived from this software
17: * without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: *
1.10 fvdl 31: * @(#)ffs_subr.c 8.5 (Berkeley) 3/21/95
1.1 mycroft 32: */
1.19 lukem 33:
1.30 lukem 34: #if HAVE_NBTOOL_CONFIG_H
35: #include "nbtool_config.h"
1.21 tv 36: #endif
37:
1.30 lukem 38: #include <sys/cdefs.h>
1.35 ! xtraeme 39: __KERNEL_RCSID(0, "$NetBSD: ffs_subr.c,v 1.34 2005/07/15 05:01:16 thorpej Exp $");
1.1 mycroft 40:
41: #include <sys/param.h>
1.20 lukem 42:
43: /* in ffs_tables.c */
1.24 matt 44: extern const int inside[], around[];
45: extern const u_char * const fragtbl[];
1.20 lukem 46:
1.10 fvdl 47: #ifndef _KERNEL
48: #include <ufs/ufs/dinode.h>
1.1 mycroft 49: #include <ufs/ffs/fs.h>
1.7 christos 50: #include <ufs/ffs/ffs_extern.h>
1.11 bouyer 51: #include <ufs/ufs/ufs_bswap.h>
1.35 ! xtraeme 52: void panic(const char *, ...)
1.20 lukem 53: __attribute__((__noreturn__,__format__(__printf__,1,2)));
1.13 drochner 54:
1.20 lukem 55: #else /* _KERNEL */
56: #include <sys/systm.h>
1.1 mycroft 57: #include <sys/vnode.h>
1.11 bouyer 58: #include <sys/mount.h>
1.1 mycroft 59: #include <sys/buf.h>
1.27 tron 60: #include <sys/inttypes.h>
1.28 fvdl 61: #include <sys/pool.h>
1.18 lukem 62: #include <ufs/ufs/inode.h>
1.11 bouyer 63: #include <ufs/ufs/ufsmount.h>
64: #include <ufs/ufs/ufs_extern.h>
1.10 fvdl 65: #include <ufs/ffs/fs.h>
66: #include <ufs/ffs/ffs_extern.h>
1.11 bouyer 67: #include <ufs/ufs/ufs_bswap.h>
1.1 mycroft 68:
69: /*
70: * Return buffer with the contents of block "offset" from the beginning of
71: * directory "ip". If "res" is non-zero, fill it in with a pointer to the
72: * remaining space in the directory.
73: */
74: int
1.34 thorpej 75: ffs_blkatoff(void *v)
1.5 christos 76: {
1.1 mycroft 77: struct vop_blkatoff_args /* {
78: struct vnode *a_vp;
79: off_t a_offset;
80: char **a_res;
81: struct buf **a_bpp;
1.5 christos 82: } */ *ap = v;
1.1 mycroft 83: struct inode *ip;
1.15 augustss 84: struct fs *fs;
1.1 mycroft 85: struct buf *bp;
1.25 fvdl 86: daddr_t lbn;
1.1 mycroft 87: int bsize, error;
88:
89: ip = VTOI(ap->a_vp);
90: fs = ip->i_fs;
91: lbn = lblkno(fs, ap->a_offset);
92: bsize = blksize(fs, ip, lbn);
93:
94: *ap->a_bpp = NULL;
1.5 christos 95: if ((error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) != 0) {
1.1 mycroft 96: brelse(bp);
97: return (error);
98: }
99: if (ap->a_res)
100: *ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
101: *ap->a_bpp = bp;
102: return (0);
103: }
1.28 fvdl 104:
105:
106: /*
107: * Load up the contents of an inode and copy the appropriate pieces
108: * to the incore copy.
109: */
110: void
1.34 thorpej 111: ffs_load_inode(struct buf *bp, struct inode *ip, struct fs *fs, ino_t ino)
1.28 fvdl 112: {
113: struct ufs1_dinode *dp1;
114: struct ufs2_dinode *dp2;
115:
116: if (ip->i_ump->um_fstype == UFS1) {
117: dp1 = (struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
118: #ifdef FFS_EI
119: if (UFS_FSNEEDSWAP(fs))
120: ffs_dinode1_swap(dp1, ip->i_din.ffs1_din);
121: else
122: #endif
123: *ip->i_din.ffs1_din = *dp1;
124:
125: ip->i_mode = ip->i_ffs1_mode;
126: ip->i_nlink = ip->i_ffs1_nlink;
127: ip->i_size = ip->i_ffs1_size;
128: ip->i_flags = ip->i_ffs1_flags;
129: ip->i_gen = ip->i_ffs1_gen;
130: ip->i_uid = ip->i_ffs1_uid;
131: ip->i_gid = ip->i_ffs1_gid;
132: } else {
133: dp2 = (struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
134: #ifdef FFS_EI
135: if (UFS_FSNEEDSWAP(fs))
136: ffs_dinode2_swap(dp2, ip->i_din.ffs2_din);
137: else
138: #endif
139: *ip->i_din.ffs2_din = *dp2;
140:
141: ip->i_mode = ip->i_ffs2_mode;
142: ip->i_nlink = ip->i_ffs2_nlink;
143: ip->i_size = ip->i_ffs2_size;
144: ip->i_flags = ip->i_ffs2_flags;
145: ip->i_gen = ip->i_ffs2_gen;
146: ip->i_uid = ip->i_ffs2_uid;
147: ip->i_gid = ip->i_ffs2_gid;
148: }
149: }
150:
1.20 lukem 151: #endif /* _KERNEL */
1.1 mycroft 152:
153: /*
1.33 perry 154: * Update the frsum fields to reflect addition or deletion
1.1 mycroft 155: * of some frags.
156: */
157: void
1.34 thorpej 158: ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt,
159: int needswap)
1.1 mycroft 160: {
161: int inblk;
1.15 augustss 162: int field, subfield;
163: int siz, pos;
1.1 mycroft 164:
165: inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
166: fragmap <<= 1;
167: for (siz = 1; siz < fs->fs_frag; siz++) {
1.22 mycroft 168: if ((inblk & (1 << (siz + (fs->fs_frag & (NBBY - 1))))) == 0)
1.1 mycroft 169: continue;
170: field = around[siz];
171: subfield = inside[siz];
172: for (pos = siz; pos <= fs->fs_frag; pos++) {
173: if ((fragmap & field) == subfield) {
1.11 bouyer 174: fraglist[siz] = ufs_rw32(
1.12 kleink 175: ufs_rw32(fraglist[siz], needswap) + cnt,
176: needswap);
1.1 mycroft 177: pos += siz;
178: field <<= siz;
179: subfield <<= siz;
180: }
181: field <<= 1;
182: subfield <<= 1;
183: }
184: }
185: }
186:
1.4 jtc 187: #if defined(_KERNEL) && defined(DIAGNOSTIC)
1.1 mycroft 188: void
1.34 thorpej 189: ffs_checkoverlap(struct buf *bp, struct inode *ip)
1.1 mycroft 190: {
1.32 pk 191: #if 0
1.15 augustss 192: struct buf *ebp, *ep;
1.25 fvdl 193: daddr_t start, last;
1.1 mycroft 194: struct vnode *vp;
195:
196: ebp = &buf[nbuf];
197: start = bp->b_blkno;
198: last = start + btodb(bp->b_bcount) - 1;
199: for (ep = buf; ep < ebp; ep++) {
200: if (ep == bp || (ep->b_flags & B_INVAL) ||
201: ep->b_vp == NULLVP)
202: continue;
203: if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0, NULL))
204: continue;
205: if (vp != ip->i_devvp)
206: continue;
207: /* look for overlap */
208: if (ep->b_bcount == 0 || ep->b_blkno > last ||
209: ep->b_blkno + btodb(ep->b_bcount) <= start)
210: continue;
211: vprint("Disk overlap", vp);
1.27 tron 212: printf("\tstart %" PRId64 ", end %" PRId64 " overlap start "
213: "%" PRId64 ", end %" PRId64 "\n",
1.8 christos 214: start, last, ep->b_blkno,
1.26 tron 215: ep->b_blkno + btodb(ep->b_bcount) - 1);
1.1 mycroft 216: panic("Disk buffer overlap");
217: }
1.32 pk 218: #else
219: printf("ffs_checkoverlap disabled due to buffer cache implementation changes\n");
220: #endif
1.1 mycroft 221: }
1.20 lukem 222: #endif /* _KERNEL && DIAGNOSTIC */
1.1 mycroft 223:
224: /*
225: * block operations
226: *
227: * check if a block is available
1.31 dbj 228: * returns true if all the correponding bits in the free map are 1
1.33 perry 229: * returns false if any corresponding bit in the free map is 0
1.1 mycroft 230: */
231: int
1.34 thorpej 232: ffs_isblock(struct fs *fs, u_char *cp, int32_t h)
1.1 mycroft 233: {
1.16 lukem 234: u_char mask;
1.1 mycroft 235:
1.22 mycroft 236: switch ((int)fs->fs_fragshift) {
237: case 3:
1.1 mycroft 238: return (cp[h] == 0xff);
1.22 mycroft 239: case 2:
1.1 mycroft 240: mask = 0x0f << ((h & 0x1) << 2);
241: return ((cp[h >> 1] & mask) == mask);
1.22 mycroft 242: case 1:
1.1 mycroft 243: mask = 0x03 << ((h & 0x3) << 1);
244: return ((cp[h >> 2] & mask) == mask);
1.22 mycroft 245: case 0:
1.1 mycroft 246: mask = 0x01 << (h & 0x7);
247: return ((cp[h >> 3] & mask) == mask);
248: default:
1.22 mycroft 249: panic("ffs_isblock: unknown fs_fragshift %d",
250: (int)fs->fs_fragshift);
1.14 fvdl 251: }
252: }
253:
254: /*
1.31 dbj 255: * check if a block is completely allocated
256: * returns true if all the corresponding bits in the free map are 0
257: * returns false if any corresponding bit in the free map is 1
1.14 fvdl 258: */
259: int
1.34 thorpej 260: ffs_isfreeblock(struct fs *fs, u_char *cp, int32_t h)
1.14 fvdl 261: {
262:
1.22 mycroft 263: switch ((int)fs->fs_fragshift) {
264: case 3:
1.14 fvdl 265: return (cp[h] == 0);
1.22 mycroft 266: case 2:
1.14 fvdl 267: return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
1.22 mycroft 268: case 1:
1.14 fvdl 269: return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
1.22 mycroft 270: case 0:
1.14 fvdl 271: return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
272: default:
1.22 mycroft 273: panic("ffs_isfreeblock: unknown fs_fragshift %d",
274: (int)fs->fs_fragshift);
1.1 mycroft 275: }
276: }
277:
278: /*
279: * take a block out of the map
280: */
281: void
1.34 thorpej 282: ffs_clrblock(struct fs *fs, u_char *cp, int32_t h)
1.1 mycroft 283: {
284:
1.22 mycroft 285: switch ((int)fs->fs_fragshift) {
286: case 3:
1.1 mycroft 287: cp[h] = 0;
288: return;
1.22 mycroft 289: case 2:
1.1 mycroft 290: cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
291: return;
1.22 mycroft 292: case 1:
1.1 mycroft 293: cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
294: return;
1.22 mycroft 295: case 0:
1.1 mycroft 296: cp[h >> 3] &= ~(0x01 << (h & 0x7));
297: return;
298: default:
1.22 mycroft 299: panic("ffs_clrblock: unknown fs_fragshift %d",
300: (int)fs->fs_fragshift);
1.1 mycroft 301: }
302: }
303:
304: /*
305: * put a block into the map
306: */
307: void
1.34 thorpej 308: ffs_setblock(struct fs *fs, u_char *cp, int32_t h)
1.1 mycroft 309: {
310:
1.22 mycroft 311: switch ((int)fs->fs_fragshift) {
312: case 3:
1.1 mycroft 313: cp[h] = 0xff;
314: return;
1.22 mycroft 315: case 2:
1.1 mycroft 316: cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
317: return;
1.22 mycroft 318: case 1:
1.1 mycroft 319: cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
320: return;
1.22 mycroft 321: case 0:
1.1 mycroft 322: cp[h >> 3] |= (0x01 << (h & 0x7));
323: return;
324: default:
1.22 mycroft 325: panic("ffs_setblock: unknown fs_fragshift %d",
326: (int)fs->fs_fragshift);
1.1 mycroft 327: }
328: }
CVSweb <webmaster@jp.NetBSD.org>