1 /* $MirOS: src/usr.sbin/makefs/nbsrc/sys/ufs/ffs/ffs_subr.c,v 1.3 2013/10/31 20:07:27 tg Exp $ */
2 /* $NetBSD: ffs_subr.c,v 1.45 2008/06/03 09:47:49 hannken Exp $ */
3
4 /*
5 * Copyright © 2013
6 * Thorsten “mirabilos” Glaser <tg@mirbsd.org>
7 * Copyright (c) 1982, 1986, 1989, 1993
8 * The Regents of the University of California. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)ffs_subr.c 8.5 (Berkeley) 3/21/95
35 */
36
37 #if HAVE_NBTOOL_CONFIG_H
38 #include "nbtool_config.h"
39 #endif
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: ffs_subr.c,v 1.45 2008/06/03 09:47:49 hannken Exp $");
43
44 #include <sys/param.h>
45
46 /* in ffs_tables.c */
47 extern const int inside[], around[];
48 extern const u_char * const fragtbl[];
49
50 #include <ufs/ufs/ufs_bswap.h>
51 #include <ufs/ffs/fs.h>
52 #include <ufs/ffs/ffs_extern.h>
53
54 #ifndef _KERNEL
55 #include <ufs/ufs/dinode.h>
56 void panic(const char *, ...)
57 __attribute__((__noreturn__, __format__(__printf__, 1, 2)));
58
59 #else /* _KERNEL */
60 #include <sys/systm.h>
61 #include <sys/vnode.h>
62 #include <sys/mount.h>
63 #include <sys/buf.h>
64 #include <sys/inttypes.h>
65 #include <sys/pool.h>
66 #include <sys/fstrans.h>
67 #include <ufs/ufs/inode.h>
68 #include <ufs/ufs/ufsmount.h>
69 #include <ufs/ufs/ufs_extern.h>
70
71 /*
72 * Load up the contents of an inode and copy the appropriate pieces
73 * to the incore copy.
74 */
75 void
ffs_load_inode(struct buf * bp,struct inode * ip,struct fs * fs,ino_t ino)76 ffs_load_inode(struct buf *bp, struct inode *ip, struct fs *fs, ino_t ino)
77 {
78 struct ufs1_dinode *dp1;
79 struct ufs2_dinode *dp2;
80
81 if (ip->i_ump->um_fstype == UFS1) {
82 dp1 = (struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
83 #ifdef FFS_EI
84 if (UFS_FSNEEDSWAP(fs))
85 ffs_dinode1_swap(dp1, ip->i_din.ffs1_din);
86 else
87 #endif
88 *ip->i_din.ffs1_din = *dp1;
89
90 ip->i_mode = ip->i_ffs1_mode;
91 ip->i_nlink = ip->i_ffs1_nlink;
92 ip->i_size = ip->i_ffs1_size;
93 ip->i_flags = ip->i_ffs1_flags;
94 ip->i_gen = ip->i_ffs1_gen;
95 ip->i_uid = ip->i_ffs1_uid;
96 ip->i_gid = ip->i_ffs1_gid;
97 } else {
98 dp2 = (struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino);
99 #ifdef FFS_EI
100 if (UFS_FSNEEDSWAP(fs))
101 ffs_dinode2_swap(dp2, ip->i_din.ffs2_din);
102 else
103 #endif
104 *ip->i_din.ffs2_din = *dp2;
105
106 ip->i_mode = ip->i_ffs2_mode;
107 ip->i_nlink = ip->i_ffs2_nlink;
108 ip->i_size = ip->i_ffs2_size;
109 ip->i_flags = ip->i_ffs2_flags;
110 ip->i_gen = ip->i_ffs2_gen;
111 ip->i_uid = ip->i_ffs2_uid;
112 ip->i_gid = ip->i_ffs2_gid;
113 }
114 }
115
116 int
ffs_getblk(struct vnode * vp,daddr_t lblkno,daddr_t blkno,int size,bool clearbuf,buf_t ** bpp)117 ffs_getblk(struct vnode *vp, daddr_t lblkno, daddr_t blkno, int size,
118 bool clearbuf, buf_t **bpp)
119 {
120 int error = 0;
121
122 KASSERT(blkno >= 0 || blkno == FFS_NOBLK);
123
124 if ((*bpp = getblk(vp, lblkno, size, 0, 0)) == NULL)
125 return ENOMEM;
126 if (blkno != FFS_NOBLK)
127 (*bpp)->b_blkno = blkno;
128 if (clearbuf)
129 clrbuf(*bpp);
130 if ((*bpp)->b_blkno >= 0 && (error = fscow_run(*bpp, false)) != 0)
131 brelse(*bpp, BC_INVAL);
132 return error;
133 }
134
135 #endif /* _KERNEL */
136
137 /*
138 * Update the frsum fields to reflect addition or deletion
139 * of some frags.
140 */
141 void
ffs_fragacct(struct fs * fs,int fragmap,int32_t fraglist[],int cnt,int needswap)142 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt,
143 int needswap)
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 - 1))))) == 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] = ufs_rw32(
159 ufs_rw32(fraglist[siz], needswap) + cnt,
160 needswap);
161 pos += siz;
162 field <<= siz;
163 subfield <<= siz;
164 }
165 field <<= 1;
166 subfield <<= 1;
167 }
168 }
169 }
170
171 /*
172 * block operations
173 *
174 * check if a block is available
175 * returns true if all the correponding bits in the free map are 1
176 * returns false if any corresponding bit in the free map is 0
177 */
178 int
ffs_isblock(struct fs * fs,u_char * cp,int32_t h)179 ffs_isblock(struct fs *fs, u_char *cp, int32_t h)
180 {
181 u_char mask;
182
183 switch ((int)fs->fs_fragshift) {
184 case 3:
185 return (cp[h] == 0xff);
186 case 2:
187 mask = 0x0f << ((h & 0x1) << 2);
188 return ((cp[h >> 1] & mask) == mask);
189 case 1:
190 mask = 0x03 << ((h & 0x3) << 1);
191 return ((cp[h >> 2] & mask) == mask);
192 case 0:
193 mask = 0x01 << (h & 0x7);
194 return ((cp[h >> 3] & mask) == mask);
195 default:
196 panic("ffs_isblock: unknown fs_fragshift %d",
197 (int)fs->fs_fragshift);
198 }
199 }
200
201 /*
202 * check if a block is completely allocated
203 * returns true if all the corresponding bits in the free map are 0
204 * returns false if any corresponding bit in the free map is 1
205 */
206 int
ffs_isfreeblock(struct fs * fs,u_char * cp,int32_t h)207 ffs_isfreeblock(struct fs *fs, u_char *cp, int32_t h)
208 {
209
210 switch ((int)fs->fs_fragshift) {
211 case 3:
212 return (cp[h] == 0);
213 case 2:
214 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
215 case 1:
216 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
217 case 0:
218 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
219 default:
220 panic("ffs_isfreeblock: unknown fs_fragshift %d",
221 (int)fs->fs_fragshift);
222 }
223 }
224
225 /*
226 * take a block out of the map
227 */
228 void
ffs_clrblock(struct fs * fs,u_char * cp,int32_t h)229 ffs_clrblock(struct fs *fs, u_char *cp, int32_t h)
230 {
231
232 switch ((int)fs->fs_fragshift) {
233 case 3:
234 cp[h] = 0;
235 return;
236 case 2:
237 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
238 return;
239 case 1:
240 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
241 return;
242 case 0:
243 cp[h >> 3] &= ~(0x01 << (h & 0x7));
244 return;
245 default:
246 panic("ffs_clrblock: unknown fs_fragshift %d",
247 (int)fs->fs_fragshift);
248 }
249 }
250
251 /*
252 * put a block into the map
253 */
254 void
ffs_setblock(struct fs * fs,u_char * cp,int32_t h)255 ffs_setblock(struct fs *fs, u_char *cp, int32_t h)
256 {
257
258 switch ((int)fs->fs_fragshift) {
259 case 3:
260 cp[h] = 0xff;
261 return;
262 case 2:
263 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
264 return;
265 case 1:
266 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
267 return;
268 case 0:
269 cp[h >> 3] |= (0x01 << (h & 0x7));
270 return;
271 default:
272 panic("ffs_setblock: unknown fs_fragshift %d",
273 (int)fs->fs_fragshift);
274 }
275 }
276