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