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