xref: /freebsd/sys/ufs/ffs/ffs_subr.c (revision 729362425c09cf6b362366aabc6fb547eee8035a)
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$
35  */
36 
37 #include <sys/param.h>
38 
39 #ifndef _KERNEL
40 #include <ufs/ufs/dinode.h>
41 #include <ufs/ffs/fs.h>
42 #include "fsck.h"
43 #else
44 #include "opt_ddb.h"
45 
46 #include <sys/systm.h>
47 #include <sys/lock.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/vnode.h>
51 #include <sys/bio.h>
52 #include <sys/buf.h>
53 #include <sys/ucred.h>
54 
55 #include <ufs/ufs/quota.h>
56 #include <ufs/ufs/inode.h>
57 #include <ufs/ufs/extattr.h>
58 #include <ufs/ufs/ufsmount.h>
59 #include <ufs/ufs/ufs_extern.h>
60 #include <ufs/ffs/ffs_extern.h>
61 #include <ufs/ffs/fs.h>
62 
63 #ifdef DDB
64 void	ffs_checkoverlap(struct buf *, struct inode *);
65 #endif
66 
67 /*
68  * Return buffer with the contents of block "offset" from the beginning of
69  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
70  * remaining space in the directory.
71  */
72 int
73 ffs_blkatoff(vp, offset, res, bpp)
74 	struct vnode *vp;
75 	off_t offset;
76 	char **res;
77 	struct buf **bpp;
78 {
79 	struct inode *ip;
80 	struct fs *fs;
81 	struct buf *bp;
82 	ufs_lbn_t lbn;
83 	int bsize, error;
84 
85 	ip = VTOI(vp);
86 	fs = ip->i_fs;
87 	lbn = lblkno(fs, offset);
88 	bsize = blksize(fs, ip, lbn);
89 
90 	*bpp = NULL;
91 	error = bread(vp, lbn, bsize, NOCRED, &bp);
92 	if (error) {
93 		brelse(bp);
94 		return (error);
95 	}
96 	if (res)
97 		*res = (char *)bp->b_data + blkoff(fs, offset);
98 	*bpp = bp;
99 	return (0);
100 }
101 
102 /*
103  * Load up the contents of an inode and copy the appropriate pieces
104  * to the incore copy.
105  */
106 void
107 ffs_load_inode(bp, ip, fs, ino)
108 	struct buf *bp;
109 	struct inode *ip;
110 	struct fs *fs;
111 	ino_t ino;
112 {
113 
114 	if (ip->i_ump->um_fstype == UFS1) {
115 		*ip->i_din1 =
116 		    *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
117 		ip->i_mode = ip->i_din1->di_mode;
118 		ip->i_nlink = ip->i_din1->di_nlink;
119 		ip->i_size = ip->i_din1->di_size;
120 		ip->i_flags = ip->i_din1->di_flags;
121 		ip->i_gen = ip->i_din1->di_gen;
122 		ip->i_uid = ip->i_din1->di_uid;
123 		ip->i_gid = ip->i_din1->di_gid;
124 	} else {
125 		*ip->i_din2 =
126 		    *((struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
127 		ip->i_mode = ip->i_din2->di_mode;
128 		ip->i_nlink = ip->i_din2->di_nlink;
129 		ip->i_size = ip->i_din2->di_size;
130 		ip->i_flags = ip->i_din2->di_flags;
131 		ip->i_gen = ip->i_din2->di_gen;
132 		ip->i_uid = ip->i_din2->di_uid;
133 		ip->i_gid = ip->i_din2->di_gid;
134 	}
135 }
136 #endif /* KERNEL */
137 
138 /*
139  * Update the frsum fields to reflect addition or deletion
140  * of some frags.
141  */
142 void
143 ffs_fragacct(fs, fragmap, fraglist, cnt)
144 	struct fs *fs;
145 	int fragmap;
146 	int32_t fraglist[];
147 	int cnt;
148 {
149 	int inblk;
150 	int field, subfield;
151 	int siz, pos;
152 
153 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
154 	fragmap <<= 1;
155 	for (siz = 1; siz < fs->fs_frag; siz++) {
156 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
157 			continue;
158 		field = around[siz];
159 		subfield = inside[siz];
160 		for (pos = siz; pos <= fs->fs_frag; pos++) {
161 			if ((fragmap & field) == subfield) {
162 				fraglist[siz] += cnt;
163 				pos += siz;
164 				field <<= siz;
165 				subfield <<= siz;
166 			}
167 			field <<= 1;
168 			subfield <<= 1;
169 		}
170 	}
171 }
172 
173 #ifdef DDB
174 void
175 ffs_checkoverlap(bp, ip)
176 	struct buf *bp;
177 	struct inode *ip;
178 {
179 	struct buf *ebp, *ep;
180 	ufs2_daddr_t start, last;
181 	struct vnode *vp;
182 
183 	ebp = &buf[nbuf];
184 	start = bp->b_blkno;
185 	last = start + btodb(bp->b_bcount) - 1;
186 	for (ep = buf; ep < ebp; ep++) {
187 		if (ep == bp || (ep->b_flags & B_INVAL) ||
188 		    ep->b_vp == NULLVP)
189 			continue;
190 		vp = ip->i_devvp;
191 		/* look for overlap */
192 		if (ep->b_bcount == 0 || ep->b_blkno > last ||
193 		    ep->b_blkno + btodb(ep->b_bcount) <= start)
194 			continue;
195 		vprint("Disk overlap", vp);
196 		printf("\tstart %jd, end %jd overlap start %jd, end %jd\n",
197 		    (intmax_t)start, (intmax_t)last, (intmax_t)ep->b_blkno,
198 		    (intmax_t)(ep->b_blkno + btodb(ep->b_bcount) - 1));
199 		panic("ffs_checkoverlap: Disk buffer overlap");
200 	}
201 }
202 #endif /* DDB */
203 
204 /*
205  * block operations
206  *
207  * check if a block is available
208  */
209 int
210 ffs_isblock(fs, cp, h)
211 	struct fs *fs;
212 	unsigned char *cp;
213 	ufs1_daddr_t h;
214 {
215 	unsigned char mask;
216 
217 	switch ((int)fs->fs_frag) {
218 	case 8:
219 		return (cp[h] == 0xff);
220 	case 4:
221 		mask = 0x0f << ((h & 0x1) << 2);
222 		return ((cp[h >> 1] & mask) == mask);
223 	case 2:
224 		mask = 0x03 << ((h & 0x3) << 1);
225 		return ((cp[h >> 2] & mask) == mask);
226 	case 1:
227 		mask = 0x01 << (h & 0x7);
228 		return ((cp[h >> 3] & mask) == mask);
229 	default:
230 		panic("ffs_isblock");
231 	}
232 	return (0);
233 }
234 
235 /*
236  * take a block out of the map
237  */
238 void
239 ffs_clrblock(fs, cp, h)
240 	struct fs *fs;
241 	u_char *cp;
242 	ufs1_daddr_t h;
243 {
244 
245 	switch ((int)fs->fs_frag) {
246 	case 8:
247 		cp[h] = 0;
248 		return;
249 	case 4:
250 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
251 		return;
252 	case 2:
253 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
254 		return;
255 	case 1:
256 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
257 		return;
258 	default:
259 		panic("ffs_clrblock");
260 	}
261 }
262 
263 /*
264  * put a block into the map
265  */
266 void
267 ffs_setblock(fs, cp, h)
268 	struct fs *fs;
269 	unsigned char *cp;
270 	ufs1_daddr_t h;
271 {
272 
273 	switch ((int)fs->fs_frag) {
274 
275 	case 8:
276 		cp[h] = 0xff;
277 		return;
278 	case 4:
279 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
280 		return;
281 	case 2:
282 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
283 		return;
284 	case 1:
285 		cp[h >> 3] |= (0x01 << (h & 0x7));
286 		return;
287 	default:
288 		panic("ffs_setblock");
289 	}
290 }
291