delete VERBOSE #ifdef's
[unix-history] / usr / src / sys / ufs / ffs / ffs_alloc.c
CommitLineData
da7c5cc6 1/*
7188ac27 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
202a4bd9 3 * All rights reserved.
da7c5cc6 4 *
b702c21d 5 * %sccs.include.redist.c%
202a4bd9 6 *
5d4e84f3 7 * @(#)ffs_alloc.c 7.38 (Berkeley) %G%
da7c5cc6 8 */
e3fe2d69 9
6d3d8a1c
KB
10#include <sys/param.h>
11#include <sys/systm.h>
12#include <sys/buf.h>
13#include <sys/proc.h>
14#include <sys/vnode.h>
5d4e84f3 15#include <sys/mount.h>
6d3d8a1c
KB
16#include <sys/kernel.h>
17#include <sys/syslog.h>
c6f5111d 18
80e9de4e
KM
19#include <vm/vm.h>
20
6d3d8a1c
KB
21#include <ufs/ufs/quota.h>
22#include <ufs/ufs/inode.h>
e3fe2d69 23
6d3d8a1c
KB
24#include <ufs/ffs/fs.h>
25#include <ufs/ffs/ffs_extern.h>
26
27extern u_long nextgennumber;
28
29static daddr_t ffs_alloccg __P((struct inode *, int, daddr_t, int));
30static daddr_t ffs_alloccgblk __P((struct fs *, struct cg *, daddr_t));
31static ino_t ffs_dirpref __P((struct fs *));
32static daddr_t ffs_fragextend __P((struct inode *, int, long, int, int));
33static void ffs_fserr __P((struct fs *, u_int, char *));
34static u_long ffs_hashalloc
35 __P((struct inode *, int, long, int, u_long (*)()));
36static ino_t ffs_ialloccg __P((struct inode *, int, daddr_t, int));
37static daddr_t ffs_mapsearch __P((struct fs *, struct cg *, daddr_t, int));
e3fe2d69 38
502770a3
KM
39/*
40 * Allocate a block in the file system.
41 *
42 * The size of the requested block is given, which must be some
43 * multiple of fs_fsize and <= fs_bsize.
44 * A preference may be optionally specified. If a preference is given
45 * the following hierarchy is used to allocate a block:
46 * 1) allocate the requested block.
47 * 2) allocate a rotationally optimal block in the same cylinder.
48 * 3) allocate a block in the same cylinder group.
49 * 4) quadradically rehash into other cylinder groups, until an
50 * available block is located.
51 * If no block preference is given the following heirarchy is used
52 * to allocate a block:
53 * 1) allocate a block in the cylinder group that contains the
54 * inode for the file.
55 * 2) quadradically rehash into other cylinder groups, until an
56 * available block is located.
57 */
9438887a 58ffs_alloc(ip, lbn, bpref, size, cred, bnp)
f3c028b7 59 register struct inode *ip;
0c36425b 60 daddr_t lbn, bpref;
e3fe2d69 61 int size;
9438887a 62 struct ucred *cred;
0c36425b 63 daddr_t *bnp;
e3fe2d69
KM
64{
65 daddr_t bno;
66 register struct fs *fs;
f3c028b7 67 register struct buf *bp;
7188ac27 68 int cg, error;
e3fe2d69 69
0c36425b 70 *bnp = 0;
f7287e4b 71 fs = ip->i_fs;
9438887a 72#ifdef DIAGNOSTIC
ffd90e52
KM
73 if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
74 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
75 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
6d3d8a1c 76 panic("ffs_alloc: bad size");
ffd90e52 77 }
9438887a
KM
78 if (cred == NOCRED)
79 panic("ffs_alloc: missing credential\n");
80#endif /* DIAGNOSTIC */
b6407c9d 81 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
0947395d 82 goto nospace;
00bff950 83 if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
e3fe2d69 84 goto nospace;
b4567e9c 85#ifdef QUOTA
00bff950 86 if (error = chkdq(ip, (long)btodb(size), cred, 0))
7188ac27 87 return (error);
ca90a6bf 88#endif
260e5e3c
KM
89 if (bpref >= fs->fs_size)
90 bpref = 0;
e3fe2d69 91 if (bpref == 0)
6994bf5d 92 cg = itog(fs, ip->i_number);
e3fe2d69 93 else
6994bf5d 94 cg = dtog(fs, bpref);
6d3d8a1c
KB
95 bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size,
96 (u_long (*)())ffs_alloccg);
0c36425b
KM
97 if (bno > 0) {
98 ip->i_blocks += btodb(size);
99 ip->i_flag |= IUPD|ICHG;
100 *bnp = bno;
101 return (0);
102 }
8a403f0d
KM
103#ifdef QUOTA
104 /*
105 * Restore user's disk quota because allocation failed.
106 */
107 (void) chkdq(ip, (long)-btodb(size), cred, FORCE);
108#endif
e3fe2d69 109nospace:
6d3d8a1c 110 ffs_fserr(fs, cred->cr_uid, "file system full");
e3fe2d69 111 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
7188ac27 112 return (ENOSPC);
e3fe2d69
KM
113}
114
502770a3
KM
115/*
116 * Reallocate a fragment to a bigger size
117 *
118 * The number and size of the old block is given, and a preference
119 * and new size is also specified. The allocator attempts to extend
120 * the original block. Failing that, the regular block allocator is
121 * invoked to get an appropriate block.
122 */
9438887a 123ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp)
f7287e4b 124 register struct inode *ip;
31cd85c7 125 daddr_t lbprev;
0c36425b 126 daddr_t bpref;
07670f7d 127 int osize, nsize;
9438887a 128 struct ucred *cred;
7188ac27 129 struct buf **bpp;
07670f7d 130{
07670f7d 131 register struct fs *fs;
7188ac27 132 struct buf *bp, *obp;
8986c97c
KM
133 int cg, request, error;
134 daddr_t bprev, bno;
07670f7d 135
7188ac27 136 *bpp = 0;
f7287e4b 137 fs = ip->i_fs;
9438887a 138#ifdef DIAGNOSTIC
d995d89d 139 if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
ffd90e52 140 (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
6d3d8a1c
KB
141 printf(
142 "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
ffd90e52 143 ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
6d3d8a1c 144 panic("ffs_realloccg: bad size");
ffd90e52 145 }
9438887a
KM
146 if (cred == NOCRED)
147 panic("ffs_realloccg: missing credential\n");
148#endif /* DIAGNOSTIC */
00bff950 149 if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
0947395d 150 goto nospace;
0c36425b 151 if ((bprev = ip->i_db[lbprev]) == 0) {
ffd90e52
KM
152 printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
153 ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
6d3d8a1c 154 panic("ffs_realloccg: bad bprev");
ffd90e52 155 }
0c36425b
KM
156 /*
157 * Allocate the extra space in the buffer.
158 */
159 if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) {
160 brelse(bp);
161 return (error);
162 }
8a403f0d
KM
163#ifdef QUOTA
164 if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) {
165 brelse(bp);
166 return (error);
167 }
168#endif
0c36425b
KM
169 /*
170 * Check for extension in the existing location.
171 */
ae851115 172 cg = dtog(fs, bprev);
6d3d8a1c 173 if (bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) {
72ef728e 174 if (bp->b_blkno != fsbtodb(fs, bno))
0c36425b 175 panic("bad blockno");
2393702a
KM
176 ip->i_blocks += btodb(nsize - osize);
177 ip->i_flag |= IUPD|ICHG;
b3f3adbd
KM
178 allocbuf(bp, nsize);
179 bp->b_flags |= B_DONE;
180 bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
7188ac27
KM
181 *bpp = bp;
182 return (0);
f3c028b7 183 }
0c36425b
KM
184 /*
185 * Allocate a new disk location.
186 */
260e5e3c
KM
187 if (bpref >= fs->fs_size)
188 bpref = 0;
aec7dd3b 189 switch ((int)fs->fs_optim) {
f8484b5f
KM
190 case FS_OPTSPACE:
191 /*
192 * Allocate an exact sized fragment. Although this makes
193 * best use of space, we will waste time relocating it if
194 * the file continues to grow. If the fragmentation is
195 * less than half of the minimum free reserve, we choose
196 * to begin optimizing for time.
197 */
f7fa0c54 198 request = nsize;
f8484b5f
KM
199 if (fs->fs_minfree < 5 ||
200 fs->fs_cstotal.cs_nffree >
201 fs->fs_dsize * fs->fs_minfree / (2 * 100))
202 break;
203 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
204 fs->fs_fsmnt);
205 fs->fs_optim = FS_OPTTIME;
206 break;
207 case FS_OPTTIME:
208 /*
6d3d8a1c
KB
209 * At this point we have discovered a file that is trying to
210 * grow a small fragment to a larger fragment. To save time,
211 * we allocate a full sized block, then free the unused portion.
212 * If the file continues to grow, the `ffs_fragextend' call
213 * above will be able to grow it in place without further
214 * copying. If aberrant programs cause disk fragmentation to
215 * grow within 2% of the free reserve, we choose to begin
216 * optimizing for space.
f8484b5f 217 */
f7fa0c54 218 request = fs->fs_bsize;
f8484b5f
KM
219 if (fs->fs_cstotal.cs_nffree <
220 fs->fs_dsize * (fs->fs_minfree - 2) / 100)
221 break;
222 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
223 fs->fs_fsmnt);
224 fs->fs_optim = FS_OPTSPACE;
225 break;
226 default:
227 printf("dev = 0x%x, optim = %d, fs = %s\n",
228 ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
6d3d8a1c 229 panic("ffs_realloccg: bad optim");
f8484b5f
KM
230 /* NOTREACHED */
231 }
6d3d8a1c
KB
232 bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
233 (u_long (*)())ffs_alloccg);
6459ebe0 234 if (bno > 0) {
ec67a3ce
MK
235#ifdef SECSIZE
236 obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize,
237 fs->fs_dbsize);
238#else SECSIZE
ec67a3ce
MK
239 count = howmany(osize, CLBYTES);
240 for (i = 0; i < count; i++)
241#ifdef SECSIZE
242 munhash(ip->i_dev, bn + i * CLBYTES / fs->fs_dbsize);
243#else SECSIZE
244 munhash(ip->i_dev, bn + i * CLBYTES / DEV_BSIZE);
245#endif SECSIZE
31cd85c7 246 ffs_blkfree(ip, bprev, (long)osize);
f7fa0c54 247 if (nsize < request)
6d3d8a1c 248 ffs_blkfree(ip, bno + numfrags(fs, nsize),
31cd85c7 249 (long)(request - nsize));
2523b389
SL
250 ip->i_blocks += btodb(nsize - osize);
251 ip->i_flag |= IUPD|ICHG;
b3f3adbd
KM
252 allocbuf(bp, nsize);
253 bp->b_flags |= B_DONE;
254 bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
7188ac27
KM
255 *bpp = bp;
256 return (0);
f3c028b7 257 }
8a403f0d
KM
258#ifdef QUOTA
259 /*
260 * Restore user's disk quota because allocation failed.
261 */
262 (void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
263#endif
0c36425b 264 brelse(bp);
0947395d 265nospace:
f3c028b7
KM
266 /*
267 * no space available
268 */
6d3d8a1c 269 ffs_fserr(fs, cred->cr_uid, "file system full");
07670f7d 270 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
7188ac27 271 return (ENOSPC);
07670f7d
KM
272}
273
502770a3
KM
274/*
275 * Allocate an inode in the file system.
276 *
6d3d8a1c
KB
277 * If allocating a directory, use ffs_dirpref to select the inode.
278 * If allocating in a directory, the following hierarchy is followed:
279 * 1) allocate the preferred inode.
502770a3
KM
280 * 2) allocate an inode in the same cylinder group.
281 * 3) quadradically rehash into other cylinder groups, until an
282 * available inode is located.
283 * If no inode preference is given the following heirarchy is used
284 * to allocate an inode:
285 * 1) allocate an inode in cylinder group 0.
286 * 2) quadradically rehash into other cylinder groups, until an
287 * available inode is located.
288 */
5d4e84f3
KM
289ffs_valloc(ap)
290 struct vop_valloc_args /* {
291 struct vnode *a_pvp;
292 int a_mode;
293 struct ucred *a_cred;
294 struct vnode **a_vpp;
295 } */ *ap;
e3fe2d69 296{
406c9a0d 297 register struct vnode *pvp = ap->a_pvp;
a9013e03 298 register struct inode *pip;
e3fe2d69
KM
299 register struct fs *fs;
300 register struct inode *ip;
5d4e84f3 301 mode_t mode = ap->a_mode;
6d3d8a1c 302 ino_t ino, ipref;
7188ac27 303 int cg, error;
e3fe2d69 304
e1b76915 305 *ap->a_vpp = NULL;
406c9a0d 306 pip = VTOI(pvp);
f7287e4b 307 fs = pip->i_fs;
0947395d 308 if (fs->fs_cstotal.cs_nifree == 0)
e3fe2d69 309 goto noinodes;
6d3d8a1c 310
5d4e84f3 311 if ((mode & IFMT) == IFDIR)
a9013e03 312 ipref = ffs_dirpref(fs);
6d3d8a1c
KB
313 else
314 ipref = pip->i_number;
260e5e3c
KM
315 if (ipref >= fs->fs_ncg * fs->fs_ipg)
316 ipref = 0;
6994bf5d 317 cg = itog(fs, ipref);
5d4e84f3 318 ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_ialloccg);
e3fe2d69
KM
319 if (ino == 0)
320 goto noinodes;
5d4e84f3 321 error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp);
7188ac27 322 if (error) {
5d4e84f3 323 VOP_VFREE(pvp, ino, mode);
7188ac27 324 return (error);
e3fe2d69 325 }
e1b76915 326 ip = VTOI(*ap->a_vpp);
ffd90e52 327 if (ip->i_mode) {
5d4e84f3 328 printf("mode = 0%o, inum = %d, fs = %s\n",
ffd90e52 329 ip->i_mode, ip->i_number, fs->fs_fsmnt);
a9013e03 330 panic("ffs_valloc: dup alloc");
ffd90e52 331 }
2523b389
SL
332 if (ip->i_blocks) { /* XXX */
333 printf("free inode %s/%d had %d blocks\n",
334 fs->fs_fsmnt, ino, ip->i_blocks);
335 ip->i_blocks = 0;
336 }
7e0dee76 337 ip->i_flags = 0;
fb92d0ab
KM
338 /*
339 * Set up a new generation number for this inode.
340 */
341 if (++nextgennumber < (u_long)time.tv_sec)
342 nextgennumber = time.tv_sec;
343 ip->i_gen = nextgennumber;
7188ac27 344 return (0);
e3fe2d69 345noinodes:
e1b76915 346 ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes");
ae851115 347 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
7188ac27 348 return (ENOSPC);
e3fe2d69
KM
349}
350
743f1ef7 351/*
502770a3
KM
352 * Find a cylinder to place a directory.
353 *
354 * The policy implemented by this algorithm is to select from
355 * among those cylinder groups with above the average number of
356 * free inodes, the one with the smallest number of directories.
743f1ef7 357 */
6d3d8a1c
KB
358static ino_t
359ffs_dirpref(fs)
e3fe2d69 360 register struct fs *fs;
f7287e4b 361{
743f1ef7 362 int cg, minndir, mincg, avgifree;
e3fe2d69 363
0947395d 364 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
743f1ef7 365 minndir = fs->fs_ipg;
e3fe2d69 366 mincg = 0;
743f1ef7 367 for (cg = 0; cg < fs->fs_ncg; cg++)
b6407c9d
KM
368 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
369 fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
e3fe2d69 370 mincg = cg;
b6407c9d 371 minndir = fs->fs_cs(fs, cg).cs_ndir;
e3fe2d69 372 }
4f083fd7 373 return ((ino_t)(fs->fs_ipg * mincg));
e3fe2d69
KM
374}
375
743f1ef7 376/*
4f083fd7
SL
377 * Select the desired position for the next block in a file. The file is
378 * logically divided into sections. The first section is composed of the
379 * direct blocks. Each additional section contains fs_maxbpg blocks.
380 *
381 * If no blocks have been allocated in the first section, the policy is to
382 * request a block in the same cylinder group as the inode that describes
383 * the file. If no blocks have been allocated in any other section, the
384 * policy is to place the section in a cylinder group with a greater than
385 * average number of free blocks. An appropriate cylinder group is found
16e7863f
KM
386 * by using a rotor that sweeps the cylinder groups. When a new group of
387 * blocks is needed, the sweep begins in the cylinder group following the
388 * cylinder group from which the previous allocation was made. The sweep
389 * continues until a cylinder group with greater than the average number
390 * of free blocks is found. If the allocation is for the first block in an
391 * indirect block, the information on the previous allocation is unavailable;
392 * here a best guess is made based upon the logical block number being
393 * allocated.
4f083fd7
SL
394 *
395 * If a section is already partially allocated, the policy is to
396 * contiguously allocate fs_maxcontig blocks. The end of one of these
397 * contiguous blocks and the beginning of the next is physically separated
398 * so that the disk head will be in transit between them for at least
399 * fs_rotdelay milliseconds. This is to allow time for the processor to
400 * schedule another I/O transfer.
743f1ef7 401 */
daaf7bee 402daddr_t
6d3d8a1c 403ffs_blkpref(ip, lbn, indx, bap)
4f083fd7
SL
404 struct inode *ip;
405 daddr_t lbn;
406 int indx;
407 daddr_t *bap;
f7287e4b 408{
4f083fd7 409 register struct fs *fs;
16e7863f
KM
410 register int cg;
411 int avgbfree, startcg;
4f083fd7 412 daddr_t nextblk;
743f1ef7 413
4f083fd7
SL
414 fs = ip->i_fs;
415 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
416 if (lbn < NDADDR) {
417 cg = itog(fs, ip->i_number);
b6407c9d 418 return (fs->fs_fpg * cg + fs->fs_frag);
743f1ef7 419 }
4f083fd7
SL
420 /*
421 * Find a cylinder with greater than average number of
422 * unused data blocks.
423 */
16e7863f
KM
424 if (indx == 0 || bap[indx - 1] == 0)
425 startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
426 else
427 startcg = dtog(fs, bap[indx - 1]) + 1;
428 startcg %= fs->fs_ncg;
4f083fd7 429 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
16e7863f 430 for (cg = startcg; cg < fs->fs_ncg; cg++)
4f083fd7
SL
431 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
432 fs->fs_cgrotor = cg;
433 return (fs->fs_fpg * cg + fs->fs_frag);
434 }
16e7863f 435 for (cg = 0; cg <= startcg; cg++)
4f083fd7
SL
436 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
437 fs->fs_cgrotor = cg;
438 return (fs->fs_fpg * cg + fs->fs_frag);
439 }
440 return (NULL);
441 }
442 /*
443 * One or more previous blocks have been laid out. If less
444 * than fs_maxcontig previous blocks are contiguous, the
445 * next block is requested contiguously, otherwise it is
446 * requested rotationally delayed by fs_rotdelay milliseconds.
447 */
448 nextblk = bap[indx - 1] + fs->fs_frag;
449 if (indx > fs->fs_maxcontig &&
240a4664 450 bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
4f083fd7
SL
451 != nextblk)
452 return (nextblk);
453 if (fs->fs_rotdelay != 0)
454 /*
455 * Here we convert ms of delay to frags as:
456 * (frags) = (ms) * (rev/sec) * (sect/rev) /
457 * ((sect/frag) * (ms/sec))
458 * then round up to the next block.
459 */
460 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
461 (NSPF(fs) * 1000), fs->fs_frag);
462 return (nextblk);
743f1ef7
KM
463}
464
502770a3
KM
465/*
466 * Implement the cylinder overflow algorithm.
467 *
468 * The policy implemented by this algorithm is:
469 * 1) allocate the block in its requested cylinder group.
470 * 2) quadradically rehash on the cylinder group number.
471 * 3) brute force search for a free block.
472 */
daaf7bee 473/*VARARGS5*/
6d3d8a1c
KB
474static u_long
475ffs_hashalloc(ip, cg, pref, size, allocator)
f7287e4b 476 struct inode *ip;
e3fe2d69
KM
477 int cg;
478 long pref;
479 int size; /* size for data blocks, mode for inodes */
daaf7bee 480 u_long (*allocator)();
e3fe2d69 481{
f7287e4b 482 register struct fs *fs;
e3fe2d69
KM
483 long result;
484 int i, icg = cg;
485
f7287e4b 486 fs = ip->i_fs;
e3fe2d69
KM
487 /*
488 * 1: preferred cylinder group
489 */
f7287e4b 490 result = (*allocator)(ip, cg, pref, size);
e3fe2d69
KM
491 if (result)
492 return (result);
493 /*
494 * 2: quadratic rehash
495 */
496 for (i = 1; i < fs->fs_ncg; i *= 2) {
497 cg += i;
498 if (cg >= fs->fs_ncg)
499 cg -= fs->fs_ncg;
f7287e4b 500 result = (*allocator)(ip, cg, 0, size);
e3fe2d69
KM
501 if (result)
502 return (result);
503 }
504 /*
505 * 3: brute force search
620b3290
SL
506 * Note that we start at i == 2, since 0 was checked initially,
507 * and 1 is always checked in the quadratic rehash.
e3fe2d69 508 */
2136305e 509 cg = (icg + 2) % fs->fs_ncg;
620b3290 510 for (i = 2; i < fs->fs_ncg; i++) {
f7287e4b 511 result = (*allocator)(ip, cg, 0, size);
e3fe2d69
KM
512 if (result)
513 return (result);
514 cg++;
515 if (cg == fs->fs_ncg)
516 cg = 0;
517 }
ae851115 518 return (NULL);
e3fe2d69
KM
519}
520
502770a3
KM
521/*
522 * Determine whether a fragment can be extended.
523 *
524 * Check to see if the necessary fragments are available, and
525 * if they are, allocate them.
526 */
6d3d8a1c
KB
527static daddr_t
528ffs_fragextend(ip, cg, bprev, osize, nsize)
f7287e4b 529 struct inode *ip;
07670f7d 530 int cg;
f3c028b7 531 long bprev;
07670f7d
KM
532 int osize, nsize;
533{
f7287e4b 534 register struct fs *fs;
f3c028b7 535 register struct cg *cgp;
7188ac27 536 struct buf *bp;
f3c028b7
KM
537 long bno;
538 int frags, bbase;
7188ac27 539 int i, error;
07670f7d 540
f7287e4b 541 fs = ip->i_fs;
a8580723 542 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
e5476900 543 return (NULL);
d995d89d 544 frags = numfrags(fs, nsize);
a8580723
KM
545 bbase = fragnum(fs, bprev);
546 if (bbase > fragnum(fs, (bprev + frags - 1))) {
ec67a3ce 547 /* cannot extend across a block boundary */
ae851115 548 return (NULL);
f3c028b7 549 }
ec67a3ce
MK
550#ifdef SECSIZE
551 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
552 fs->fs_dbsize);
553#else SECSIZE
7188ac27 554 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
a937f856 555 (int)fs->fs_cgsize, NOCRED, &bp);
7188ac27
KM
556 if (error) {
557 brelse(bp);
558 return (NULL);
559 }
ec67a3ce 560#endif SECSIZE
e5476900 561 cgp = bp->b_un.b_cg;
7188ac27 562 if (!cg_chkmagic(cgp)) {
d995d89d 563 brelse(bp);
ae851115 564 return (NULL);
d995d89d 565 }
95d0807c 566 cgp->cg_time = time.tv_sec;
6994bf5d 567 bno = dtogd(fs, bprev);
d995d89d 568 for (i = numfrags(fs, osize); i < frags; i++)
10adeb11 569 if (isclr(cg_blksfree(cgp), bno + i)) {
aca50d72 570 brelse(bp);
ae851115 571 return (NULL);
aca50d72
KM
572 }
573 /*
574 * the current fragment can be extended
575 * deduct the count on fragment being extended into
576 * increase the count on the remaining fragment (if any)
577 * allocate the extended piece
578 */
579 for (i = frags; i < fs->fs_frag - bbase; i++)
10adeb11 580 if (isclr(cg_blksfree(cgp), bno + i))
f3c028b7 581 break;
d995d89d 582 cgp->cg_frsum[i - numfrags(fs, osize)]--;
aca50d72
KM
583 if (i != frags)
584 cgp->cg_frsum[i - frags]++;
d995d89d 585 for (i = numfrags(fs, osize); i < frags; i++) {
10adeb11 586 clrbit(cg_blksfree(cgp), bno + i);
aca50d72
KM
587 cgp->cg_cs.cs_nffree--;
588 fs->fs_cstotal.cs_nffree--;
589 fs->fs_cs(fs, cg).cs_nffree--;
f3c028b7 590 }
e809642a 591 fs->fs_fmod = 1;
aca50d72
KM
592 bdwrite(bp);
593 return (bprev);
07670f7d
KM
594}
595
502770a3
KM
596/*
597 * Determine whether a block can be allocated.
598 *
599 * Check to see if a block of the apprpriate size is available,
600 * and if it is, allocate it.
601 */
cd434177 602static daddr_t
6d3d8a1c 603ffs_alloccg(ip, cg, bpref, size)
f7287e4b 604 struct inode *ip;
e3fe2d69
KM
605 int cg;
606 daddr_t bpref;
607 int size;
608{
f7287e4b 609 register struct fs *fs;
f3c028b7 610 register struct cg *cgp;
7188ac27 611 struct buf *bp;
f3c028b7 612 register int i;
7188ac27 613 int error, bno, frags, allocsiz;
e3fe2d69 614
f7287e4b 615 fs = ip->i_fs;
b6407c9d 616 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
ae851115 617 return (NULL);
ec67a3ce
MK
618#ifdef SECSIZE
619 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
620 fs->fs_dbsize);
621#else SECSIZE
7188ac27 622 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
a937f856 623 (int)fs->fs_cgsize, NOCRED, &bp);
7188ac27
KM
624 if (error) {
625 brelse(bp);
626 return (NULL);
627 }
ec67a3ce 628#endif SECSIZE
e5476900 629 cgp = bp->b_un.b_cg;
7188ac27 630 if (!cg_chkmagic(cgp) ||
0f538882 631 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
d995d89d 632 brelse(bp);
ae851115 633 return (NULL);
d995d89d 634 }
95d0807c 635 cgp->cg_time = time.tv_sec;
b6407c9d 636 if (size == fs->fs_bsize) {
6d3d8a1c 637 bno = ffs_alloccgblk(fs, cgp, bpref);
f3c028b7
KM
638 bdwrite(bp);
639 return (bno);
640 }
641 /*
642 * check to see if any fragments are already available
643 * allocsiz is the size which will be allocated, hacking
644 * it down to a smaller size if necessary
645 */
d995d89d 646 frags = numfrags(fs, size);
b6407c9d 647 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
f3c028b7
KM
648 if (cgp->cg_frsum[allocsiz] != 0)
649 break;
b6407c9d 650 if (allocsiz == fs->fs_frag) {
f3c028b7
KM
651 /*
652 * no fragments were available, so a block will be
653 * allocated, and hacked up
654 */
0947395d 655 if (cgp->cg_cs.cs_nbfree == 0) {
f3c028b7 656 brelse(bp);
ae851115 657 return (NULL);
f3c028b7 658 }
6d3d8a1c 659 bno = ffs_alloccgblk(fs, cgp, bpref);
6994bf5d 660 bpref = dtogd(fs, bno);
b6407c9d 661 for (i = frags; i < fs->fs_frag; i++)
10adeb11 662 setbit(cg_blksfree(cgp), bpref + i);
b6407c9d 663 i = fs->fs_frag - frags;
0947395d
KM
664 cgp->cg_cs.cs_nffree += i;
665 fs->fs_cstotal.cs_nffree += i;
b6407c9d 666 fs->fs_cs(fs, cg).cs_nffree += i;
e809642a 667 fs->fs_fmod = 1;
f3c028b7
KM
668 cgp->cg_frsum[i]++;
669 bdwrite(bp);
670 return (bno);
671 }
6d3d8a1c 672 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
0f538882
KM
673 if (bno < 0) {
674 brelse(bp);
ae851115 675 return (NULL);
0f538882 676 }
f3c028b7 677 for (i = 0; i < frags; i++)
10adeb11 678 clrbit(cg_blksfree(cgp), bno + i);
0947395d
KM
679 cgp->cg_cs.cs_nffree -= frags;
680 fs->fs_cstotal.cs_nffree -= frags;
b6407c9d 681 fs->fs_cs(fs, cg).cs_nffree -= frags;
e809642a 682 fs->fs_fmod = 1;
f3c028b7
KM
683 cgp->cg_frsum[allocsiz]--;
684 if (frags != allocsiz)
685 cgp->cg_frsum[allocsiz - frags]++;
686 bdwrite(bp);
687 return (cg * fs->fs_fpg + bno);
688}
689
502770a3
KM
690/*
691 * Allocate a block in a cylinder group.
692 *
693 * This algorithm implements the following policy:
694 * 1) allocate the requested block.
695 * 2) allocate a rotationally optimal block in the same cylinder.
696 * 3) allocate the next available block on the block rotor for the
697 * specified cylinder group.
698 * Note that this routine only allocates fs_bsize blocks; these
699 * blocks may be fragmented by the routine that allocates them.
700 */
6d3d8a1c
KB
701static daddr_t
702ffs_alloccgblk(fs, cgp, bpref)
f7287e4b 703 register struct fs *fs;
f3c028b7
KM
704 register struct cg *cgp;
705 daddr_t bpref;
706{
743f1ef7 707 daddr_t bno;
ae851115 708 int cylno, pos, delta;
743f1ef7 709 short *cylbp;
aca50d72 710 register int i;
f3c028b7 711
743f1ef7
KM
712 if (bpref == 0) {
713 bpref = cgp->cg_rotor;
aca50d72
KM
714 goto norot;
715 }
a8580723 716 bpref = blknum(fs, bpref);
6994bf5d 717 bpref = dtogd(fs, bpref);
aca50d72
KM
718 /*
719 * if the requested block is available, use it
720 */
6d3d8a1c 721 if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
aca50d72
KM
722 bno = bpref;
723 goto gotit;
724 }
aca50d72
KM
725 /*
726 * check for a block available on the same cylinder
aca50d72
KM
727 */
728 cylno = cbtocylno(fs, bpref);
10adeb11 729 if (cg_blktot(cgp)[cylno] == 0)
502770a3
KM
730 goto norot;
731 if (fs->fs_cpc == 0) {
732 /*
733 * block layout info is not available, so just have
734 * to take any block in this cylinder.
735 */
736 bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
737 goto norot;
738 }
aca50d72
KM
739 /*
740 * check the summary information to see if a block is
741 * available in the requested cylinder starting at the
4f083fd7 742 * requested rotational position and proceeding around.
aca50d72 743 */
10adeb11 744 cylbp = cg_blks(fs, cgp, cylno);
4f083fd7 745 pos = cbtorpos(fs, bpref);
10adeb11 746 for (i = pos; i < fs->fs_nrpos; i++)
aca50d72
KM
747 if (cylbp[i] > 0)
748 break;
10adeb11 749 if (i == fs->fs_nrpos)
aca50d72 750 for (i = 0; i < pos; i++)
743f1ef7
KM
751 if (cylbp[i] > 0)
752 break;
aca50d72
KM
753 if (cylbp[i] > 0) {
754 /*
755 * found a rotational position, now find the actual
756 * block. A panic if none is actually there.
757 */
758 pos = cylno % fs->fs_cpc;
759 bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
10adeb11 760 if (fs_postbl(fs, pos)[i] == -1) {
ffd90e52
KM
761 printf("pos = %d, i = %d, fs = %s\n",
762 pos, i, fs->fs_fsmnt);
6d3d8a1c 763 panic("ffs_alloccgblk: cyl groups corrupted");
ffd90e52 764 }
10adeb11 765 for (i = fs_postbl(fs, pos)[i];; ) {
6d3d8a1c 766 if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) {
240a4664 767 bno = blkstofrags(fs, (bno + i));
aca50d72 768 goto gotit;
743f1ef7 769 }
10adeb11
KM
770 delta = fs_rotbl(fs)[i];
771 if (delta <= 0 ||
772 delta + i > fragstoblks(fs, fs->fs_fpg))
aca50d72 773 break;
ae851115 774 i += delta;
743f1ef7 775 }
ffd90e52 776 printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
6d3d8a1c 777 panic("ffs_alloccgblk: can't find blk in cyl");
e3fe2d69 778 }
aca50d72
KM
779norot:
780 /*
781 * no blocks in the requested cylinder, so take next
782 * available one in this cylinder group.
783 */
6d3d8a1c 784 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
6459ebe0 785 if (bno < 0)
ae851115 786 return (NULL);
743f1ef7 787 cgp->cg_rotor = bno;
e3fe2d69 788gotit:
6d3d8a1c 789 ffs_clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno));
0947395d
KM
790 cgp->cg_cs.cs_nbfree--;
791 fs->fs_cstotal.cs_nbfree--;
b6407c9d 792 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
502770a3 793 cylno = cbtocylno(fs, bno);
10adeb11
KM
794 cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
795 cg_blktot(cgp)[cylno]--;
e809642a 796 fs->fs_fmod = 1;
743f1ef7 797 return (cgp->cg_cgx * fs->fs_fpg + bno);
e3fe2d69 798}
10adeb11 799
502770a3
KM
800/*
801 * Determine whether an inode can be allocated.
802 *
803 * Check to see if an inode is available, and if it is,
804 * allocate it using the following policy:
805 * 1) allocate the requested inode.
806 * 2) allocate the next available inode after the requested
807 * inode in the specified cylinder group.
808 */
6d3d8a1c
KB
809static ino_t
810ffs_ialloccg(ip, cg, ipref, mode)
f7287e4b 811 struct inode *ip;
e3fe2d69
KM
812 int cg;
813 daddr_t ipref;
814 int mode;
815{
f7287e4b 816 register struct fs *fs;
f3c028b7 817 register struct cg *cgp;
4e0c7b8a 818 struct buf *bp;
7188ac27 819 int error, start, len, loc, map, i;
e3fe2d69 820
f7287e4b 821 fs = ip->i_fs;
b6407c9d 822 if (fs->fs_cs(fs, cg).cs_nifree == 0)
ae851115 823 return (NULL);
ec67a3ce
MK
824#ifdef SECSIZE
825 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
826 fs->fs_dbsize);
827#else SECSIZE
7188ac27 828 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
a937f856 829 (int)fs->fs_cgsize, NOCRED, &bp);
7188ac27
KM
830 if (error) {
831 brelse(bp);
832 return (NULL);
833 }
ec67a3ce 834#endif SECSIZE
e5476900 835 cgp = bp->b_un.b_cg;
7188ac27 836 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
d995d89d 837 brelse(bp);
ae851115 838 return (NULL);
d995d89d 839 }
95d0807c 840 cgp->cg_time = time.tv_sec;
e3fe2d69
KM
841 if (ipref) {
842 ipref %= fs->fs_ipg;
10adeb11 843 if (isclr(cg_inosused(cgp), ipref))
e3fe2d69 844 goto gotit;
4e0c7b8a
KM
845 }
846 start = cgp->cg_irotor / NBBY;
847 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
10adeb11 848 loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
4e0c7b8a 849 if (loc == 0) {
e5889092
KM
850 len = start + 1;
851 start = 0;
10adeb11 852 loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
e5889092
KM
853 if (loc == 0) {
854 printf("cg = %s, irotor = %d, fs = %s\n",
855 cg, cgp->cg_irotor, fs->fs_fsmnt);
6d3d8a1c 856 panic("ffs_ialloccg: map corrupted");
e5889092
KM
857 /* NOTREACHED */
858 }
4e0c7b8a
KM
859 }
860 i = start + len - loc;
10adeb11 861 map = cg_inosused(cgp)[i];
4e0c7b8a
KM
862 ipref = i * NBBY;
863 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
864 if ((map & i) == 0) {
e3fe2d69
KM
865 cgp->cg_irotor = ipref;
866 goto gotit;
867 }
868 }
4e0c7b8a 869 printf("fs = %s\n", fs->fs_fsmnt);
6d3d8a1c 870 panic("ffs_ialloccg: block not in map");
4e0c7b8a 871 /* NOTREACHED */
e3fe2d69 872gotit:
10adeb11 873 setbit(cg_inosused(cgp), ipref);
0947395d
KM
874 cgp->cg_cs.cs_nifree--;
875 fs->fs_cstotal.cs_nifree--;
b6407c9d 876 fs->fs_cs(fs, cg).cs_nifree--;
e809642a 877 fs->fs_fmod = 1;
e3fe2d69 878 if ((mode & IFMT) == IFDIR) {
0947395d
KM
879 cgp->cg_cs.cs_ndir++;
880 fs->fs_cstotal.cs_ndir++;
b6407c9d 881 fs->fs_cs(fs, cg).cs_ndir++;
e3fe2d69
KM
882 }
883 bdwrite(bp);
884 return (cg * fs->fs_ipg + ipref);
885}
886
502770a3
KM
887/*
888 * Free a block or fragment.
889 *
890 * The specified block or fragment is placed back in the
891 * free map. If a fragment is deallocated, a possible
892 * block reassembly is checked.
893 */
6d3d8a1c 894ffs_blkfree(ip, bno, size)
f7287e4b 895 register struct inode *ip;
e3fe2d69 896 daddr_t bno;
31cd85c7 897 long size;
e3fe2d69
KM
898{
899 register struct fs *fs;
900 register struct cg *cgp;
7188ac27
KM
901 struct buf *bp;
902 int error, cg, blk, frags, bbase;
f3c028b7 903 register int i;
e3fe2d69 904
f7287e4b 905 fs = ip->i_fs;
ffd90e52
KM
906 if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
907 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
908 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
ced3a252 909 panic("blkfree: bad size");
ffd90e52 910 }
6994bf5d 911 cg = dtog(fs, bno);
e2546752 912 if ((unsigned)bno >= fs->fs_size) {
6459ebe0 913 printf("bad block %d, ino %d\n", bno, ip->i_number);
9438887a 914 ffs_fserr(fs, ip->i_uid, "bad block");
e3fe2d69 915 return;
6459ebe0 916 }
ec67a3ce
MK
917#ifdef SECSIZE
918 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
919 fs->fs_dbsize);
920#else SECSIZE
7188ac27 921 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
a937f856 922 (int)fs->fs_cgsize, NOCRED, &bp);
7188ac27
KM
923 if (error) {
924 brelse(bp);
925 return;
926 }
ec67a3ce 927#endif SECSIZE
e5476900 928 cgp = bp->b_un.b_cg;
7188ac27 929 if (!cg_chkmagic(cgp)) {
d995d89d 930 brelse(bp);
e3fe2d69 931 return;
d995d89d 932 }
95d0807c 933 cgp->cg_time = time.tv_sec;
6994bf5d 934 bno = dtogd(fs, bno);
b6407c9d 935 if (size == fs->fs_bsize) {
6d3d8a1c 936 if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) {
ffd90e52
KM
937 printf("dev = 0x%x, block = %d, fs = %s\n",
938 ip->i_dev, bno, fs->fs_fsmnt);
ced3a252 939 panic("blkfree: freeing free block");
6459ebe0 940 }
6d3d8a1c 941 ffs_setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno));
0947395d
KM
942 cgp->cg_cs.cs_nbfree++;
943 fs->fs_cstotal.cs_nbfree++;
b6407c9d 944 fs->fs_cs(fs, cg).cs_nbfree++;
502770a3 945 i = cbtocylno(fs, bno);
10adeb11
KM
946 cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
947 cg_blktot(cgp)[i]++;
07670f7d 948 } else {
a8580723 949 bbase = bno - fragnum(fs, bno);
f3c028b7
KM
950 /*
951 * decrement the counts associated with the old frags
952 */
10adeb11 953 blk = blkmap(fs, cg_blksfree(cgp), bbase);
6d3d8a1c 954 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
f3c028b7
KM
955 /*
956 * deallocate the fragment
957 */
d995d89d 958 frags = numfrags(fs, size);
f3c028b7 959 for (i = 0; i < frags; i++) {
10adeb11 960 if (isset(cg_blksfree(cgp), bno + i)) {
ffd90e52
KM
961 printf("dev = 0x%x, block = %d, fs = %s\n",
962 ip->i_dev, bno + i, fs->fs_fsmnt);
ced3a252 963 panic("blkfree: freeing free frag");
ffd90e52 964 }
10adeb11 965 setbit(cg_blksfree(cgp), bno + i);
07670f7d 966 }
ae851115
KM
967 cgp->cg_cs.cs_nffree += i;
968 fs->fs_cstotal.cs_nffree += i;
969 fs->fs_cs(fs, cg).cs_nffree += i;
f3c028b7
KM
970 /*
971 * add back in counts associated with the new frags
972 */
10adeb11 973 blk = blkmap(fs, cg_blksfree(cgp), bbase);
6d3d8a1c 974 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
f3c028b7
KM
975 /*
976 * if a complete block has been reassembled, account for it
977 */
6d3d8a1c 978 if (ffs_isblock(fs, cg_blksfree(cgp),
9523729e 979 (daddr_t)fragstoblks(fs, bbase))) {
b6407c9d
KM
980 cgp->cg_cs.cs_nffree -= fs->fs_frag;
981 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
982 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
0947395d
KM
983 cgp->cg_cs.cs_nbfree++;
984 fs->fs_cstotal.cs_nbfree++;
b6407c9d 985 fs->fs_cs(fs, cg).cs_nbfree++;
502770a3 986 i = cbtocylno(fs, bbase);
10adeb11
KM
987 cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
988 cg_blktot(cgp)[i]++;
07670f7d
KM
989 }
990 }
e809642a 991 fs->fs_fmod = 1;
e3fe2d69
KM
992 bdwrite(bp);
993}
994
502770a3
KM
995/*
996 * Free an inode.
997 *
998 * The specified inode is placed back in the free map.
999 */
4e2f97fb 1000int
5d4e84f3
KM
1001ffs_vfree(ap)
1002 struct vop_vfree_args /* {
1003 struct vnode *a_pvp;
1004 ino_t a_ino;
1005 int a_mode;
1006 } */ *ap;
e3fe2d69
KM
1007{
1008 register struct fs *fs;
1009 register struct cg *cgp;
a9013e03 1010 register struct inode *pip;
5d4e84f3 1011 ino_t ino = ap->a_ino;
7188ac27
KM
1012 struct buf *bp;
1013 int error, cg;
e3fe2d69 1014
e1b76915 1015 pip = VTOI(ap->a_pvp);
6d3d8a1c 1016 fs = pip->i_fs;
5d4e84f3
KM
1017 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1018 panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
1019 pip->i_dev, ino, fs->fs_fsmnt);
1020 cg = itog(fs, ino);
ec67a3ce
MK
1021#ifdef SECSIZE
1022 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1023 fs->fs_dbsize);
1024#else SECSIZE
6d3d8a1c 1025 error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
a937f856 1026 (int)fs->fs_cgsize, NOCRED, &bp);
7188ac27
KM
1027 if (error) {
1028 brelse(bp);
4e2f97fb 1029 return (0);
7188ac27 1030 }
ec67a3ce 1031#endif SECSIZE
e5476900 1032 cgp = bp->b_un.b_cg;
7188ac27 1033 if (!cg_chkmagic(cgp)) {
d995d89d 1034 brelse(bp);
4e2f97fb 1035 return (0);
d995d89d 1036 }
95d0807c 1037 cgp->cg_time = time.tv_sec;
5d4e84f3
KM
1038 ino %= fs->fs_ipg;
1039 if (isclr(cg_inosused(cgp), ino)) {
1040 printf("dev = 0x%x, ino = %d, fs = %s\n",
1041 pip->i_dev, ino, fs->fs_fsmnt);
b9ccf514
KM
1042 if (fs->fs_ronly == 0)
1043 panic("ifree: freeing free inode");
ffd90e52 1044 }
5d4e84f3
KM
1045 clrbit(cg_inosused(cgp), ino);
1046 if (ino < cgp->cg_irotor)
1047 cgp->cg_irotor = ino;
0947395d
KM
1048 cgp->cg_cs.cs_nifree++;
1049 fs->fs_cstotal.cs_nifree++;
b6407c9d 1050 fs->fs_cs(fs, cg).cs_nifree++;
e1b76915 1051 if ((ap->a_mode & IFMT) == IFDIR) {
0947395d
KM
1052 cgp->cg_cs.cs_ndir--;
1053 fs->fs_cstotal.cs_ndir--;
b6407c9d 1054 fs->fs_cs(fs, cg).cs_ndir--;
e3fe2d69 1055 }
e809642a 1056 fs->fs_fmod = 1;
e3fe2d69 1057 bdwrite(bp);
4e2f97fb 1058 return (0);
e3fe2d69
KM
1059}
1060
743f1ef7 1061/*
502770a3
KM
1062 * Find a block of the specified size in the specified cylinder group.
1063 *
743f1ef7
KM
1064 * It is a panic if a request is made to find a block if none are
1065 * available.
1066 */
6d3d8a1c
KB
1067static daddr_t
1068ffs_mapsearch(fs, cgp, bpref, allocsiz)
743f1ef7
KM
1069 register struct fs *fs;
1070 register struct cg *cgp;
1071 daddr_t bpref;
1072 int allocsiz;
1073{
1074 daddr_t bno;
1075 int start, len, loc, i;
1076 int blk, field, subfield, pos;
1077
1078 /*
1079 * find the fragment by searching through the free block
1080 * map for an appropriate bit pattern
1081 */
1082 if (bpref)
6994bf5d 1083 start = dtogd(fs, bpref) / NBBY;
743f1ef7
KM
1084 else
1085 start = cgp->cg_frotor / NBBY;
942bd18b 1086 len = howmany(fs->fs_fpg, NBBY) - start;
9523729e
KM
1087 loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start],
1088 (u_char *)fragtbl[fs->fs_frag],
1089 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
743f1ef7 1090 if (loc == 0) {
e5476900
KM
1091 len = start + 1;
1092 start = 0;
9523729e
KM
1093 loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0],
1094 (u_char *)fragtbl[fs->fs_frag],
1095 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
4e0c7b8a
KM
1096 if (loc == 0) {
1097 printf("start = %d, len = %d, fs = %s\n",
1098 start, len, fs->fs_fsmnt);
6d3d8a1c 1099 panic("ffs_alloccg: map corrupted");
e5889092 1100 /* NOTREACHED */
4e0c7b8a 1101 }
743f1ef7
KM
1102 }
1103 bno = (start + len - loc) * NBBY;
1104 cgp->cg_frotor = bno;
1105 /*
1106 * found the byte in the map
1107 * sift through the bits to find the selected frag
1108 */
ae851115 1109 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
10adeb11 1110 blk = blkmap(fs, cg_blksfree(cgp), bno);
743f1ef7
KM
1111 blk <<= 1;
1112 field = around[allocsiz];
1113 subfield = inside[allocsiz];
b6407c9d 1114 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
ae851115
KM
1115 if ((blk & field) == subfield)
1116 return (bno + pos);
743f1ef7
KM
1117 field <<= 1;
1118 subfield <<= 1;
1119 }
1120 }
ffd90e52 1121 printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
6d3d8a1c 1122 panic("ffs_alloccg: block not in map");
e5476900 1123 return (-1);
743f1ef7
KM
1124}
1125
e3fe2d69 1126/*
502770a3
KM
1127 * Fserr prints the name of a file system with an error diagnostic.
1128 *
1129 * The form of the error message is:
e3fe2d69
KM
1130 * fs: error message
1131 */
6d3d8a1c
KB
1132static void
1133ffs_fserr(fs, uid, cp)
e3fe2d69 1134 struct fs *fs;
6d3d8a1c 1135 u_int uid;
e3fe2d69
KM
1136 char *cp;
1137{
1138
e2546752 1139 log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
e3fe2d69 1140}