ext2fs_alloc.c revision 1.12 1 1.12 lukem /* $NetBSD: ext2fs_alloc.c,v 1.12 2001/10/26 05:56:07 lukem Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.1 bouyer * Copyright (c) 1997 Manuel Bouyer.
5 1.1 bouyer * Copyright (c) 1982, 1986, 1989, 1993
6 1.1 bouyer * The Regents of the University of California. All rights reserved.
7 1.1 bouyer *
8 1.1 bouyer * Redistribution and use in source and binary forms, with or without
9 1.1 bouyer * modification, are permitted provided that the following conditions
10 1.1 bouyer * are met:
11 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
12 1.1 bouyer * notice, this list of conditions and the following disclaimer.
13 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
15 1.1 bouyer * documentation and/or other materials provided with the distribution.
16 1.1 bouyer * 3. All advertising materials mentioning features or use of this software
17 1.1 bouyer * must display the following acknowledgement:
18 1.1 bouyer * This product includes software developed by the University of
19 1.1 bouyer * California, Berkeley and its contributors.
20 1.1 bouyer * 4. Neither the name of the University nor the names of its contributors
21 1.1 bouyer * may be used to endorse or promote products derived from this software
22 1.1 bouyer * without specific prior written permission.
23 1.1 bouyer *
24 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 bouyer * SUCH DAMAGE.
35 1.1 bouyer *
36 1.1 bouyer * @(#)ffs_alloc.c 8.11 (Berkeley) 10/27/94
37 1.1 bouyer * Modified for ext2fs by Manuel Bouyer.
38 1.1 bouyer */
39 1.1 bouyer
40 1.1 bouyer #include <sys/param.h>
41 1.1 bouyer #include <sys/systm.h>
42 1.1 bouyer #include <sys/buf.h>
43 1.1 bouyer #include <sys/proc.h>
44 1.1 bouyer #include <sys/vnode.h>
45 1.1 bouyer #include <sys/mount.h>
46 1.1 bouyer #include <sys/kernel.h>
47 1.1 bouyer #include <sys/syslog.h>
48 1.1 bouyer
49 1.1 bouyer #include <ufs/ufs/inode.h>
50 1.1 bouyer #include <ufs/ufs/ufs_extern.h>
51 1.1 bouyer
52 1.1 bouyer #include <ufs/ext2fs/ext2fs.h>
53 1.1 bouyer #include <ufs/ext2fs/ext2fs_extern.h>
54 1.1 bouyer
55 1.1 bouyer u_long ext2gennumber;
56 1.1 bouyer
57 1.3 fvdl static ufs_daddr_t ext2fs_alloccg __P((struct inode *, int, ufs_daddr_t, int));
58 1.1 bouyer static u_long ext2fs_dirpref __P((struct m_ext2fs *));
59 1.1 bouyer static void ext2fs_fserr __P((struct m_ext2fs *, u_int, char *));
60 1.1 bouyer static u_long ext2fs_hashalloc __P((struct inode *, int, long, int,
61 1.3 fvdl ufs_daddr_t (*)(struct inode *, int, ufs_daddr_t,
62 1.1 bouyer int)));
63 1.3 fvdl static ufs_daddr_t ext2fs_nodealloccg __P((struct inode *, int, ufs_daddr_t, int));
64 1.3 fvdl static ufs_daddr_t ext2fs_mapsearch __P((struct m_ext2fs *, char *, ufs_daddr_t));
65 1.1 bouyer
66 1.1 bouyer /*
67 1.1 bouyer * Allocate a block in the file system.
68 1.1 bouyer *
69 1.1 bouyer * A preference may be optionally specified. If a preference is given
70 1.1 bouyer * the following hierarchy is used to allocate a block:
71 1.1 bouyer * 1) allocate the requested block.
72 1.1 bouyer * 2) allocate a rotationally optimal block in the same cylinder.
73 1.1 bouyer * 3) allocate a block in the same cylinder group.
74 1.1 bouyer * 4) quadradically rehash into other cylinder groups, until an
75 1.1 bouyer * available block is located.
76 1.11 wiz * If no block preference is given the following hierarchy is used
77 1.1 bouyer * to allocate a block:
78 1.1 bouyer * 1) allocate a block in the cylinder group that contains the
79 1.1 bouyer * inode for the file.
80 1.1 bouyer * 2) quadradically rehash into other cylinder groups, until an
81 1.1 bouyer * available block is located.
82 1.1 bouyer */
83 1.1 bouyer int
84 1.1 bouyer ext2fs_alloc(ip, lbn, bpref, cred, bnp)
85 1.7 augustss struct inode *ip;
86 1.3 fvdl ufs_daddr_t lbn, bpref;
87 1.1 bouyer struct ucred *cred;
88 1.3 fvdl ufs_daddr_t *bnp;
89 1.1 bouyer {
90 1.7 augustss struct m_ext2fs *fs;
91 1.3 fvdl ufs_daddr_t bno;
92 1.1 bouyer int cg;
93 1.1 bouyer
94 1.1 bouyer *bnp = 0;
95 1.1 bouyer fs = ip->i_e2fs;
96 1.1 bouyer #ifdef DIAGNOSTIC
97 1.1 bouyer if (cred == NOCRED)
98 1.1 bouyer panic("ext2fs_alloc: missing credential\n");
99 1.1 bouyer #endif /* DIAGNOSTIC */
100 1.1 bouyer if (fs->e2fs.e2fs_fbcount == 0)
101 1.1 bouyer goto nospace;
102 1.1 bouyer if (cred->cr_uid != 0 && freespace(fs) <= 0)
103 1.1 bouyer goto nospace;
104 1.1 bouyer if (bpref >= fs->e2fs.e2fs_bcount)
105 1.1 bouyer bpref = 0;
106 1.1 bouyer if (bpref == 0)
107 1.1 bouyer cg = ino_to_cg(fs, ip->i_number);
108 1.1 bouyer else
109 1.1 bouyer cg = dtog(fs, bpref);
110 1.3 fvdl bno = (ufs_daddr_t)ext2fs_hashalloc(ip, cg, bpref, fs->e2fs_bsize,
111 1.1 bouyer ext2fs_alloccg);
112 1.1 bouyer if (bno > 0) {
113 1.1 bouyer ip->i_e2fs_nblock += btodb(fs->e2fs_bsize);
114 1.1 bouyer ip->i_flag |= IN_CHANGE | IN_UPDATE;
115 1.1 bouyer *bnp = bno;
116 1.1 bouyer return (0);
117 1.1 bouyer }
118 1.1 bouyer nospace:
119 1.1 bouyer ext2fs_fserr(fs, cred->cr_uid, "file system full");
120 1.1 bouyer uprintf("\n%s: write failed, file system is full\n", fs->e2fs_fsmnt);
121 1.1 bouyer return (ENOSPC);
122 1.1 bouyer }
123 1.1 bouyer
124 1.1 bouyer /*
125 1.1 bouyer * Allocate an inode in the file system.
126 1.1 bouyer *
127 1.1 bouyer * If allocating a directory, use ext2fs_dirpref to select the inode.
128 1.1 bouyer * If allocating in a directory, the following hierarchy is followed:
129 1.1 bouyer * 1) allocate the preferred inode.
130 1.1 bouyer * 2) allocate an inode in the same cylinder group.
131 1.1 bouyer * 3) quadradically rehash into other cylinder groups, until an
132 1.1 bouyer * available inode is located.
133 1.11 wiz * If no inode preference is given the following hierarchy is used
134 1.1 bouyer * to allocate an inode:
135 1.1 bouyer * 1) allocate an inode in cylinder group 0.
136 1.1 bouyer * 2) quadradically rehash into other cylinder groups, until an
137 1.1 bouyer * available inode is located.
138 1.1 bouyer */
139 1.1 bouyer int
140 1.1 bouyer ext2fs_valloc(v)
141 1.1 bouyer void *v;
142 1.1 bouyer {
143 1.1 bouyer struct vop_valloc_args /* {
144 1.1 bouyer struct vnode *a_pvp;
145 1.1 bouyer int a_mode;
146 1.1 bouyer struct ucred *a_cred;
147 1.1 bouyer struct vnode **a_vpp;
148 1.1 bouyer } */ *ap = v;
149 1.7 augustss struct vnode *pvp = ap->a_pvp;
150 1.7 augustss struct inode *pip;
151 1.7 augustss struct m_ext2fs *fs;
152 1.7 augustss struct inode *ip;
153 1.1 bouyer mode_t mode = ap->a_mode;
154 1.1 bouyer ino_t ino, ipref;
155 1.1 bouyer int cg, error;
156 1.1 bouyer
157 1.1 bouyer *ap->a_vpp = NULL;
158 1.1 bouyer pip = VTOI(pvp);
159 1.1 bouyer fs = pip->i_e2fs;
160 1.1 bouyer if (fs->e2fs.e2fs_ficount == 0)
161 1.1 bouyer goto noinodes;
162 1.1 bouyer
163 1.1 bouyer if ((mode & IFMT) == IFDIR)
164 1.1 bouyer cg = ext2fs_dirpref(fs);
165 1.1 bouyer else
166 1.1 bouyer cg = ino_to_cg(fs, pip->i_number);
167 1.1 bouyer ipref = cg * fs->e2fs.e2fs_ipg + 1;
168 1.1 bouyer ino = (ino_t)ext2fs_hashalloc(pip, cg, (long)ipref, mode, ext2fs_nodealloccg);
169 1.1 bouyer if (ino == 0)
170 1.1 bouyer goto noinodes;
171 1.1 bouyer error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp);
172 1.1 bouyer if (error) {
173 1.1 bouyer VOP_VFREE(pvp, ino, mode);
174 1.1 bouyer return (error);
175 1.1 bouyer }
176 1.1 bouyer ip = VTOI(*ap->a_vpp);
177 1.1 bouyer if (ip->i_e2fs_mode && ip->i_e2fs_nlink != 0) {
178 1.1 bouyer printf("mode = 0%o, nlinks %d, inum = %d, fs = %s\n",
179 1.1 bouyer ip->i_e2fs_mode, ip->i_e2fs_nlink, ip->i_number, fs->e2fs_fsmnt);
180 1.1 bouyer panic("ext2fs_valloc: dup alloc");
181 1.1 bouyer }
182 1.1 bouyer
183 1.5 thorpej memset(&ip->i_din, 0, sizeof(ip->i_din));
184 1.1 bouyer
185 1.1 bouyer /*
186 1.1 bouyer * Set up a new generation number for this inode.
187 1.1 bouyer */
188 1.1 bouyer if (++ext2gennumber < (u_long)time.tv_sec)
189 1.1 bouyer ext2gennumber = time.tv_sec;
190 1.1 bouyer ip->i_e2fs_gen = ext2gennumber;
191 1.1 bouyer return (0);
192 1.1 bouyer noinodes:
193 1.1 bouyer ext2fs_fserr(fs, ap->a_cred->cr_uid, "out of inodes");
194 1.1 bouyer uprintf("\n%s: create/symlink failed, no inodes free\n", fs->e2fs_fsmnt);
195 1.1 bouyer return (ENOSPC);
196 1.1 bouyer }
197 1.1 bouyer
198 1.1 bouyer /*
199 1.1 bouyer * Find a cylinder to place a directory.
200 1.1 bouyer *
201 1.1 bouyer * The policy implemented by this algorithm is to select from
202 1.1 bouyer * among those cylinder groups with above the average number of
203 1.1 bouyer * free inodes, the one with the smallest number of directories.
204 1.1 bouyer */
205 1.1 bouyer static u_long
206 1.1 bouyer ext2fs_dirpref(fs)
207 1.7 augustss struct m_ext2fs *fs;
208 1.1 bouyer {
209 1.1 bouyer int cg, maxspace, mincg, avgifree;
210 1.1 bouyer
211 1.1 bouyer avgifree = fs->e2fs.e2fs_ficount / fs->e2fs_ncg;
212 1.1 bouyer maxspace = 0;
213 1.1 bouyer mincg = -1;
214 1.1 bouyer for (cg = 0; cg < fs->e2fs_ncg; cg++)
215 1.1 bouyer if ( fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree) {
216 1.1 bouyer if (mincg == -1 || fs->e2fs_gd[cg].ext2bgd_nbfree > maxspace) {
217 1.1 bouyer mincg = cg;
218 1.1 bouyer maxspace = fs->e2fs_gd[cg].ext2bgd_nbfree;
219 1.1 bouyer }
220 1.1 bouyer }
221 1.1 bouyer return mincg;
222 1.1 bouyer }
223 1.1 bouyer
224 1.1 bouyer /*
225 1.1 bouyer * Select the desired position for the next block in a file. The file is
226 1.1 bouyer * logically divided into sections. The first section is composed of the
227 1.1 bouyer * direct blocks. Each additional section contains fs_maxbpg blocks.
228 1.1 bouyer *
229 1.1 bouyer * If no blocks have been allocated in the first section, the policy is to
230 1.1 bouyer * request a block in the same cylinder group as the inode that describes
231 1.1 bouyer * the file. Otherwise, the policy is to try to allocate the blocks
232 1.10 toshii * contigously. The two fields of the ext2 inode extension (see
233 1.1 bouyer * ufs/ufs/inode.h) help this.
234 1.1 bouyer */
235 1.3 fvdl ufs_daddr_t
236 1.1 bouyer ext2fs_blkpref(ip, lbn, indx, bap)
237 1.1 bouyer struct inode *ip;
238 1.3 fvdl ufs_daddr_t lbn;
239 1.1 bouyer int indx;
240 1.3 fvdl ufs_daddr_t *bap;
241 1.1 bouyer {
242 1.7 augustss struct m_ext2fs *fs;
243 1.7 augustss int cg, i;
244 1.1 bouyer
245 1.1 bouyer fs = ip->i_e2fs;
246 1.1 bouyer /*
247 1.1 bouyer * if we are doing contigous lbn allocation, try to alloc blocks
248 1.1 bouyer * contigously on disk
249 1.1 bouyer */
250 1.1 bouyer
251 1.1 bouyer if ( ip->i_e2fs_last_blk && lbn == ip->i_e2fs_last_lblk + 1) {
252 1.1 bouyer return ip->i_e2fs_last_blk + 1;
253 1.1 bouyer }
254 1.1 bouyer
255 1.1 bouyer /*
256 1.1 bouyer * bap, if provided, gives us a list of blocks to which we want to
257 1.1 bouyer * stay close
258 1.1 bouyer */
259 1.1 bouyer
260 1.1 bouyer if (bap) {
261 1.1 bouyer for (i = indx; i >= 0 ; i--) {
262 1.1 bouyer if (bap[i]) {
263 1.2 bouyer return fs2h32(bap[i]) + 1;
264 1.1 bouyer }
265 1.1 bouyer }
266 1.1 bouyer }
267 1.1 bouyer
268 1.1 bouyer /* fall back to the first block of the cylinder containing the inode */
269 1.1 bouyer
270 1.1 bouyer cg = ino_to_cg(fs, ip->i_number);
271 1.1 bouyer return fs->e2fs.e2fs_bpg * cg + fs->e2fs.e2fs_first_dblock + 1;
272 1.1 bouyer }
273 1.1 bouyer
274 1.1 bouyer /*
275 1.1 bouyer * Implement the cylinder overflow algorithm.
276 1.1 bouyer *
277 1.1 bouyer * The policy implemented by this algorithm is:
278 1.1 bouyer * 1) allocate the block in its requested cylinder group.
279 1.1 bouyer * 2) quadradically rehash on the cylinder group number.
280 1.1 bouyer * 3) brute force search for a free block.
281 1.1 bouyer */
282 1.1 bouyer static u_long
283 1.1 bouyer ext2fs_hashalloc(ip, cg, pref, size, allocator)
284 1.1 bouyer struct inode *ip;
285 1.1 bouyer int cg;
286 1.1 bouyer long pref;
287 1.1 bouyer int size; /* size for data blocks, mode for inodes */
288 1.3 fvdl ufs_daddr_t (*allocator) __P((struct inode *, int, ufs_daddr_t, int));
289 1.1 bouyer {
290 1.7 augustss struct m_ext2fs *fs;
291 1.1 bouyer long result;
292 1.1 bouyer int i, icg = cg;
293 1.1 bouyer
294 1.1 bouyer fs = ip->i_e2fs;
295 1.1 bouyer /*
296 1.1 bouyer * 1: preferred cylinder group
297 1.1 bouyer */
298 1.1 bouyer result = (*allocator)(ip, cg, pref, size);
299 1.1 bouyer if (result)
300 1.1 bouyer return (result);
301 1.1 bouyer /*
302 1.1 bouyer * 2: quadratic rehash
303 1.1 bouyer */
304 1.1 bouyer for (i = 1; i < fs->e2fs_ncg; i *= 2) {
305 1.1 bouyer cg += i;
306 1.1 bouyer if (cg >= fs->e2fs_ncg)
307 1.1 bouyer cg -= fs->e2fs_ncg;
308 1.1 bouyer result = (*allocator)(ip, cg, 0, size);
309 1.1 bouyer if (result)
310 1.1 bouyer return (result);
311 1.1 bouyer }
312 1.1 bouyer /*
313 1.1 bouyer * 3: brute force search
314 1.1 bouyer * Note that we start at i == 2, since 0 was checked initially,
315 1.1 bouyer * and 1 is always checked in the quadratic rehash.
316 1.1 bouyer */
317 1.1 bouyer cg = (icg + 2) % fs->e2fs_ncg;
318 1.1 bouyer for (i = 2; i < fs->e2fs_ncg; i++) {
319 1.1 bouyer result = (*allocator)(ip, cg, 0, size);
320 1.1 bouyer if (result)
321 1.1 bouyer return (result);
322 1.1 bouyer cg++;
323 1.1 bouyer if (cg == fs->e2fs_ncg)
324 1.1 bouyer cg = 0;
325 1.1 bouyer }
326 1.8 thorpej return (0);
327 1.1 bouyer }
328 1.1 bouyer
329 1.1 bouyer /*
330 1.1 bouyer * Determine whether a block can be allocated.
331 1.1 bouyer *
332 1.1 bouyer * Check to see if a block of the appropriate size is available,
333 1.1 bouyer * and if it is, allocate it.
334 1.1 bouyer */
335 1.1 bouyer
336 1.3 fvdl static ufs_daddr_t
337 1.1 bouyer ext2fs_alloccg(ip, cg, bpref, size)
338 1.1 bouyer struct inode *ip;
339 1.1 bouyer int cg;
340 1.3 fvdl ufs_daddr_t bpref;
341 1.1 bouyer int size;
342 1.1 bouyer {
343 1.7 augustss struct m_ext2fs *fs;
344 1.7 augustss char *bbp;
345 1.1 bouyer struct buf *bp;
346 1.1 bouyer int error, bno, start, end, loc;
347 1.1 bouyer
348 1.1 bouyer fs = ip->i_e2fs;
349 1.1 bouyer if (fs->e2fs_gd[cg].ext2bgd_nbfree == 0)
350 1.8 thorpej return (0);
351 1.6 bouyer error = bread(ip->i_devvp, fsbtodb(fs,
352 1.6 bouyer fs->e2fs_gd[cg].ext2bgd_b_bitmap),
353 1.1 bouyer (int)fs->e2fs_bsize, NOCRED, &bp);
354 1.1 bouyer if (error) {
355 1.1 bouyer brelse(bp);
356 1.8 thorpej return (0);
357 1.1 bouyer }
358 1.1 bouyer bbp = (char *)bp->b_data;
359 1.1 bouyer
360 1.1 bouyer if (dtog(fs, bpref) != cg)
361 1.1 bouyer bpref = 0;
362 1.1 bouyer if (bpref != 0) {
363 1.1 bouyer bpref = dtogd(fs, bpref);
364 1.1 bouyer /*
365 1.1 bouyer * if the requested block is available, use it
366 1.1 bouyer */
367 1.1 bouyer if (isclr(bbp, bpref)) {
368 1.1 bouyer bno = bpref;
369 1.1 bouyer goto gotit;
370 1.1 bouyer }
371 1.1 bouyer }
372 1.1 bouyer /*
373 1.1 bouyer * no blocks in the requested cylinder, so take next
374 1.1 bouyer * available one in this cylinder group.
375 1.1 bouyer * first try to get 8 contigous blocks, then fall back to a single
376 1.1 bouyer * block.
377 1.1 bouyer */
378 1.1 bouyer if (bpref)
379 1.1 bouyer start = dtogd(fs, bpref) / NBBY;
380 1.1 bouyer else
381 1.1 bouyer start = 0;
382 1.1 bouyer end = howmany(fs->e2fs.e2fs_fpg, NBBY) - start;
383 1.1 bouyer for (loc = start; loc < end; loc++) {
384 1.1 bouyer if (bbp[loc] == 0) {
385 1.1 bouyer bno = loc * NBBY;
386 1.1 bouyer goto gotit;
387 1.1 bouyer }
388 1.1 bouyer }
389 1.1 bouyer for (loc = 0; loc < start; loc++) {
390 1.1 bouyer if (bbp[loc] == 0) {
391 1.1 bouyer bno = loc * NBBY;
392 1.1 bouyer goto gotit;
393 1.1 bouyer }
394 1.1 bouyer }
395 1.1 bouyer
396 1.1 bouyer bno = ext2fs_mapsearch(fs, bbp, bpref);
397 1.1 bouyer if (bno < 0)
398 1.8 thorpej return (0);
399 1.1 bouyer gotit:
400 1.1 bouyer #ifdef DIAGNOSTIC
401 1.1 bouyer if (isset(bbp, (long)bno)) {
402 1.1 bouyer printf("ext2fs_alloccgblk: cg=%d bno=%d fs=%s\n",
403 1.1 bouyer cg, bno, fs->e2fs_fsmnt);
404 1.2 bouyer panic("ext2fs_alloccg: dup alloc");
405 1.1 bouyer }
406 1.1 bouyer #endif
407 1.1 bouyer setbit(bbp, (long)bno);
408 1.1 bouyer fs->e2fs.e2fs_fbcount--;
409 1.1 bouyer fs->e2fs_gd[cg].ext2bgd_nbfree--;
410 1.1 bouyer fs->e2fs_fmod = 1;
411 1.1 bouyer bdwrite(bp);
412 1.1 bouyer return (cg * fs->e2fs.e2fs_fpg + fs->e2fs.e2fs_first_dblock + bno);
413 1.1 bouyer }
414 1.1 bouyer
415 1.1 bouyer /*
416 1.1 bouyer * Determine whether an inode can be allocated.
417 1.1 bouyer *
418 1.1 bouyer * Check to see if an inode is available, and if it is,
419 1.1 bouyer * allocate it using the following policy:
420 1.1 bouyer * 1) allocate the requested inode.
421 1.1 bouyer * 2) allocate the next available inode after the requested
422 1.1 bouyer * inode in the specified cylinder group.
423 1.1 bouyer */
424 1.3 fvdl static ufs_daddr_t
425 1.1 bouyer ext2fs_nodealloccg(ip, cg, ipref, mode)
426 1.1 bouyer struct inode *ip;
427 1.1 bouyer int cg;
428 1.3 fvdl ufs_daddr_t ipref;
429 1.1 bouyer int mode;
430 1.1 bouyer {
431 1.7 augustss struct m_ext2fs *fs;
432 1.7 augustss char *ibp;
433 1.1 bouyer struct buf *bp;
434 1.1 bouyer int error, start, len, loc, map, i;
435 1.1 bouyer
436 1.1 bouyer ipref--; /* to avoid a lot of (ipref -1) */
437 1.1 bouyer fs = ip->i_e2fs;
438 1.1 bouyer if (fs->e2fs_gd[cg].ext2bgd_nifree == 0)
439 1.8 thorpej return (0);
440 1.6 bouyer error = bread(ip->i_devvp, fsbtodb(fs,
441 1.6 bouyer fs->e2fs_gd[cg].ext2bgd_i_bitmap),
442 1.1 bouyer (int)fs->e2fs_bsize, NOCRED, &bp);
443 1.1 bouyer if (error) {
444 1.1 bouyer brelse(bp);
445 1.8 thorpej return (0);
446 1.1 bouyer }
447 1.1 bouyer ibp = (char *)bp->b_data;
448 1.1 bouyer if (ipref) {
449 1.1 bouyer ipref %= fs->e2fs.e2fs_ipg;
450 1.1 bouyer if (isclr(ibp, ipref))
451 1.1 bouyer goto gotit;
452 1.1 bouyer }
453 1.1 bouyer start = ipref / NBBY;
454 1.1 bouyer len = howmany(fs->e2fs.e2fs_ipg - ipref, NBBY);
455 1.1 bouyer loc = skpc(0xff, len, &ibp[start]);
456 1.1 bouyer if (loc == 0) {
457 1.1 bouyer len = start + 1;
458 1.1 bouyer start = 0;
459 1.1 bouyer loc = skpc(0xff, len, &ibp[0]);
460 1.1 bouyer if (loc == 0) {
461 1.1 bouyer printf("cg = %d, ipref = %d, fs = %s\n",
462 1.1 bouyer cg, ipref, fs->e2fs_fsmnt);
463 1.1 bouyer panic("ext2fs_nodealloccg: map corrupted");
464 1.1 bouyer /* NOTREACHED */
465 1.1 bouyer }
466 1.1 bouyer }
467 1.1 bouyer i = start + len - loc;
468 1.1 bouyer map = ibp[i];
469 1.1 bouyer ipref = i * NBBY;
470 1.1 bouyer for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
471 1.1 bouyer if ((map & i) == 0) {
472 1.1 bouyer goto gotit;
473 1.1 bouyer }
474 1.1 bouyer }
475 1.1 bouyer printf("fs = %s\n", fs->e2fs_fsmnt);
476 1.1 bouyer panic("ext2fs_nodealloccg: block not in map");
477 1.1 bouyer /* NOTREACHED */
478 1.1 bouyer gotit:
479 1.1 bouyer setbit(ibp, ipref);
480 1.1 bouyer fs->e2fs.e2fs_ficount--;
481 1.1 bouyer fs->e2fs_gd[cg].ext2bgd_nifree--;
482 1.1 bouyer fs->e2fs_fmod = 1;
483 1.1 bouyer if ((mode & IFMT) == IFDIR) {
484 1.1 bouyer fs->e2fs_gd[cg].ext2bgd_ndirs++;
485 1.1 bouyer }
486 1.1 bouyer bdwrite(bp);
487 1.1 bouyer return (cg * fs->e2fs.e2fs_ipg + ipref +1);
488 1.1 bouyer }
489 1.1 bouyer
490 1.1 bouyer /*
491 1.1 bouyer * Free a block.
492 1.1 bouyer *
493 1.1 bouyer * The specified block is placed back in the
494 1.1 bouyer * free map.
495 1.1 bouyer */
496 1.1 bouyer void
497 1.1 bouyer ext2fs_blkfree(ip, bno)
498 1.7 augustss struct inode *ip;
499 1.3 fvdl ufs_daddr_t bno;
500 1.1 bouyer {
501 1.7 augustss struct m_ext2fs *fs;
502 1.7 augustss char *bbp;
503 1.1 bouyer struct buf *bp;
504 1.1 bouyer int error, cg;
505 1.1 bouyer
506 1.1 bouyer fs = ip->i_e2fs;
507 1.1 bouyer cg = dtog(fs, bno);
508 1.1 bouyer if ((u_int)bno >= fs->e2fs.e2fs_bcount) {
509 1.1 bouyer printf("bad block %d, ino %d\n", bno, ip->i_number);
510 1.1 bouyer ext2fs_fserr(fs, ip->i_e2fs_uid, "bad block");
511 1.1 bouyer return;
512 1.1 bouyer }
513 1.6 bouyer error = bread(ip->i_devvp,
514 1.6 bouyer fsbtodb(fs, fs->e2fs_gd[cg].ext2bgd_b_bitmap),
515 1.1 bouyer (int)fs->e2fs_bsize, NOCRED, &bp);
516 1.1 bouyer if (error) {
517 1.1 bouyer brelse(bp);
518 1.1 bouyer return;
519 1.1 bouyer }
520 1.1 bouyer bbp = (char *)bp->b_data;
521 1.1 bouyer bno = dtogd(fs, bno);
522 1.1 bouyer if (isclr(bbp, bno)) {
523 1.1 bouyer printf("dev = 0x%x, block = %d, fs = %s\n",
524 1.1 bouyer ip->i_dev, bno, fs->e2fs_fsmnt);
525 1.1 bouyer panic("blkfree: freeing free block");
526 1.1 bouyer }
527 1.1 bouyer clrbit(bbp, bno);
528 1.1 bouyer fs->e2fs.e2fs_fbcount++;
529 1.1 bouyer fs->e2fs_gd[cg].ext2bgd_nbfree++;
530 1.1 bouyer
531 1.1 bouyer fs->e2fs_fmod = 1;
532 1.1 bouyer bdwrite(bp);
533 1.1 bouyer }
534 1.1 bouyer
535 1.1 bouyer /*
536 1.1 bouyer * Free an inode.
537 1.1 bouyer *
538 1.1 bouyer * The specified inode is placed back in the free map.
539 1.1 bouyer */
540 1.1 bouyer int
541 1.1 bouyer ext2fs_vfree(v)
542 1.1 bouyer void *v;
543 1.1 bouyer {
544 1.1 bouyer struct vop_vfree_args /* {
545 1.1 bouyer struct vnode *a_pvp;
546 1.1 bouyer ino_t a_ino;
547 1.1 bouyer int a_mode;
548 1.1 bouyer } */ *ap = v;
549 1.7 augustss struct m_ext2fs *fs;
550 1.7 augustss char *ibp;
551 1.7 augustss struct inode *pip;
552 1.1 bouyer ino_t ino = ap->a_ino;
553 1.1 bouyer struct buf *bp;
554 1.1 bouyer int error, cg;
555 1.1 bouyer
556 1.1 bouyer pip = VTOI(ap->a_pvp);
557 1.1 bouyer fs = pip->i_e2fs;
558 1.1 bouyer if ((u_int)ino >= fs->e2fs.e2fs_icount || (u_int)ino < EXT2_FIRSTINO)
559 1.1 bouyer panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
560 1.1 bouyer pip->i_dev, ino, fs->e2fs_fsmnt);
561 1.1 bouyer cg = ino_to_cg(fs, ino);
562 1.6 bouyer error = bread(pip->i_devvp,
563 1.6 bouyer fsbtodb(fs, fs->e2fs_gd[cg].ext2bgd_i_bitmap),
564 1.1 bouyer (int)fs->e2fs_bsize, NOCRED, &bp);
565 1.1 bouyer if (error) {
566 1.1 bouyer brelse(bp);
567 1.1 bouyer return (0);
568 1.1 bouyer }
569 1.1 bouyer ibp = (char *)bp->b_data;
570 1.1 bouyer ino = (ino - 1) % fs->e2fs.e2fs_ipg;
571 1.1 bouyer if (isclr(ibp, ino)) {
572 1.1 bouyer printf("dev = 0x%x, ino = %d, fs = %s\n",
573 1.1 bouyer pip->i_dev, ino, fs->e2fs_fsmnt);
574 1.1 bouyer if (fs->e2fs_ronly == 0)
575 1.1 bouyer panic("ifree: freeing free inode");
576 1.1 bouyer }
577 1.1 bouyer clrbit(ibp, ino);
578 1.1 bouyer fs->e2fs.e2fs_ficount++;
579 1.1 bouyer fs->e2fs_gd[cg].ext2bgd_nifree++;
580 1.1 bouyer if ((ap->a_mode & IFMT) == IFDIR) {
581 1.1 bouyer fs->e2fs_gd[cg].ext2bgd_ndirs--;
582 1.1 bouyer }
583 1.1 bouyer fs->e2fs_fmod = 1;
584 1.1 bouyer bdwrite(bp);
585 1.1 bouyer return (0);
586 1.1 bouyer }
587 1.1 bouyer
588 1.1 bouyer /*
589 1.1 bouyer * Find a block in the specified cylinder group.
590 1.1 bouyer *
591 1.1 bouyer * It is a panic if a request is made to find a block if none are
592 1.1 bouyer * available.
593 1.1 bouyer */
594 1.1 bouyer
595 1.3 fvdl static ufs_daddr_t
596 1.1 bouyer ext2fs_mapsearch(fs, bbp, bpref)
597 1.7 augustss struct m_ext2fs *fs;
598 1.7 augustss char *bbp;
599 1.3 fvdl ufs_daddr_t bpref;
600 1.1 bouyer {
601 1.3 fvdl ufs_daddr_t bno;
602 1.1 bouyer int start, len, loc, i, map;
603 1.1 bouyer
604 1.1 bouyer /*
605 1.1 bouyer * find the fragment by searching through the free block
606 1.1 bouyer * map for an appropriate bit pattern
607 1.1 bouyer */
608 1.1 bouyer if (bpref)
609 1.1 bouyer start = dtogd(fs, bpref) / NBBY;
610 1.1 bouyer else
611 1.1 bouyer start = 0;
612 1.1 bouyer len = howmany(fs->e2fs.e2fs_fpg, NBBY) - start;
613 1.1 bouyer loc = skpc(0xff, len, &bbp[start]);
614 1.1 bouyer if (loc == 0) {
615 1.1 bouyer len = start + 1;
616 1.1 bouyer start = 0;
617 1.1 bouyer loc = skpc(0xff, len, &bbp[start]);
618 1.1 bouyer if (loc == 0) {
619 1.1 bouyer printf("start = %d, len = %d, fs = %s\n",
620 1.1 bouyer start, len, fs->e2fs_fsmnt);
621 1.1 bouyer panic("ext2fs_alloccg: map corrupted");
622 1.1 bouyer /* NOTREACHED */
623 1.1 bouyer }
624 1.1 bouyer }
625 1.1 bouyer i = start + len - loc;
626 1.1 bouyer map = bbp[i];
627 1.1 bouyer bno = i * NBBY;
628 1.1 bouyer for (i = 1; i < (1 << NBBY); i <<= 1, bno++) {
629 1.1 bouyer if ((map & i) == 0)
630 1.1 bouyer return (bno);
631 1.1 bouyer }
632 1.1 bouyer printf("fs = %s\n", fs->e2fs_fsmnt);
633 1.1 bouyer panic("ext2fs_mapsearch: block not in map");
634 1.1 bouyer /* NOTREACHED */
635 1.1 bouyer }
636 1.1 bouyer
637 1.1 bouyer /*
638 1.1 bouyer * Fserr prints the name of a file system with an error diagnostic.
639 1.1 bouyer *
640 1.1 bouyer * The form of the error message is:
641 1.1 bouyer * fs: error message
642 1.1 bouyer */
643 1.1 bouyer static void
644 1.1 bouyer ext2fs_fserr(fs, uid, cp)
645 1.1 bouyer struct m_ext2fs *fs;
646 1.1 bouyer u_int uid;
647 1.1 bouyer char *cp;
648 1.1 bouyer {
649 1.1 bouyer
650 1.1 bouyer log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->e2fs_fsmnt, cp);
651 1.1 bouyer }
652