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