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