ffs_alloc.c revision 1.8 1 /* $NetBSD: ffs_alloc.c,v 1.8 1995/07/19 15:47:36 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)ffs_alloc.c 8.11 (Berkeley) 10/27/94
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/buf.h>
41 #include <sys/proc.h>
42 #include <sys/vnode.h>
43 #include <sys/mount.h>
44 #include <sys/kernel.h>
45 #include <sys/syslog.h>
46
47 #include <vm/vm.h>
48
49 #include <ufs/ufs/quota.h>
50 #include <ufs/ufs/inode.h>
51
52 #include <ufs/ffs/fs.h>
53 #include <ufs/ffs/ffs_extern.h>
54
55 extern u_long nextgennumber;
56
57 static daddr_t ffs_alloccg __P((struct inode *, int, daddr_t, int));
58 static daddr_t ffs_alloccgblk __P((struct fs *, struct cg *, daddr_t));
59 static daddr_t ffs_clusteralloc __P((struct inode *, int, daddr_t, int));
60 static ino_t ffs_dirpref __P((struct fs *));
61 static daddr_t ffs_fragextend __P((struct inode *, int, long, int, int));
62 static void ffs_fserr __P((struct fs *, u_int, char *));
63 static u_long ffs_hashalloc
64 __P((struct inode *, int, long, int, u_int32_t (*)()));
65 static ino_t ffs_nodealloccg __P((struct inode *, int, daddr_t, int));
66 static daddr_t ffs_mapsearch __P((struct fs *, struct cg *, daddr_t, int));
67
68 /*
69 * Allocate a block in the file system.
70 *
71 * The size of the requested block is given, which must be some
72 * multiple of fs_fsize and <= fs_bsize.
73 * A preference may be optionally specified. If a preference is given
74 * the following hierarchy is used to allocate a block:
75 * 1) allocate the requested block.
76 * 2) allocate a rotationally optimal block in the same cylinder.
77 * 3) allocate a block in the same cylinder group.
78 * 4) quadradically rehash into other cylinder groups, until an
79 * available block is located.
80 * If no block preference is given the following heirarchy is used
81 * to allocate a block:
82 * 1) allocate a block in the cylinder group that contains the
83 * inode for the file.
84 * 2) quadradically rehash into other cylinder groups, until an
85 * available block is located.
86 */
87 ffs_alloc(ip, lbn, bpref, size, cred, bnp)
88 register struct inode *ip;
89 daddr_t lbn, bpref;
90 int size;
91 struct ucred *cred;
92 daddr_t *bnp;
93 {
94 register struct fs *fs;
95 daddr_t bno;
96 int cg, error;
97
98 *bnp = 0;
99 fs = ip->i_fs;
100 #ifdef DIAGNOSTIC
101 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
102 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
103 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
104 panic("ffs_alloc: bad size");
105 }
106 if (cred == NOCRED)
107 panic("ffs_alloc: missing credential\n");
108 #endif /* DIAGNOSTIC */
109 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
110 goto nospace;
111 if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
112 goto nospace;
113 #ifdef QUOTA
114 if (error = chkdq(ip, (long)btodb(size), cred, 0))
115 return (error);
116 #endif
117 if (bpref >= fs->fs_size)
118 bpref = 0;
119 if (bpref == 0)
120 cg = ino_to_cg(fs, ip->i_number);
121 else
122 cg = dtog(fs, bpref);
123 bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size,
124 (u_int32_t (*)())ffs_alloccg);
125 if (bno > 0) {
126 ip->i_blocks += btodb(size);
127 ip->i_flag |= IN_CHANGE | IN_UPDATE;
128 *bnp = bno;
129 return (0);
130 }
131 #ifdef QUOTA
132 /*
133 * Restore user's disk quota because allocation failed.
134 */
135 (void) chkdq(ip, (long)-btodb(size), cred, FORCE);
136 #endif
137 nospace:
138 ffs_fserr(fs, cred->cr_uid, "file system full");
139 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
140 return (ENOSPC);
141 }
142
143 /*
144 * Reallocate a fragment to a bigger size
145 *
146 * The number and size of the old block is given, and a preference
147 * and new size is also specified. The allocator attempts to extend
148 * the original block. Failing that, the regular block allocator is
149 * invoked to get an appropriate block.
150 */
151 ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp)
152 register struct inode *ip;
153 daddr_t lbprev;
154 daddr_t bpref;
155 int osize, nsize;
156 struct ucred *cred;
157 struct buf **bpp;
158 {
159 register struct fs *fs;
160 struct buf *bp;
161 int cg, request, error;
162 daddr_t bprev, bno;
163
164 *bpp = 0;
165 fs = ip->i_fs;
166 #ifdef DIAGNOSTIC
167 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
168 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
169 printf(
170 "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
171 ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
172 panic("ffs_realloccg: bad size");
173 }
174 if (cred == NOCRED)
175 panic("ffs_realloccg: missing credential\n");
176 #endif /* DIAGNOSTIC */
177 if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
178 goto nospace;
179 if ((bprev = ip->i_db[lbprev]) == 0) {
180 printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
181 ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
182 panic("ffs_realloccg: bad bprev");
183 }
184 /*
185 * Allocate the extra space in the buffer.
186 */
187 if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) {
188 brelse(bp);
189 return (error);
190 }
191 #ifdef QUOTA
192 if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) {
193 brelse(bp);
194 return (error);
195 }
196 #endif
197 /*
198 * Check for extension in the existing location.
199 */
200 cg = dtog(fs, bprev);
201 if (bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) {
202 if (bp->b_blkno != fsbtodb(fs, bno))
203 panic("bad blockno");
204 ip->i_blocks += btodb(nsize - osize);
205 ip->i_flag |= IN_CHANGE | IN_UPDATE;
206 allocbuf(bp, nsize);
207 bp->b_flags |= B_DONE;
208 bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
209 *bpp = bp;
210 return (0);
211 }
212 /*
213 * Allocate a new disk location.
214 */
215 if (bpref >= fs->fs_size)
216 bpref = 0;
217 switch ((int)fs->fs_optim) {
218 case FS_OPTSPACE:
219 /*
220 * Allocate an exact sized fragment. Although this makes
221 * best use of space, we will waste time relocating it if
222 * the file continues to grow. If the fragmentation is
223 * less than half of the minimum free reserve, we choose
224 * to begin optimizing for time.
225 */
226 request = nsize;
227 if (fs->fs_minfree < 5 ||
228 fs->fs_cstotal.cs_nffree >
229 fs->fs_dsize * fs->fs_minfree / (2 * 100))
230 break;
231 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
232 fs->fs_fsmnt);
233 fs->fs_optim = FS_OPTTIME;
234 break;
235 case FS_OPTTIME:
236 /*
237 * At this point we have discovered a file that is trying to
238 * grow a small fragment to a larger fragment. To save time,
239 * we allocate a full sized block, then free the unused portion.
240 * If the file continues to grow, the `ffs_fragextend' call
241 * above will be able to grow it in place without further
242 * copying. If aberrant programs cause disk fragmentation to
243 * grow within 2% of the free reserve, we choose to begin
244 * optimizing for space.
245 */
246 request = fs->fs_bsize;
247 if (fs->fs_cstotal.cs_nffree <
248 fs->fs_dsize * (fs->fs_minfree - 2) / 100)
249 break;
250 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
251 fs->fs_fsmnt);
252 fs->fs_optim = FS_OPTSPACE;
253 break;
254 default:
255 printf("dev = 0x%x, optim = %d, fs = %s\n",
256 ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
257 panic("ffs_realloccg: bad optim");
258 /* NOTREACHED */
259 }
260 bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
261 (u_int32_t (*)())ffs_alloccg);
262 if (bno > 0) {
263 bp->b_blkno = fsbtodb(fs, bno);
264 (void) vnode_pager_uncache(ITOV(ip));
265 ffs_blkfree(ip, bprev, (long)osize);
266 if (nsize < request)
267 ffs_blkfree(ip, bno + numfrags(fs, nsize),
268 (long)(request - nsize));
269 ip->i_blocks += btodb(nsize - osize);
270 ip->i_flag |= IN_CHANGE | IN_UPDATE;
271 allocbuf(bp, nsize);
272 bp->b_flags |= B_DONE;
273 bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
274 *bpp = bp;
275 return (0);
276 }
277 #ifdef QUOTA
278 /*
279 * Restore user's disk quota because allocation failed.
280 */
281 (void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
282 #endif
283 brelse(bp);
284 nospace:
285 /*
286 * no space available
287 */
288 ffs_fserr(fs, cred->cr_uid, "file system full");
289 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
290 return (ENOSPC);
291 }
292
293 /*
294 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
295 *
296 * The vnode and an array of buffer pointers for a range of sequential
297 * logical blocks to be made contiguous is given. The allocator attempts
298 * to find a range of sequential blocks starting as close as possible to
299 * an fs_rotdelay offset from the end of the allocation for the logical
300 * block immediately preceeding the current range. If successful, the
301 * physical block numbers in the buffer pointers and in the inode are
302 * changed to reflect the new allocation. If unsuccessful, the allocation
303 * is left unchanged. The success in doing the reallocation is returned.
304 * Note that the error return is not reflected back to the user. Rather
305 * the previous block allocation will be used.
306 */
307 #ifdef DEBUG
308 #include <sys/sysctl.h>
309 int doasyncfree = 1;
310 struct ctldebug debug14 = { "doasyncfree", &doasyncfree };
311 int prtrealloc = 0;
312 struct ctldebug debug15 = { "prtrealloc", &prtrealloc };
313 #else
314 #define doasyncfree 1
315 #endif
316
317 int
318 ffs_reallocblks(ap)
319 struct vop_reallocblks_args /* {
320 struct vnode *a_vp;
321 struct cluster_save *a_buflist;
322 } */ *ap;
323 {
324 struct fs *fs;
325 struct inode *ip;
326 struct vnode *vp;
327 struct buf *sbp, *ebp;
328 daddr_t *bap, *sbap, *ebap;
329 struct cluster_save *buflist;
330 daddr_t start_lbn, end_lbn, soff, eoff, newblk, blkno;
331 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
332 int i, len, start_lvl, end_lvl, pref, ssize;
333
334 vp = ap->a_vp;
335 ip = VTOI(vp);
336 fs = ip->i_fs;
337 if (fs->fs_contigsumsize <= 0)
338 return (ENOSPC);
339 buflist = ap->a_buflist;
340 len = buflist->bs_nchildren;
341 start_lbn = buflist->bs_children[0]->b_lblkno;
342 end_lbn = start_lbn + len - 1;
343 #ifdef DIAGNOSTIC
344 for (i = 1; i < len; i++)
345 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
346 panic("ffs_reallocblks: non-cluster");
347 #endif
348 /*
349 * If the latest allocation is in a new cylinder group, assume that
350 * the filesystem has decided to move and do not force it back to
351 * the previous cylinder group.
352 */
353 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
354 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
355 return (ENOSPC);
356 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
357 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
358 return (ENOSPC);
359 /*
360 * Get the starting offset and block map for the first block.
361 */
362 if (start_lvl == 0) {
363 sbap = &ip->i_db[0];
364 soff = start_lbn;
365 } else {
366 idp = &start_ap[start_lvl - 1];
367 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
368 brelse(sbp);
369 return (ENOSPC);
370 }
371 sbap = (daddr_t *)sbp->b_data;
372 soff = idp->in_off;
373 }
374 /*
375 * Find the preferred location for the cluster.
376 */
377 pref = ffs_blkpref(ip, start_lbn, soff, sbap);
378 /*
379 * If the block range spans two block maps, get the second map.
380 */
381 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
382 ssize = len;
383 } else {
384 #ifdef DIAGNOSTIC
385 if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
386 panic("ffs_reallocblk: start == end");
387 #endif
388 ssize = len - (idp->in_off + 1);
389 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
390 goto fail;
391 ebap = (daddr_t *)ebp->b_data;
392 }
393 /*
394 * Search the block map looking for an allocation of the desired size.
395 */
396 if ((newblk = (daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref,
397 len, (u_int32_t (*)())ffs_clusteralloc)) == 0)
398 goto fail;
399 /*
400 * We have found a new contiguous block.
401 *
402 * First we have to replace the old block pointers with the new
403 * block pointers in the inode and indirect blocks associated
404 * with the file.
405 */
406 #ifdef DEBUG
407 if (prtrealloc)
408 printf("realloc: ino %d, lbns %d-%d\n\told:", ip->i_number,
409 start_lbn, end_lbn);
410 #endif
411 blkno = newblk;
412 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
413 if (i == ssize)
414 bap = ebap;
415 #ifdef DIAGNOSTIC
416 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
417 panic("ffs_reallocblks: alloc mismatch");
418 #endif
419 #ifdef DEBUG
420 if (prtrealloc)
421 printf(" %d,", *bap);
422 #endif
423 *bap++ = blkno;
424 }
425 /*
426 * Next we must write out the modified inode and indirect blocks.
427 * For strict correctness, the writes should be synchronous since
428 * the old block values may have been written to disk. In practise
429 * they are almost never written, but if we are concerned about
430 * strict correctness, the `doasyncfree' flag should be set to zero.
431 *
432 * The test on `doasyncfree' should be changed to test a flag
433 * that shows whether the associated buffers and inodes have
434 * been written. The flag should be set when the cluster is
435 * started and cleared whenever the buffer or inode is flushed.
436 * We can then check below to see if it is set, and do the
437 * synchronous write only when it has been cleared.
438 */
439 if (sbap != &ip->i_db[0]) {
440 if (doasyncfree)
441 bdwrite(sbp);
442 else
443 bwrite(sbp);
444 } else {
445 ip->i_flag |= IN_CHANGE | IN_UPDATE;
446 if (!doasyncfree)
447 VOP_UPDATE(vp, (struct timeval *)&time,
448 (struct timeval *)&time, MNT_WAIT);
449 }
450 if (ssize < len)
451 if (doasyncfree)
452 bdwrite(ebp);
453 else
454 bwrite(ebp);
455 /*
456 * Last, free the old blocks and assign the new blocks to the buffers.
457 */
458 #ifdef DEBUG
459 if (prtrealloc)
460 printf("\n\tnew:");
461 #endif
462 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
463 ffs_blkfree(ip, dbtofsb(fs, buflist->bs_children[i]->b_blkno),
464 fs->fs_bsize);
465 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
466 #ifdef DEBUG
467 if (prtrealloc)
468 printf(" %d,", blkno);
469 #endif
470 }
471 #ifdef DEBUG
472 if (prtrealloc) {
473 prtrealloc--;
474 printf("\n");
475 }
476 #endif
477 return (0);
478
479 fail:
480 if (ssize < len)
481 brelse(ebp);
482 if (sbap != &ip->i_db[0])
483 brelse(sbp);
484 return (ENOSPC);
485 }
486
487 /*
488 * Allocate an inode in the file system.
489 *
490 * If allocating a directory, use ffs_dirpref to select the inode.
491 * If allocating in a directory, the following hierarchy is followed:
492 * 1) allocate the preferred inode.
493 * 2) allocate an inode in the same cylinder group.
494 * 3) quadradically rehash into other cylinder groups, until an
495 * available inode is located.
496 * If no inode preference is given the following heirarchy is used
497 * to allocate an inode:
498 * 1) allocate an inode in cylinder group 0.
499 * 2) quadradically rehash into other cylinder groups, until an
500 * available inode is located.
501 */
502 ffs_valloc(ap)
503 struct vop_valloc_args /* {
504 struct vnode *a_pvp;
505 int a_mode;
506 struct ucred *a_cred;
507 struct vnode **a_vpp;
508 } */ *ap;
509 {
510 register struct vnode *pvp = ap->a_pvp;
511 register struct inode *pip;
512 register struct fs *fs;
513 register struct inode *ip;
514 mode_t mode = ap->a_mode;
515 ino_t ino, ipref;
516 int cg, error;
517
518 *ap->a_vpp = NULL;
519 pip = VTOI(pvp);
520 fs = pip->i_fs;
521 if (fs->fs_cstotal.cs_nifree == 0)
522 goto noinodes;
523
524 if ((mode & IFMT) == IFDIR)
525 ipref = ffs_dirpref(fs);
526 else
527 ipref = pip->i_number;
528 if (ipref >= fs->fs_ncg * fs->fs_ipg)
529 ipref = 0;
530 cg = ino_to_cg(fs, ipref);
531 ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_nodealloccg);
532 if (ino == 0)
533 goto noinodes;
534 error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp);
535 if (error) {
536 VOP_VFREE(pvp, ino, mode);
537 return (error);
538 }
539 ip = VTOI(*ap->a_vpp);
540 if (ip->i_mode) {
541 printf("mode = 0%o, inum = %d, fs = %s\n",
542 ip->i_mode, ip->i_number, fs->fs_fsmnt);
543 panic("ffs_valloc: dup alloc");
544 }
545 if (ip->i_blocks) { /* XXX */
546 printf("free inode %s/%d had %d blocks\n",
547 fs->fs_fsmnt, ino, ip->i_blocks);
548 ip->i_blocks = 0;
549 }
550 ip->i_flags = 0;
551 /*
552 * Set up a new generation number for this inode.
553 */
554 if (++nextgennumber < (u_long)time.tv_sec)
555 nextgennumber = time.tv_sec;
556 ip->i_gen = nextgennumber;
557 return (0);
558 noinodes:
559 ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes");
560 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
561 return (ENOSPC);
562 }
563
564 /*
565 * Find a cylinder to place a directory.
566 *
567 * The policy implemented by this algorithm is to select from
568 * among those cylinder groups with above the average number of
569 * free inodes, the one with the smallest number of directories.
570 */
571 static ino_t
572 ffs_dirpref(fs)
573 register struct fs *fs;
574 {
575 int cg, minndir, mincg, avgifree;
576
577 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
578 minndir = fs->fs_ipg;
579 mincg = 0;
580 for (cg = 0; cg < fs->fs_ncg; cg++)
581 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
582 fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
583 mincg = cg;
584 minndir = fs->fs_cs(fs, cg).cs_ndir;
585 }
586 return ((ino_t)(fs->fs_ipg * mincg));
587 }
588
589 /*
590 * Select the desired position for the next block in a file. The file is
591 * logically divided into sections. The first section is composed of the
592 * direct blocks. Each additional section contains fs_maxbpg blocks.
593 *
594 * If no blocks have been allocated in the first section, the policy is to
595 * request a block in the same cylinder group as the inode that describes
596 * the file. If no blocks have been allocated in any other section, the
597 * policy is to place the section in a cylinder group with a greater than
598 * average number of free blocks. An appropriate cylinder group is found
599 * by using a rotor that sweeps the cylinder groups. When a new group of
600 * blocks is needed, the sweep begins in the cylinder group following the
601 * cylinder group from which the previous allocation was made. The sweep
602 * continues until a cylinder group with greater than the average number
603 * of free blocks is found. If the allocation is for the first block in an
604 * indirect block, the information on the previous allocation is unavailable;
605 * here a best guess is made based upon the logical block number being
606 * allocated.
607 *
608 * If a section is already partially allocated, the policy is to
609 * contiguously allocate fs_maxcontig blocks. The end of one of these
610 * contiguous blocks and the beginning of the next is physically separated
611 * so that the disk head will be in transit between them for at least
612 * fs_rotdelay milliseconds. This is to allow time for the processor to
613 * schedule another I/O transfer.
614 */
615 daddr_t
616 ffs_blkpref(ip, lbn, indx, bap)
617 struct inode *ip;
618 daddr_t lbn;
619 int indx;
620 daddr_t *bap;
621 {
622 register struct fs *fs;
623 register int cg;
624 int avgbfree, startcg;
625 daddr_t nextblk;
626
627 fs = ip->i_fs;
628 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
629 if (lbn < NDADDR) {
630 cg = ino_to_cg(fs, ip->i_number);
631 return (fs->fs_fpg * cg + fs->fs_frag);
632 }
633 /*
634 * Find a cylinder with greater than average number of
635 * unused data blocks.
636 */
637 if (indx == 0 || bap[indx - 1] == 0)
638 startcg =
639 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
640 else
641 startcg = dtog(fs, bap[indx - 1]) + 1;
642 startcg %= fs->fs_ncg;
643 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
644 for (cg = startcg; cg < fs->fs_ncg; cg++)
645 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
646 fs->fs_cgrotor = cg;
647 return (fs->fs_fpg * cg + fs->fs_frag);
648 }
649 for (cg = 0; cg <= startcg; cg++)
650 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
651 fs->fs_cgrotor = cg;
652 return (fs->fs_fpg * cg + fs->fs_frag);
653 }
654 return (NULL);
655 }
656 /*
657 * One or more previous blocks have been laid out. If less
658 * than fs_maxcontig previous blocks are contiguous, the
659 * next block is requested contiguously, otherwise it is
660 * requested rotationally delayed by fs_rotdelay milliseconds.
661 */
662 nextblk = bap[indx - 1] + fs->fs_frag;
663 if (indx < fs->fs_maxcontig || bap[indx - fs->fs_maxcontig] +
664 blkstofrags(fs, fs->fs_maxcontig) != nextblk)
665 return (nextblk);
666 if (fs->fs_rotdelay != 0)
667 /*
668 * Here we convert ms of delay to frags as:
669 * (frags) = (ms) * (rev/sec) * (sect/rev) /
670 * ((sect/frag) * (ms/sec))
671 * then round up to the next block.
672 */
673 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
674 (NSPF(fs) * 1000), fs->fs_frag);
675 return (nextblk);
676 }
677
678 /*
679 * Implement the cylinder overflow algorithm.
680 *
681 * The policy implemented by this algorithm is:
682 * 1) allocate the block in its requested cylinder group.
683 * 2) quadradically rehash on the cylinder group number.
684 * 3) brute force search for a free block.
685 */
686 /*VARARGS5*/
687 static u_long
688 ffs_hashalloc(ip, cg, pref, size, allocator)
689 struct inode *ip;
690 int cg;
691 long pref;
692 int size; /* size for data blocks, mode for inodes */
693 u_int32_t (*allocator)();
694 {
695 register struct fs *fs;
696 long result;
697 int i, icg = cg;
698
699 fs = ip->i_fs;
700 /*
701 * 1: preferred cylinder group
702 */
703 result = (*allocator)(ip, cg, pref, size);
704 if (result)
705 return (result);
706 /*
707 * 2: quadratic rehash
708 */
709 for (i = 1; i < fs->fs_ncg; i *= 2) {
710 cg += i;
711 if (cg >= fs->fs_ncg)
712 cg -= fs->fs_ncg;
713 result = (*allocator)(ip, cg, 0, size);
714 if (result)
715 return (result);
716 }
717 /*
718 * 3: brute force search
719 * Note that we start at i == 2, since 0 was checked initially,
720 * and 1 is always checked in the quadratic rehash.
721 */
722 cg = (icg + 2) % fs->fs_ncg;
723 for (i = 2; i < fs->fs_ncg; i++) {
724 result = (*allocator)(ip, cg, 0, size);
725 if (result)
726 return (result);
727 cg++;
728 if (cg == fs->fs_ncg)
729 cg = 0;
730 }
731 return (NULL);
732 }
733
734 /*
735 * Determine whether a fragment can be extended.
736 *
737 * Check to see if the necessary fragments are available, and
738 * if they are, allocate them.
739 */
740 static daddr_t
741 ffs_fragextend(ip, cg, bprev, osize, nsize)
742 struct inode *ip;
743 int cg;
744 long bprev;
745 int osize, nsize;
746 {
747 register struct fs *fs;
748 register struct cg *cgp;
749 struct buf *bp;
750 long bno;
751 int frags, bbase;
752 int i, error;
753
754 fs = ip->i_fs;
755 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
756 return (NULL);
757 frags = numfrags(fs, nsize);
758 bbase = fragnum(fs, bprev);
759 if (bbase > fragnum(fs, (bprev + frags - 1))) {
760 /* cannot extend across a block boundary */
761 return (NULL);
762 }
763 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
764 (int)fs->fs_cgsize, NOCRED, &bp);
765 if (error) {
766 brelse(bp);
767 return (NULL);
768 }
769 cgp = (struct cg *)bp->b_data;
770 if (!cg_chkmagic(cgp)) {
771 brelse(bp);
772 return (NULL);
773 }
774 cgp->cg_time = time.tv_sec;
775 bno = dtogd(fs, bprev);
776 for (i = numfrags(fs, osize); i < frags; i++)
777 if (isclr(cg_blksfree(cgp), bno + i)) {
778 brelse(bp);
779 return (NULL);
780 }
781 /*
782 * the current fragment can be extended
783 * deduct the count on fragment being extended into
784 * increase the count on the remaining fragment (if any)
785 * allocate the extended piece
786 */
787 for (i = frags; i < fs->fs_frag - bbase; i++)
788 if (isclr(cg_blksfree(cgp), bno + i))
789 break;
790 cgp->cg_frsum[i - numfrags(fs, osize)]--;
791 if (i != frags)
792 cgp->cg_frsum[i - frags]++;
793 for (i = numfrags(fs, osize); i < frags; i++) {
794 clrbit(cg_blksfree(cgp), bno + i);
795 cgp->cg_cs.cs_nffree--;
796 fs->fs_cstotal.cs_nffree--;
797 fs->fs_cs(fs, cg).cs_nffree--;
798 }
799 fs->fs_fmod = 1;
800 bdwrite(bp);
801 return (bprev);
802 }
803
804 /*
805 * Determine whether a block can be allocated.
806 *
807 * Check to see if a block of the appropriate size is available,
808 * and if it is, allocate it.
809 */
810 static daddr_t
811 ffs_alloccg(ip, cg, bpref, size)
812 struct inode *ip;
813 int cg;
814 daddr_t bpref;
815 int size;
816 {
817 register struct fs *fs;
818 register struct cg *cgp;
819 struct buf *bp;
820 register int i;
821 int error, bno, frags, allocsiz;
822
823 fs = ip->i_fs;
824 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
825 return (NULL);
826 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
827 (int)fs->fs_cgsize, NOCRED, &bp);
828 if (error) {
829 brelse(bp);
830 return (NULL);
831 }
832 cgp = (struct cg *)bp->b_data;
833 if (!cg_chkmagic(cgp) ||
834 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
835 brelse(bp);
836 return (NULL);
837 }
838 cgp->cg_time = time.tv_sec;
839 if (size == fs->fs_bsize) {
840 bno = ffs_alloccgblk(fs, cgp, bpref);
841 bdwrite(bp);
842 return (bno);
843 }
844 /*
845 * check to see if any fragments are already available
846 * allocsiz is the size which will be allocated, hacking
847 * it down to a smaller size if necessary
848 */
849 frags = numfrags(fs, size);
850 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
851 if (cgp->cg_frsum[allocsiz] != 0)
852 break;
853 if (allocsiz == fs->fs_frag) {
854 /*
855 * no fragments were available, so a block will be
856 * allocated, and hacked up
857 */
858 if (cgp->cg_cs.cs_nbfree == 0) {
859 brelse(bp);
860 return (NULL);
861 }
862 bno = ffs_alloccgblk(fs, cgp, bpref);
863 bpref = dtogd(fs, bno);
864 for (i = frags; i < fs->fs_frag; i++)
865 setbit(cg_blksfree(cgp), bpref + i);
866 i = fs->fs_frag - frags;
867 cgp->cg_cs.cs_nffree += i;
868 fs->fs_cstotal.cs_nffree += i;
869 fs->fs_cs(fs, cg).cs_nffree += i;
870 fs->fs_fmod = 1;
871 cgp->cg_frsum[i]++;
872 bdwrite(bp);
873 return (bno);
874 }
875 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
876 if (bno < 0) {
877 brelse(bp);
878 return (NULL);
879 }
880 for (i = 0; i < frags; i++)
881 clrbit(cg_blksfree(cgp), bno + i);
882 cgp->cg_cs.cs_nffree -= frags;
883 fs->fs_cstotal.cs_nffree -= frags;
884 fs->fs_cs(fs, cg).cs_nffree -= frags;
885 fs->fs_fmod = 1;
886 cgp->cg_frsum[allocsiz]--;
887 if (frags != allocsiz)
888 cgp->cg_frsum[allocsiz - frags]++;
889 bdwrite(bp);
890 return (cg * fs->fs_fpg + bno);
891 }
892
893 /*
894 * Allocate a block in a cylinder group.
895 *
896 * This algorithm implements the following policy:
897 * 1) allocate the requested block.
898 * 2) allocate a rotationally optimal block in the same cylinder.
899 * 3) allocate the next available block on the block rotor for the
900 * specified cylinder group.
901 * Note that this routine only allocates fs_bsize blocks; these
902 * blocks may be fragmented by the routine that allocates them.
903 */
904 static daddr_t
905 ffs_alloccgblk(fs, cgp, bpref)
906 register struct fs *fs;
907 register struct cg *cgp;
908 daddr_t bpref;
909 {
910 daddr_t bno, blkno;
911 int cylno, pos, delta;
912 short *cylbp;
913 register int i;
914
915 if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
916 bpref = cgp->cg_rotor;
917 goto norot;
918 }
919 bpref = blknum(fs, bpref);
920 bpref = dtogd(fs, bpref);
921 /*
922 * if the requested block is available, use it
923 */
924 if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
925 bno = bpref;
926 goto gotit;
927 }
928 if (fs->fs_cpc == 0 || fs->fs_nrpos <= 1) {
929 /*
930 * Block layout information is not available.
931 * Leaving bpref unchanged means we take the
932 * next available free block following the one
933 * we just allocated. Hopefully this will at
934 * least hit a track cache on drives of unknown
935 * geometry (e.g. SCSI).
936 */
937 goto norot;
938 }
939 /*
940 * check for a block available on the same cylinder
941 */
942 cylno = cbtocylno(fs, bpref);
943 if (cg_blktot(cgp)[cylno] == 0)
944 goto norot;
945 /*
946 * check the summary information to see if a block is
947 * available in the requested cylinder starting at the
948 * requested rotational position and proceeding around.
949 */
950 cylbp = cg_blks(fs, cgp, cylno);
951 pos = cbtorpos(fs, bpref);
952 for (i = pos; i < fs->fs_nrpos; i++)
953 if (cylbp[i] > 0)
954 break;
955 if (i == fs->fs_nrpos)
956 for (i = 0; i < pos; i++)
957 if (cylbp[i] > 0)
958 break;
959 if (cylbp[i] > 0) {
960 /*
961 * found a rotational position, now find the actual
962 * block. A panic if none is actually there.
963 */
964 pos = cylno % fs->fs_cpc;
965 bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
966 if (fs_postbl(fs, pos)[i] == -1) {
967 printf("pos = %d, i = %d, fs = %s\n",
968 pos, i, fs->fs_fsmnt);
969 panic("ffs_alloccgblk: cyl groups corrupted");
970 }
971 for (i = fs_postbl(fs, pos)[i];; ) {
972 if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) {
973 bno = blkstofrags(fs, (bno + i));
974 goto gotit;
975 }
976 delta = fs_rotbl(fs)[i];
977 if (delta <= 0 ||
978 delta + i > fragstoblks(fs, fs->fs_fpg))
979 break;
980 i += delta;
981 }
982 printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
983 panic("ffs_alloccgblk: can't find blk in cyl");
984 }
985 norot:
986 /*
987 * no blocks in the requested cylinder, so take next
988 * available one in this cylinder group.
989 */
990 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
991 if (bno < 0)
992 return (NULL);
993 cgp->cg_rotor = bno;
994 gotit:
995 blkno = fragstoblks(fs, bno);
996 ffs_clrblock(fs, cg_blksfree(cgp), (long)blkno);
997 ffs_clusteracct(fs, cgp, blkno, -1);
998 cgp->cg_cs.cs_nbfree--;
999 fs->fs_cstotal.cs_nbfree--;
1000 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1001 cylno = cbtocylno(fs, bno);
1002 cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
1003 cg_blktot(cgp)[cylno]--;
1004 fs->fs_fmod = 1;
1005 return (cgp->cg_cgx * fs->fs_fpg + bno);
1006 }
1007
1008 /*
1009 * Determine whether a cluster can be allocated.
1010 *
1011 * We do not currently check for optimal rotational layout if there
1012 * are multiple choices in the same cylinder group. Instead we just
1013 * take the first one that we find following bpref.
1014 */
1015 static daddr_t
1016 ffs_clusteralloc(ip, cg, bpref, len)
1017 struct inode *ip;
1018 int cg;
1019 daddr_t bpref;
1020 int len;
1021 {
1022 register struct fs *fs;
1023 register struct cg *cgp;
1024 struct buf *bp;
1025 int i, run, bno, bit, map;
1026 u_char *mapp;
1027 int32_t *lp;
1028
1029 fs = ip->i_fs;
1030 if (fs->fs_maxcluster[cg] < len)
1031 return (NULL);
1032 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1033 NOCRED, &bp))
1034 goto fail;
1035 cgp = (struct cg *)bp->b_data;
1036 if (!cg_chkmagic(cgp))
1037 goto fail;
1038 /*
1039 * Check to see if a cluster of the needed size (or bigger) is
1040 * available in this cylinder group.
1041 */
1042 lp = &cg_clustersum(cgp)[len];
1043 for (i = len; i <= fs->fs_contigsumsize; i++)
1044 if (*lp++ > 0)
1045 break;
1046 if (i > fs->fs_contigsumsize) {
1047 /*
1048 * This is the first time looking for a cluster in this
1049 * cylinder group. Update the cluster summary information
1050 * to reflect the true maximum sized cluster so that
1051 * future cluster allocation requests can avoid reading
1052 * the cylinder group map only to find no clusters.
1053 */
1054 lp = &cg_clustersum(cgp)[len - 1];
1055 for (i = len - 1; i > 0; i--)
1056 if (*lp-- > 0)
1057 break;
1058 fs->fs_maxcluster[cg] = i;
1059 goto fail;
1060 }
1061 /*
1062 * Search the cluster map to find a big enough cluster.
1063 * We take the first one that we find, even if it is larger
1064 * than we need as we prefer to get one close to the previous
1065 * block allocation. We do not search before the current
1066 * preference point as we do not want to allocate a block
1067 * that is allocated before the previous one (as we will
1068 * then have to wait for another pass of the elevator
1069 * algorithm before it will be read). We prefer to fail and
1070 * be recalled to try an allocation in the next cylinder group.
1071 */
1072 if (dtog(fs, bpref) != cg)
1073 bpref = 0;
1074 else
1075 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1076 mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1077 map = *mapp++;
1078 bit = 1 << (bpref % NBBY);
1079 for (run = 0, i = bpref; i < cgp->cg_nclusterblks; i++) {
1080 if ((map & bit) == 0) {
1081 run = 0;
1082 } else {
1083 run++;
1084 if (run == len)
1085 break;
1086 }
1087 if ((i & (NBBY - 1)) != (NBBY - 1)) {
1088 bit <<= 1;
1089 } else {
1090 map = *mapp++;
1091 bit = 1;
1092 }
1093 }
1094 if (i == cgp->cg_nclusterblks)
1095 goto fail;
1096 /*
1097 * Allocate the cluster that we have found.
1098 */
1099 bno = cg * fs->fs_fpg + blkstofrags(fs, i - run + 1);
1100 len = blkstofrags(fs, len);
1101 for (i = 0; i < len; i += fs->fs_frag)
1102 if (ffs_alloccgblk(fs, cgp, bno + i) != bno + i)
1103 panic("ffs_clusteralloc: lost block");
1104 bdwrite(bp);
1105 return (bno);
1106
1107 fail:
1108 brelse(bp);
1109 return (0);
1110 }
1111
1112 /*
1113 * Determine whether an inode can be allocated.
1114 *
1115 * Check to see if an inode is available, and if it is,
1116 * allocate it using the following policy:
1117 * 1) allocate the requested inode.
1118 * 2) allocate the next available inode after the requested
1119 * inode in the specified cylinder group.
1120 */
1121 static ino_t
1122 ffs_nodealloccg(ip, cg, ipref, mode)
1123 struct inode *ip;
1124 int cg;
1125 daddr_t ipref;
1126 int mode;
1127 {
1128 register struct fs *fs;
1129 register struct cg *cgp;
1130 struct buf *bp;
1131 int error, start, len, loc, map, i;
1132
1133 fs = ip->i_fs;
1134 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1135 return (NULL);
1136 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1137 (int)fs->fs_cgsize, NOCRED, &bp);
1138 if (error) {
1139 brelse(bp);
1140 return (NULL);
1141 }
1142 cgp = (struct cg *)bp->b_data;
1143 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
1144 brelse(bp);
1145 return (NULL);
1146 }
1147 cgp->cg_time = time.tv_sec;
1148 if (ipref) {
1149 ipref %= fs->fs_ipg;
1150 if (isclr(cg_inosused(cgp), ipref))
1151 goto gotit;
1152 }
1153 start = cgp->cg_irotor / NBBY;
1154 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
1155 loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
1156 if (loc == 0) {
1157 len = start + 1;
1158 start = 0;
1159 loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
1160 if (loc == 0) {
1161 printf("cg = %d, irotor = %d, fs = %s\n",
1162 cg, cgp->cg_irotor, fs->fs_fsmnt);
1163 panic("ffs_nodealloccg: map corrupted");
1164 /* NOTREACHED */
1165 }
1166 }
1167 i = start + len - loc;
1168 map = cg_inosused(cgp)[i];
1169 ipref = i * NBBY;
1170 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1171 if ((map & i) == 0) {
1172 cgp->cg_irotor = ipref;
1173 goto gotit;
1174 }
1175 }
1176 printf("fs = %s\n", fs->fs_fsmnt);
1177 panic("ffs_nodealloccg: block not in map");
1178 /* NOTREACHED */
1179 gotit:
1180 setbit(cg_inosused(cgp), ipref);
1181 cgp->cg_cs.cs_nifree--;
1182 fs->fs_cstotal.cs_nifree--;
1183 fs->fs_cs(fs, cg).cs_nifree--;
1184 fs->fs_fmod = 1;
1185 if ((mode & IFMT) == IFDIR) {
1186 cgp->cg_cs.cs_ndir++;
1187 fs->fs_cstotal.cs_ndir++;
1188 fs->fs_cs(fs, cg).cs_ndir++;
1189 }
1190 bdwrite(bp);
1191 return (cg * fs->fs_ipg + ipref);
1192 }
1193
1194 /*
1195 * Free a block or fragment.
1196 *
1197 * The specified block or fragment is placed back in the
1198 * free map. If a fragment is deallocated, a possible
1199 * block reassembly is checked.
1200 */
1201 ffs_blkfree(ip, bno, size)
1202 register struct inode *ip;
1203 daddr_t bno;
1204 long size;
1205 {
1206 register struct fs *fs;
1207 register struct cg *cgp;
1208 struct buf *bp;
1209 daddr_t blkno;
1210 int i, error, cg, blk, frags, bbase;
1211
1212 fs = ip->i_fs;
1213 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1214 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
1215 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
1216 panic("blkfree: bad size");
1217 }
1218 cg = dtog(fs, bno);
1219 if ((u_int)bno >= fs->fs_size) {
1220 printf("bad block %d, ino %d\n", bno, ip->i_number);
1221 ffs_fserr(fs, ip->i_uid, "bad block");
1222 return;
1223 }
1224 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1225 (int)fs->fs_cgsize, NOCRED, &bp);
1226 if (error) {
1227 brelse(bp);
1228 return;
1229 }
1230 cgp = (struct cg *)bp->b_data;
1231 if (!cg_chkmagic(cgp)) {
1232 brelse(bp);
1233 return;
1234 }
1235 cgp->cg_time = time.tv_sec;
1236 bno = dtogd(fs, bno);
1237 if (size == fs->fs_bsize) {
1238 blkno = fragstoblks(fs, bno);
1239 if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) {
1240 printf("dev = 0x%x, block = %d, fs = %s\n",
1241 ip->i_dev, bno, fs->fs_fsmnt);
1242 panic("blkfree: freeing free block");
1243 }
1244 ffs_setblock(fs, cg_blksfree(cgp), blkno);
1245 ffs_clusteracct(fs, cgp, blkno, 1);
1246 cgp->cg_cs.cs_nbfree++;
1247 fs->fs_cstotal.cs_nbfree++;
1248 fs->fs_cs(fs, cg).cs_nbfree++;
1249 i = cbtocylno(fs, bno);
1250 cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
1251 cg_blktot(cgp)[i]++;
1252 } else {
1253 bbase = bno - fragnum(fs, bno);
1254 /*
1255 * decrement the counts associated with the old frags
1256 */
1257 blk = blkmap(fs, cg_blksfree(cgp), bbase);
1258 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
1259 /*
1260 * deallocate the fragment
1261 */
1262 frags = numfrags(fs, size);
1263 for (i = 0; i < frags; i++) {
1264 if (isset(cg_blksfree(cgp), bno + i)) {
1265 printf("dev = 0x%x, block = %d, fs = %s\n",
1266 ip->i_dev, bno + i, fs->fs_fsmnt);
1267 panic("blkfree: freeing free frag");
1268 }
1269 setbit(cg_blksfree(cgp), bno + i);
1270 }
1271 cgp->cg_cs.cs_nffree += i;
1272 fs->fs_cstotal.cs_nffree += i;
1273 fs->fs_cs(fs, cg).cs_nffree += i;
1274 /*
1275 * add back in counts associated with the new frags
1276 */
1277 blk = blkmap(fs, cg_blksfree(cgp), bbase);
1278 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
1279 /*
1280 * if a complete block has been reassembled, account for it
1281 */
1282 blkno = fragstoblks(fs, bbase);
1283 if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) {
1284 cgp->cg_cs.cs_nffree -= fs->fs_frag;
1285 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1286 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1287 ffs_clusteracct(fs, cgp, blkno, 1);
1288 cgp->cg_cs.cs_nbfree++;
1289 fs->fs_cstotal.cs_nbfree++;
1290 fs->fs_cs(fs, cg).cs_nbfree++;
1291 i = cbtocylno(fs, bbase);
1292 cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
1293 cg_blktot(cgp)[i]++;
1294 }
1295 }
1296 fs->fs_fmod = 1;
1297 bdwrite(bp);
1298 }
1299
1300 /*
1301 * Free an inode.
1302 *
1303 * The specified inode is placed back in the free map.
1304 */
1305 int
1306 ffs_vfree(ap)
1307 struct vop_vfree_args /* {
1308 struct vnode *a_pvp;
1309 ino_t a_ino;
1310 int a_mode;
1311 } */ *ap;
1312 {
1313 register struct fs *fs;
1314 register struct cg *cgp;
1315 register struct inode *pip;
1316 ino_t ino = ap->a_ino;
1317 struct buf *bp;
1318 int error, cg;
1319
1320 pip = VTOI(ap->a_pvp);
1321 fs = pip->i_fs;
1322 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1323 panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
1324 pip->i_dev, ino, fs->fs_fsmnt);
1325 cg = ino_to_cg(fs, ino);
1326 error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1327 (int)fs->fs_cgsize, NOCRED, &bp);
1328 if (error) {
1329 brelse(bp);
1330 return (0);
1331 }
1332 cgp = (struct cg *)bp->b_data;
1333 if (!cg_chkmagic(cgp)) {
1334 brelse(bp);
1335 return (0);
1336 }
1337 cgp->cg_time = time.tv_sec;
1338 ino %= fs->fs_ipg;
1339 if (isclr(cg_inosused(cgp), ino)) {
1340 printf("dev = 0x%x, ino = %d, fs = %s\n",
1341 pip->i_dev, ino, fs->fs_fsmnt);
1342 if (fs->fs_ronly == 0)
1343 panic("ifree: freeing free inode");
1344 }
1345 clrbit(cg_inosused(cgp), ino);
1346 if (ino < cgp->cg_irotor)
1347 cgp->cg_irotor = ino;
1348 cgp->cg_cs.cs_nifree++;
1349 fs->fs_cstotal.cs_nifree++;
1350 fs->fs_cs(fs, cg).cs_nifree++;
1351 if ((ap->a_mode & IFMT) == IFDIR) {
1352 cgp->cg_cs.cs_ndir--;
1353 fs->fs_cstotal.cs_ndir--;
1354 fs->fs_cs(fs, cg).cs_ndir--;
1355 }
1356 fs->fs_fmod = 1;
1357 bdwrite(bp);
1358 return (0);
1359 }
1360
1361 /*
1362 * Find a block of the specified size in the specified cylinder group.
1363 *
1364 * It is a panic if a request is made to find a block if none are
1365 * available.
1366 */
1367 static daddr_t
1368 ffs_mapsearch(fs, cgp, bpref, allocsiz)
1369 register struct fs *fs;
1370 register struct cg *cgp;
1371 daddr_t bpref;
1372 int allocsiz;
1373 {
1374 daddr_t bno;
1375 int start, len, loc, i;
1376 int blk, field, subfield, pos;
1377
1378 /*
1379 * find the fragment by searching through the free block
1380 * map for an appropriate bit pattern
1381 */
1382 if (bpref)
1383 start = dtogd(fs, bpref) / NBBY;
1384 else
1385 start = cgp->cg_frotor / NBBY;
1386 len = howmany(fs->fs_fpg, NBBY) - start;
1387 loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[start],
1388 (u_char *)fragtbl[fs->fs_frag],
1389 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1390 if (loc == 0) {
1391 len = start + 1;
1392 start = 0;
1393 loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[0],
1394 (u_char *)fragtbl[fs->fs_frag],
1395 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1396 if (loc == 0) {
1397 printf("start = %d, len = %d, fs = %s\n",
1398 start, len, fs->fs_fsmnt);
1399 panic("ffs_alloccg: map corrupted");
1400 /* NOTREACHED */
1401 }
1402 }
1403 bno = (start + len - loc) * NBBY;
1404 cgp->cg_frotor = bno;
1405 /*
1406 * found the byte in the map
1407 * sift through the bits to find the selected frag
1408 */
1409 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1410 blk = blkmap(fs, cg_blksfree(cgp), bno);
1411 blk <<= 1;
1412 field = around[allocsiz];
1413 subfield = inside[allocsiz];
1414 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
1415 if ((blk & field) == subfield)
1416 return (bno + pos);
1417 field <<= 1;
1418 subfield <<= 1;
1419 }
1420 }
1421 printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
1422 panic("ffs_alloccg: block not in map");
1423 return (-1);
1424 }
1425
1426 /*
1427 * Update the cluster map because of an allocation or free.
1428 *
1429 * Cnt == 1 means free; cnt == -1 means allocating.
1430 */
1431 ffs_clusteracct(fs, cgp, blkno, cnt)
1432 struct fs *fs;
1433 struct cg *cgp;
1434 daddr_t blkno;
1435 int cnt;
1436 {
1437 int32_t *sump;
1438 int32_t *lp;
1439 u_char *freemapp, *mapp;
1440 int i, start, end, forw, back, map, bit;
1441
1442 if (fs->fs_contigsumsize <= 0)
1443 return;
1444 freemapp = cg_clustersfree(cgp);
1445 sump = cg_clustersum(cgp);
1446 /*
1447 * Allocate or clear the actual block.
1448 */
1449 if (cnt > 0)
1450 setbit(freemapp, blkno);
1451 else
1452 clrbit(freemapp, blkno);
1453 /*
1454 * Find the size of the cluster going forward.
1455 */
1456 start = blkno + 1;
1457 end = start + fs->fs_contigsumsize;
1458 if (end >= cgp->cg_nclusterblks)
1459 end = cgp->cg_nclusterblks;
1460 mapp = &freemapp[start / NBBY];
1461 map = *mapp++;
1462 bit = 1 << (start % NBBY);
1463 for (i = start; i < end; i++) {
1464 if ((map & bit) == 0)
1465 break;
1466 if ((i & (NBBY - 1)) != (NBBY - 1)) {
1467 bit <<= 1;
1468 } else {
1469 map = *mapp++;
1470 bit = 1;
1471 }
1472 }
1473 forw = i - start;
1474 /*
1475 * Find the size of the cluster going backward.
1476 */
1477 start = blkno - 1;
1478 end = start - fs->fs_contigsumsize;
1479 if (end < 0)
1480 end = -1;
1481 mapp = &freemapp[start / NBBY];
1482 map = *mapp--;
1483 bit = 1 << (start % NBBY);
1484 for (i = start; i > end; i--) {
1485 if ((map & bit) == 0)
1486 break;
1487 if ((i & (NBBY - 1)) != 0) {
1488 bit >>= 1;
1489 } else {
1490 map = *mapp--;
1491 bit = 1 << (NBBY - 1);
1492 }
1493 }
1494 back = start - i;
1495 /*
1496 * Account for old cluster and the possibly new forward and
1497 * back clusters.
1498 */
1499 i = back + forw + 1;
1500 if (i > fs->fs_contigsumsize)
1501 i = fs->fs_contigsumsize;
1502 sump[i] += cnt;
1503 if (back > 0)
1504 sump[back] -= cnt;
1505 if (forw > 0)
1506 sump[forw] -= cnt;
1507 /*
1508 * Update cluster summary information.
1509 */
1510 lp = &sump[fs->fs_contigsumsize];
1511 for (i = fs->fs_contigsumsize; i > 0; i--)
1512 if (*lp-- > 0)
1513 break;
1514 fs->fs_maxcluster[cgp->cg_cgx] = i;
1515 }
1516
1517 /*
1518 * Fserr prints the name of a file system with an error diagnostic.
1519 *
1520 * The form of the error message is:
1521 * fs: error message
1522 */
1523 static void
1524 ffs_fserr(fs, uid, cp)
1525 struct fs *fs;
1526 u_int uid;
1527 char *cp;
1528 {
1529
1530 log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
1531 }
1532