xref: /freebsd/sys/ufs/ffs/ffs_subr.c (revision 7bd6fde3)
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)ffs_subr.c	8.5 (Berkeley) 3/21/95
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 
37 #ifndef _KERNEL
38 #include <ufs/ufs/dinode.h>
39 #include <ufs/ffs/fs.h>
40 #include "fsck.h"
41 #else
42 #include <sys/systm.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/vnode.h>
47 #include <sys/bio.h>
48 #include <sys/buf.h>
49 #include <sys/ucred.h>
50 
51 #include <ufs/ufs/quota.h>
52 #include <ufs/ufs/inode.h>
53 #include <ufs/ufs/extattr.h>
54 #include <ufs/ufs/ufsmount.h>
55 #include <ufs/ufs/ufs_extern.h>
56 #include <ufs/ffs/ffs_extern.h>
57 #include <ufs/ffs/fs.h>
58 
59 #ifdef KDB
60 void	ffs_checkoverlap(struct buf *, struct inode *);
61 #endif
62 
63 /*
64  * Return buffer with the contents of block "offset" from the beginning of
65  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
66  * remaining space in the directory.
67  */
68 int
69 ffs_blkatoff(vp, offset, res, bpp)
70 	struct vnode *vp;
71 	off_t offset;
72 	char **res;
73 	struct buf **bpp;
74 {
75 	struct inode *ip;
76 	struct fs *fs;
77 	struct buf *bp;
78 	ufs_lbn_t lbn;
79 	int bsize, error;
80 
81 	ip = VTOI(vp);
82 	fs = ip->i_fs;
83 	lbn = lblkno(fs, offset);
84 	bsize = blksize(fs, ip, lbn);
85 
86 	*bpp = NULL;
87 	error = bread(vp, lbn, bsize, NOCRED, &bp);
88 	if (error) {
89 		brelse(bp);
90 		return (error);
91 	}
92 	if (res)
93 		*res = (char *)bp->b_data + blkoff(fs, offset);
94 	*bpp = bp;
95 	return (0);
96 }
97 
98 /*
99  * Load up the contents of an inode and copy the appropriate pieces
100  * to the incore copy.
101  */
102 void
103 ffs_load_inode(bp, ip, fs, ino)
104 	struct buf *bp;
105 	struct inode *ip;
106 	struct fs *fs;
107 	ino_t ino;
108 {
109 
110 	if (ip->i_ump->um_fstype == UFS1) {
111 		*ip->i_din1 =
112 		    *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
113 		ip->i_mode = ip->i_din1->di_mode;
114 		ip->i_nlink = ip->i_din1->di_nlink;
115 		ip->i_size = ip->i_din1->di_size;
116 		ip->i_flags = ip->i_din1->di_flags;
117 		ip->i_gen = ip->i_din1->di_gen;
118 		ip->i_uid = ip->i_din1->di_uid;
119 		ip->i_gid = ip->i_din1->di_gid;
120 	} else {
121 		*ip->i_din2 =
122 		    *((struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
123 		ip->i_mode = ip->i_din2->di_mode;
124 		ip->i_nlink = ip->i_din2->di_nlink;
125 		ip->i_size = ip->i_din2->di_size;
126 		ip->i_flags = ip->i_din2->di_flags;
127 		ip->i_gen = ip->i_din2->di_gen;
128 		ip->i_uid = ip->i_din2->di_uid;
129 		ip->i_gid = ip->i_din2->di_gid;
130 	}
131 }
132 #endif /* KERNEL */
133 
134 /*
135  * Update the frsum fields to reflect addition or deletion
136  * of some frags.
137  */
138 void
139 ffs_fragacct(fs, fragmap, fraglist, cnt)
140 	struct fs *fs;
141 	int fragmap;
142 	int32_t fraglist[];
143 	int cnt;
144 {
145 	int inblk;
146 	int field, subfield;
147 	int siz, pos;
148 
149 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
150 	fragmap <<= 1;
151 	for (siz = 1; siz < fs->fs_frag; siz++) {
152 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
153 			continue;
154 		field = around[siz];
155 		subfield = inside[siz];
156 		for (pos = siz; pos <= fs->fs_frag; pos++) {
157 			if ((fragmap & field) == subfield) {
158 				fraglist[siz] += cnt;
159 				pos += siz;
160 				field <<= siz;
161 				subfield <<= siz;
162 			}
163 			field <<= 1;
164 			subfield <<= 1;
165 		}
166 	}
167 }
168 
169 #ifdef KDB
170 void
171 ffs_checkoverlap(bp, ip)
172 	struct buf *bp;
173 	struct inode *ip;
174 {
175 	struct buf *ebp, *ep;
176 	ufs2_daddr_t start, last;
177 	struct vnode *vp;
178 
179 	ebp = &buf[nbuf];
180 	start = bp->b_blkno;
181 	last = start + btodb(bp->b_bcount) - 1;
182 	for (ep = buf; ep < ebp; ep++) {
183 		if (ep == bp || (ep->b_flags & B_INVAL) ||
184 		    ep->b_vp == NULLVP)
185 			continue;
186 		vp = ip->i_devvp;
187 		/* look for overlap */
188 		if (ep->b_bcount == 0 || ep->b_blkno > last ||
189 		    ep->b_blkno + btodb(ep->b_bcount) <= start)
190 			continue;
191 		vprint("Disk overlap", vp);
192 		printf("\tstart %jd, end %jd overlap start %jd, end %jd\n",
193 		    (intmax_t)start, (intmax_t)last, (intmax_t)ep->b_blkno,
194 		    (intmax_t)(ep->b_blkno + btodb(ep->b_bcount) - 1));
195 		panic("ffs_checkoverlap: Disk buffer overlap");
196 	}
197 }
198 #endif /* KDB */
199 
200 /*
201  * block operations
202  *
203  * check if a block is available
204  */
205 int
206 ffs_isblock(fs, cp, h)
207 	struct fs *fs;
208 	unsigned char *cp;
209 	ufs1_daddr_t h;
210 {
211 	unsigned char mask;
212 
213 	switch ((int)fs->fs_frag) {
214 	case 8:
215 		return (cp[h] == 0xff);
216 	case 4:
217 		mask = 0x0f << ((h & 0x1) << 2);
218 		return ((cp[h >> 1] & mask) == mask);
219 	case 2:
220 		mask = 0x03 << ((h & 0x3) << 1);
221 		return ((cp[h >> 2] & mask) == mask);
222 	case 1:
223 		mask = 0x01 << (h & 0x7);
224 		return ((cp[h >> 3] & mask) == mask);
225 	default:
226 		panic("ffs_isblock");
227 	}
228 	return (0);
229 }
230 
231 /*
232  * take a block out of the map
233  */
234 void
235 ffs_clrblock(fs, cp, h)
236 	struct fs *fs;
237 	u_char *cp;
238 	ufs1_daddr_t h;
239 {
240 
241 	switch ((int)fs->fs_frag) {
242 	case 8:
243 		cp[h] = 0;
244 		return;
245 	case 4:
246 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
247 		return;
248 	case 2:
249 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
250 		return;
251 	case 1:
252 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
253 		return;
254 	default:
255 		panic("ffs_clrblock");
256 	}
257 }
258 
259 /*
260  * put a block into the map
261  */
262 void
263 ffs_setblock(fs, cp, h)
264 	struct fs *fs;
265 	unsigned char *cp;
266 	ufs1_daddr_t h;
267 {
268 
269 	switch ((int)fs->fs_frag) {
270 
271 	case 8:
272 		cp[h] = 0xff;
273 		return;
274 	case 4:
275 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
276 		return;
277 	case 2:
278 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
279 		return;
280 	case 1:
281 		cp[h >> 3] |= (0x01 << (h & 0x7));
282 		return;
283 	default:
284 		panic("ffs_setblock");
285 	}
286 }
287