ffs_alloc.c revision 1.35.4.4 1 /* $NetBSD: ffs_alloc.c,v 1.35.4.4 2001/11/25 20:01:55 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 return (fs->fs_fpg * cg + fs->fs_frag);
812 }
813 for (cg = 0; cg < startcg; cg++)
814 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
815 return (fs->fs_fpg * cg + fs->fs_frag);
816 }
817 return (0);
818 }
819 /*
820 * One or more previous blocks have been laid out. If less
821 * than fs_maxcontig previous blocks are contiguous, the
822 * next block is requested contiguously, otherwise it is
823 * requested rotationally delayed by fs_rotdelay milliseconds.
824 */
825 nextblk = ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
826 if (indx < fs->fs_maxcontig ||
827 ufs_rw32(bap[indx - fs->fs_maxcontig], UFS_FSNEEDSWAP(fs)) +
828 blkstofrags(fs, fs->fs_maxcontig) != nextblk)
829 return (nextblk);
830 if (fs->fs_rotdelay != 0)
831 /*
832 * Here we convert ms of delay to frags as:
833 * (frags) = (ms) * (rev/sec) * (sect/rev) /
834 * ((sect/frag) * (ms/sec))
835 * then round up to the next block.
836 */
837 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
838 (NSPF(fs) * 1000), fs->fs_frag);
839 return (nextblk);
840 }
841
842 /*
843 * Implement the cylinder overflow algorithm.
844 *
845 * The policy implemented by this algorithm is:
846 * 1) allocate the block in its requested cylinder group.
847 * 2) quadradically rehash on the cylinder group number.
848 * 3) brute force search for a free block.
849 */
850 /*VARARGS5*/
851 static u_long
852 ffs_hashalloc(ip, cg, pref, size, allocator)
853 struct inode *ip;
854 int cg;
855 long pref;
856 int size; /* size for data blocks, mode for inodes */
857 ufs_daddr_t (*allocator) __P((struct inode *, int, ufs_daddr_t, int));
858 {
859 struct fs *fs;
860 long result;
861 int i, icg = cg;
862
863 fs = ip->i_fs;
864 /*
865 * 1: preferred cylinder group
866 */
867 result = (*allocator)(ip, cg, pref, size);
868 if (result)
869 return (result);
870 /*
871 * 2: quadratic rehash
872 */
873 for (i = 1; i < fs->fs_ncg; i *= 2) {
874 cg += i;
875 if (cg >= fs->fs_ncg)
876 cg -= fs->fs_ncg;
877 result = (*allocator)(ip, cg, 0, size);
878 if (result)
879 return (result);
880 }
881 /*
882 * 3: brute force search
883 * Note that we start at i == 2, since 0 was checked initially,
884 * and 1 is always checked in the quadratic rehash.
885 */
886 cg = (icg + 2) % fs->fs_ncg;
887 for (i = 2; i < fs->fs_ncg; i++) {
888 result = (*allocator)(ip, cg, 0, size);
889 if (result)
890 return (result);
891 cg++;
892 if (cg == fs->fs_ncg)
893 cg = 0;
894 }
895 return (0);
896 }
897
898 /*
899 * Determine whether a fragment can be extended.
900 *
901 * Check to see if the necessary fragments are available, and
902 * if they are, allocate them.
903 */
904 static ufs_daddr_t
905 ffs_fragextend(ip, cg, bprev, osize, nsize)
906 struct inode *ip;
907 int cg;
908 long bprev;
909 int osize, nsize;
910 {
911 struct fs *fs;
912 struct cg *cgp;
913 struct buf *bp;
914 long bno;
915 int frags, bbase;
916 int i, error;
917
918 fs = ip->i_fs;
919 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
920 return (0);
921 frags = numfrags(fs, nsize);
922 bbase = fragnum(fs, bprev);
923 if (bbase > fragnum(fs, (bprev + frags - 1))) {
924 /* cannot extend across a block boundary */
925 return (0);
926 }
927 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
928 (int)fs->fs_cgsize, NOCRED, &bp);
929 if (error) {
930 brelse(bp);
931 return (0);
932 }
933 cgp = (struct cg *)bp->b_data;
934 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
935 brelse(bp);
936 return (0);
937 }
938 cgp->cg_time = ufs_rw32(time.tv_sec, UFS_FSNEEDSWAP(fs));
939 bno = dtogd(fs, bprev);
940 for (i = numfrags(fs, osize); i < frags; i++)
941 if (isclr(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i)) {
942 brelse(bp);
943 return (0);
944 }
945 /*
946 * the current fragment can be extended
947 * deduct the count on fragment being extended into
948 * increase the count on the remaining fragment (if any)
949 * allocate the extended piece
950 */
951 for (i = frags; i < fs->fs_frag - bbase; i++)
952 if (isclr(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i))
953 break;
954 ufs_add32(cgp->cg_frsum[i - numfrags(fs, osize)], -1, UFS_FSNEEDSWAP(fs));
955 if (i != frags)
956 ufs_add32(cgp->cg_frsum[i - frags], 1, UFS_FSNEEDSWAP(fs));
957 for (i = numfrags(fs, osize); i < frags; i++) {
958 clrbit(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i);
959 ufs_add32(cgp->cg_cs.cs_nffree, -1, UFS_FSNEEDSWAP(fs));
960 fs->fs_cstotal.cs_nffree--;
961 fs->fs_cs(fs, cg).cs_nffree--;
962 }
963 fs->fs_fmod = 1;
964 if (DOINGSOFTDEP(ITOV(ip)))
965 softdep_setup_blkmapdep(bp, fs, bprev);
966 bdwrite(bp);
967 return (bprev);
968 }
969
970 /*
971 * Determine whether a block can be allocated.
972 *
973 * Check to see if a block of the appropriate size is available,
974 * and if it is, allocate it.
975 */
976 static ufs_daddr_t
977 ffs_alloccg(ip, cg, bpref, size)
978 struct inode *ip;
979 int cg;
980 ufs_daddr_t bpref;
981 int size;
982 {
983 struct cg *cgp;
984 struct buf *bp;
985 ufs_daddr_t bno, blkno;
986 int error, frags, allocsiz, i;
987 struct fs *fs = ip->i_fs;
988 #ifdef FFS_EI
989 const int needswap = UFS_FSNEEDSWAP(fs);
990 #endif
991
992 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
993 return (0);
994 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
995 (int)fs->fs_cgsize, NOCRED, &bp);
996 if (error) {
997 brelse(bp);
998 return (0);
999 }
1000 cgp = (struct cg *)bp->b_data;
1001 if (!cg_chkmagic(cgp, needswap) ||
1002 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
1003 brelse(bp);
1004 return (0);
1005 }
1006 cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
1007 if (size == fs->fs_bsize) {
1008 bno = ffs_alloccgblk(ip, bp, bpref);
1009 bdwrite(bp);
1010 return (bno);
1011 }
1012 /*
1013 * check to see if any fragments are already available
1014 * allocsiz is the size which will be allocated, hacking
1015 * it down to a smaller size if necessary
1016 */
1017 frags = numfrags(fs, size);
1018 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1019 if (cgp->cg_frsum[allocsiz] != 0)
1020 break;
1021 if (allocsiz == fs->fs_frag) {
1022 /*
1023 * no fragments were available, so a block will be
1024 * allocated, and hacked up
1025 */
1026 if (cgp->cg_cs.cs_nbfree == 0) {
1027 brelse(bp);
1028 return (0);
1029 }
1030 bno = ffs_alloccgblk(ip, bp, bpref);
1031 bpref = dtogd(fs, bno);
1032 for (i = frags; i < fs->fs_frag; i++)
1033 setbit(cg_blksfree(cgp, needswap), bpref + i);
1034 i = fs->fs_frag - frags;
1035 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1036 fs->fs_cstotal.cs_nffree += i;
1037 fs->fs_cs(fs, cg).cs_nffree += i;
1038 fs->fs_fmod = 1;
1039 ufs_add32(cgp->cg_frsum[i], 1, needswap);
1040 bdwrite(bp);
1041 return (bno);
1042 }
1043 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1044 #if 0
1045 /*
1046 * XXX fvdl mapsearch will panic, and never return -1
1047 * also: returning NULL as ufs_daddr_t ?
1048 */
1049 if (bno < 0) {
1050 brelse(bp);
1051 return (0);
1052 }
1053 #endif
1054 for (i = 0; i < frags; i++)
1055 clrbit(cg_blksfree(cgp, needswap), bno + i);
1056 ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
1057 fs->fs_cstotal.cs_nffree -= frags;
1058 fs->fs_cs(fs, cg).cs_nffree -= frags;
1059 fs->fs_fmod = 1;
1060 ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
1061 if (frags != allocsiz)
1062 ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
1063 blkno = cg * fs->fs_fpg + bno;
1064 if (DOINGSOFTDEP(ITOV(ip)))
1065 softdep_setup_blkmapdep(bp, fs, blkno);
1066 bdwrite(bp);
1067 return blkno;
1068 }
1069
1070 /*
1071 * Allocate a block in a cylinder group.
1072 *
1073 * This algorithm implements the following policy:
1074 * 1) allocate the requested block.
1075 * 2) allocate a rotationally optimal block in the same cylinder.
1076 * 3) allocate the next available block on the block rotor for the
1077 * specified cylinder group.
1078 * Note that this routine only allocates fs_bsize blocks; these
1079 * blocks may be fragmented by the routine that allocates them.
1080 */
1081 static ufs_daddr_t
1082 ffs_alloccgblk(ip, bp, bpref)
1083 struct inode *ip;
1084 struct buf *bp;
1085 ufs_daddr_t bpref;
1086 {
1087 struct cg *cgp;
1088 ufs_daddr_t bno, blkno;
1089 int cylno, pos, delta;
1090 short *cylbp;
1091 int i;
1092 struct fs *fs = ip->i_fs;
1093 #ifdef FFS_EI
1094 const int needswap = UFS_FSNEEDSWAP(fs);
1095 #endif
1096
1097 cgp = (struct cg *)bp->b_data;
1098 if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
1099 bpref = ufs_rw32(cgp->cg_rotor, needswap);
1100 goto norot;
1101 }
1102 bpref = blknum(fs, bpref);
1103 bpref = dtogd(fs, bpref);
1104 /*
1105 * if the requested block is available, use it
1106 */
1107 if (ffs_isblock(fs, cg_blksfree(cgp, needswap),
1108 fragstoblks(fs, bpref))) {
1109 bno = bpref;
1110 goto gotit;
1111 }
1112 if (fs->fs_nrpos <= 1 || fs->fs_cpc == 0) {
1113 /*
1114 * Block layout information is not available.
1115 * Leaving bpref unchanged means we take the
1116 * next available free block following the one
1117 * we just allocated. Hopefully this will at
1118 * least hit a track cache on drives of unknown
1119 * geometry (e.g. SCSI).
1120 */
1121 goto norot;
1122 }
1123 /*
1124 * check for a block available on the same cylinder
1125 */
1126 cylno = cbtocylno(fs, bpref);
1127 if (cg_blktot(cgp, needswap)[cylno] == 0)
1128 goto norot;
1129 /*
1130 * check the summary information to see if a block is
1131 * available in the requested cylinder starting at the
1132 * requested rotational position and proceeding around.
1133 */
1134 cylbp = cg_blks(fs, cgp, cylno, needswap);
1135 pos = cbtorpos(fs, bpref);
1136 for (i = pos; i < fs->fs_nrpos; i++)
1137 if (ufs_rw16(cylbp[i], needswap) > 0)
1138 break;
1139 if (i == fs->fs_nrpos)
1140 for (i = 0; i < pos; i++)
1141 if (ufs_rw16(cylbp[i], needswap) > 0)
1142 break;
1143 if (ufs_rw16(cylbp[i], needswap) > 0) {
1144 /*
1145 * found a rotational position, now find the actual
1146 * block. A panic if none is actually there.
1147 */
1148 pos = cylno % fs->fs_cpc;
1149 bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
1150 if (fs_postbl(fs, pos)[i] == -1) {
1151 printf("pos = %d, i = %d, fs = %s\n",
1152 pos, i, fs->fs_fsmnt);
1153 panic("ffs_alloccgblk: cyl groups corrupted");
1154 }
1155 for (i = fs_postbl(fs, pos)[i];; ) {
1156 if (ffs_isblock(fs, cg_blksfree(cgp, needswap), bno + i)) {
1157 bno = blkstofrags(fs, (bno + i));
1158 goto gotit;
1159 }
1160 delta = fs_rotbl(fs)[i];
1161 if (delta <= 0 ||
1162 delta + i > fragstoblks(fs, fs->fs_fpg))
1163 break;
1164 i += delta;
1165 }
1166 printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
1167 panic("ffs_alloccgblk: can't find blk in cyl");
1168 }
1169 norot:
1170 /*
1171 * no blocks in the requested cylinder, so take next
1172 * available one in this cylinder group.
1173 */
1174 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1175 if (bno < 0)
1176 return (0);
1177 cgp->cg_rotor = ufs_rw32(bno, needswap);
1178 gotit:
1179 blkno = fragstoblks(fs, bno);
1180 ffs_clrblock(fs, cg_blksfree(cgp, needswap), (long)blkno);
1181 ffs_clusteracct(fs, cgp, blkno, -1);
1182 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1183 fs->fs_cstotal.cs_nbfree--;
1184 fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
1185 cylno = cbtocylno(fs, bno);
1186 ufs_add16(cg_blks(fs, cgp, cylno, needswap)[cbtorpos(fs, bno)], -1,
1187 needswap);
1188 ufs_add32(cg_blktot(cgp, needswap)[cylno], -1, needswap);
1189 fs->fs_fmod = 1;
1190 blkno = ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno;
1191 if (DOINGSOFTDEP(ITOV(ip)))
1192 softdep_setup_blkmapdep(bp, fs, blkno);
1193 return (blkno);
1194 }
1195
1196 /*
1197 * Determine whether a cluster can be allocated.
1198 *
1199 * We do not currently check for optimal rotational layout if there
1200 * are multiple choices in the same cylinder group. Instead we just
1201 * take the first one that we find following bpref.
1202 */
1203 static ufs_daddr_t
1204 ffs_clusteralloc(ip, cg, bpref, len)
1205 struct inode *ip;
1206 int cg;
1207 ufs_daddr_t bpref;
1208 int len;
1209 {
1210 struct fs *fs;
1211 struct cg *cgp;
1212 struct buf *bp;
1213 int i, got, run, bno, bit, map;
1214 u_char *mapp;
1215 int32_t *lp;
1216
1217 fs = ip->i_fs;
1218 if (fs->fs_maxcluster[cg] < len)
1219 return (0);
1220 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1221 NOCRED, &bp))
1222 goto fail;
1223 cgp = (struct cg *)bp->b_data;
1224 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs)))
1225 goto fail;
1226 /*
1227 * Check to see if a cluster of the needed size (or bigger) is
1228 * available in this cylinder group.
1229 */
1230 lp = &cg_clustersum(cgp, UFS_FSNEEDSWAP(fs))[len];
1231 for (i = len; i <= fs->fs_contigsumsize; i++)
1232 if (ufs_rw32(*lp++, UFS_FSNEEDSWAP(fs)) > 0)
1233 break;
1234 if (i > fs->fs_contigsumsize) {
1235 /*
1236 * This is the first time looking for a cluster in this
1237 * cylinder group. Update the cluster summary information
1238 * to reflect the true maximum sized cluster so that
1239 * future cluster allocation requests can avoid reading
1240 * the cylinder group map only to find no clusters.
1241 */
1242 lp = &cg_clustersum(cgp, UFS_FSNEEDSWAP(fs))[len - 1];
1243 for (i = len - 1; i > 0; i--)
1244 if (ufs_rw32(*lp--, UFS_FSNEEDSWAP(fs)) > 0)
1245 break;
1246 fs->fs_maxcluster[cg] = i;
1247 goto fail;
1248 }
1249 /*
1250 * Search the cluster map to find a big enough cluster.
1251 * We take the first one that we find, even if it is larger
1252 * than we need as we prefer to get one close to the previous
1253 * block allocation. We do not search before the current
1254 * preference point as we do not want to allocate a block
1255 * that is allocated before the previous one (as we will
1256 * then have to wait for another pass of the elevator
1257 * algorithm before it will be read). We prefer to fail and
1258 * be recalled to try an allocation in the next cylinder group.
1259 */
1260 if (dtog(fs, bpref) != cg)
1261 bpref = 0;
1262 else
1263 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1264 mapp = &cg_clustersfree(cgp, UFS_FSNEEDSWAP(fs))[bpref / NBBY];
1265 map = *mapp++;
1266 bit = 1 << (bpref % NBBY);
1267 for (run = 0, got = bpref;
1268 got < ufs_rw32(cgp->cg_nclusterblks, UFS_FSNEEDSWAP(fs)); got++) {
1269 if ((map & bit) == 0) {
1270 run = 0;
1271 } else {
1272 run++;
1273 if (run == len)
1274 break;
1275 }
1276 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1277 bit <<= 1;
1278 } else {
1279 map = *mapp++;
1280 bit = 1;
1281 }
1282 }
1283 if (got == ufs_rw32(cgp->cg_nclusterblks, UFS_FSNEEDSWAP(fs)))
1284 goto fail;
1285 /*
1286 * Allocate the cluster that we have found.
1287 */
1288 #ifdef DIAGNOSTIC
1289 for (i = 1; i <= len; i++)
1290 if (!ffs_isblock(fs, cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)),
1291 got - run + i))
1292 panic("ffs_clusteralloc: map mismatch");
1293 #endif
1294 bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1);
1295 if (dtog(fs, bno) != cg)
1296 panic("ffs_clusteralloc: allocated out of group");
1297 len = blkstofrags(fs, len);
1298 for (i = 0; i < len; i += fs->fs_frag)
1299 if ((got = ffs_alloccgblk(ip, bp, bno + i)) != bno + i)
1300 panic("ffs_clusteralloc: lost block");
1301 bdwrite(bp);
1302 return (bno);
1303
1304 fail:
1305 brelse(bp);
1306 return (0);
1307 }
1308
1309 /*
1310 * Determine whether an inode can be allocated.
1311 *
1312 * Check to see if an inode is available, and if it is,
1313 * allocate it using the following policy:
1314 * 1) allocate the requested inode.
1315 * 2) allocate the next available inode after the requested
1316 * inode in the specified cylinder group.
1317 */
1318 static ufs_daddr_t
1319 ffs_nodealloccg(ip, cg, ipref, mode)
1320 struct inode *ip;
1321 int cg;
1322 ufs_daddr_t ipref;
1323 int mode;
1324 {
1325 struct cg *cgp;
1326 struct buf *bp;
1327 int error, start, len, loc, map, i;
1328 struct fs *fs = ip->i_fs;
1329 #ifdef FFS_EI
1330 const int needswap = UFS_FSNEEDSWAP(fs);
1331 #endif
1332
1333 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1334 return (0);
1335 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1336 (int)fs->fs_cgsize, NOCRED, &bp);
1337 if (error) {
1338 brelse(bp);
1339 return (0);
1340 }
1341 cgp = (struct cg *)bp->b_data;
1342 if (!cg_chkmagic(cgp, needswap) || cgp->cg_cs.cs_nifree == 0) {
1343 brelse(bp);
1344 return (0);
1345 }
1346 cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
1347 if (ipref) {
1348 ipref %= fs->fs_ipg;
1349 if (isclr(cg_inosused(cgp, needswap), ipref))
1350 goto gotit;
1351 }
1352 start = ufs_rw32(cgp->cg_irotor, needswap) / NBBY;
1353 len = howmany(fs->fs_ipg - ufs_rw32(cgp->cg_irotor, needswap),
1354 NBBY);
1355 loc = skpc(0xff, len, &cg_inosused(cgp, needswap)[start]);
1356 if (loc == 0) {
1357 len = start + 1;
1358 start = 0;
1359 loc = skpc(0xff, len, &cg_inosused(cgp, needswap)[0]);
1360 if (loc == 0) {
1361 printf("cg = %d, irotor = %d, fs = %s\n",
1362 cg, ufs_rw32(cgp->cg_irotor, needswap),
1363 fs->fs_fsmnt);
1364 panic("ffs_nodealloccg: map corrupted");
1365 /* NOTREACHED */
1366 }
1367 }
1368 i = start + len - loc;
1369 map = cg_inosused(cgp, needswap)[i];
1370 ipref = i * NBBY;
1371 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1372 if ((map & i) == 0) {
1373 cgp->cg_irotor = ufs_rw32(ipref, needswap);
1374 goto gotit;
1375 }
1376 }
1377 printf("fs = %s\n", fs->fs_fsmnt);
1378 panic("ffs_nodealloccg: block not in map");
1379 /* NOTREACHED */
1380 gotit:
1381 if (DOINGSOFTDEP(ITOV(ip)))
1382 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1383 setbit(cg_inosused(cgp, needswap), ipref);
1384 ufs_add32(cgp->cg_cs.cs_nifree, -1, needswap);
1385 fs->fs_cstotal.cs_nifree--;
1386 fs->fs_cs(fs, cg).cs_nifree--;
1387 fs->fs_fmod = 1;
1388 if ((mode & IFMT) == IFDIR) {
1389 ufs_add32(cgp->cg_cs.cs_ndir, 1, needswap);
1390 fs->fs_cstotal.cs_ndir++;
1391 fs->fs_cs(fs, cg).cs_ndir++;
1392 }
1393 bdwrite(bp);
1394 return (cg * fs->fs_ipg + ipref);
1395 }
1396
1397 /*
1398 * Free a block or fragment.
1399 *
1400 * The specified block or fragment is placed back in the
1401 * free map. If a fragment is deallocated, a possible
1402 * block reassembly is checked.
1403 */
1404 void
1405 ffs_blkfree(ip, bno, size)
1406 struct inode *ip;
1407 ufs_daddr_t bno;
1408 long size;
1409 {
1410 struct cg *cgp;
1411 struct buf *bp;
1412 ufs_daddr_t blkno;
1413 int i, error, cg, blk, frags, bbase;
1414 struct fs *fs = ip->i_fs;
1415 const int needswap = UFS_FSNEEDSWAP(fs);
1416
1417 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1418 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1419 printf("dev = 0x%x, bno = %u bsize = %d, size = %ld, fs = %s\n",
1420 ip->i_dev, bno, fs->fs_bsize, size, fs->fs_fsmnt);
1421 panic("blkfree: bad size");
1422 }
1423 cg = dtog(fs, bno);
1424 if ((u_int)bno >= fs->fs_size) {
1425 printf("bad block %d, ino %d\n", bno, ip->i_number);
1426 ffs_fserr(fs, ip->i_ffs_uid, "bad block");
1427 return;
1428 }
1429 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1430 (int)fs->fs_cgsize, NOCRED, &bp);
1431 if (error) {
1432 brelse(bp);
1433 return;
1434 }
1435 cgp = (struct cg *)bp->b_data;
1436 if (!cg_chkmagic(cgp, needswap)) {
1437 brelse(bp);
1438 return;
1439 }
1440 cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
1441 bno = dtogd(fs, bno);
1442 if (size == fs->fs_bsize) {
1443 blkno = fragstoblks(fs, bno);
1444 if (!ffs_isfreeblock(fs, cg_blksfree(cgp, needswap), blkno)) {
1445 printf("dev = 0x%x, block = %d, fs = %s\n",
1446 ip->i_dev, bno, fs->fs_fsmnt);
1447 panic("blkfree: freeing free block");
1448 }
1449 ffs_setblock(fs, cg_blksfree(cgp, needswap), blkno);
1450 ffs_clusteracct(fs, cgp, blkno, 1);
1451 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1452 fs->fs_cstotal.cs_nbfree++;
1453 fs->fs_cs(fs, cg).cs_nbfree++;
1454 i = cbtocylno(fs, bno);
1455 ufs_add16(cg_blks(fs, cgp, i, needswap)[cbtorpos(fs, bno)], 1,
1456 needswap);
1457 ufs_add32(cg_blktot(cgp, needswap)[i], 1, needswap);
1458 } else {
1459 bbase = bno - fragnum(fs, bno);
1460 /*
1461 * decrement the counts associated with the old frags
1462 */
1463 blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
1464 ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
1465 /*
1466 * deallocate the fragment
1467 */
1468 frags = numfrags(fs, size);
1469 for (i = 0; i < frags; i++) {
1470 if (isset(cg_blksfree(cgp, needswap), bno + i)) {
1471 printf("dev = 0x%x, block = %d, fs = %s\n",
1472 ip->i_dev, bno + i, fs->fs_fsmnt);
1473 panic("blkfree: freeing free frag");
1474 }
1475 setbit(cg_blksfree(cgp, needswap), bno + i);
1476 }
1477 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1478 fs->fs_cstotal.cs_nffree += i;
1479 fs->fs_cs(fs, cg).cs_nffree += i;
1480 /*
1481 * add back in counts associated with the new frags
1482 */
1483 blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
1484 ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
1485 /*
1486 * if a complete block has been reassembled, account for it
1487 */
1488 blkno = fragstoblks(fs, bbase);
1489 if (ffs_isblock(fs, cg_blksfree(cgp, needswap), blkno)) {
1490 ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
1491 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1492 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1493 ffs_clusteracct(fs, cgp, blkno, 1);
1494 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1495 fs->fs_cstotal.cs_nbfree++;
1496 fs->fs_cs(fs, cg).cs_nbfree++;
1497 i = cbtocylno(fs, bbase);
1498 ufs_add16(cg_blks(fs, cgp, i, needswap)[cbtorpos(fs,
1499 bbase)], 1,
1500 needswap);
1501 ufs_add32(cg_blktot(cgp, needswap)[i], 1, needswap);
1502 }
1503 }
1504 fs->fs_fmod = 1;
1505 bdwrite(bp);
1506 }
1507
1508 #if defined(DIAGNOSTIC) || defined(DEBUG)
1509 /*
1510 * Verify allocation of a block or fragment. Returns true if block or
1511 * fragment is allocated, false if it is free.
1512 */
1513 static int
1514 ffs_checkblk(ip, bno, size)
1515 struct inode *ip;
1516 ufs_daddr_t bno;
1517 long size;
1518 {
1519 struct fs *fs;
1520 struct cg *cgp;
1521 struct buf *bp;
1522 int i, error, frags, free;
1523
1524 fs = ip->i_fs;
1525 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1526 printf("bsize = %d, size = %ld, fs = %s\n",
1527 fs->fs_bsize, size, fs->fs_fsmnt);
1528 panic("checkblk: bad size");
1529 }
1530 if ((u_int)bno >= fs->fs_size)
1531 panic("checkblk: bad block %d", bno);
1532 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
1533 (int)fs->fs_cgsize, NOCRED, &bp);
1534 if (error) {
1535 brelse(bp);
1536 return 0;
1537 }
1538 cgp = (struct cg *)bp->b_data;
1539 if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
1540 brelse(bp);
1541 return 0;
1542 }
1543 bno = dtogd(fs, bno);
1544 if (size == fs->fs_bsize) {
1545 free = ffs_isblock(fs, cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)),
1546 fragstoblks(fs, bno));
1547 } else {
1548 frags = numfrags(fs, size);
1549 for (free = 0, i = 0; i < frags; i++)
1550 if (isset(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i))
1551 free++;
1552 if (free != 0 && free != frags)
1553 panic("checkblk: partially free fragment");
1554 }
1555 brelse(bp);
1556 return (!free);
1557 }
1558 #endif /* DIAGNOSTIC */
1559
1560 /*
1561 * Free an inode.
1562 */
1563 int
1564 ffs_vfree(v)
1565 void *v;
1566 {
1567 struct vop_vfree_args /* {
1568 struct vnode *a_pvp;
1569 ino_t a_ino;
1570 int a_mode;
1571 } */ *ap = v;
1572
1573 if (DOINGSOFTDEP(ap->a_pvp)) {
1574 softdep_freefile(ap);
1575 return (0);
1576 }
1577 return (ffs_freefile(ap));
1578 }
1579
1580 /*
1581 * Do the actual free operation.
1582 * The specified inode is placed back in the free map.
1583 */
1584 int
1585 ffs_freefile(v)
1586 void *v;
1587 {
1588 struct vop_vfree_args /* {
1589 struct vnode *a_pvp;
1590 ino_t a_ino;
1591 int a_mode;
1592 } */ *ap = v;
1593 struct cg *cgp;
1594 struct inode *pip = VTOI(ap->a_pvp);
1595 struct fs *fs = pip->i_fs;
1596 ino_t ino = ap->a_ino;
1597 struct buf *bp;
1598 int error, cg;
1599 #ifdef FFS_EI
1600 const int needswap = UFS_FSNEEDSWAP(fs);
1601 #endif
1602
1603 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1604 panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
1605 pip->i_dev, ino, fs->fs_fsmnt);
1606 cg = ino_to_cg(fs, ino);
1607 error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1608 (int)fs->fs_cgsize, NOCRED, &bp);
1609 if (error) {
1610 brelse(bp);
1611 return (error);
1612 }
1613 cgp = (struct cg *)bp->b_data;
1614 if (!cg_chkmagic(cgp, needswap)) {
1615 brelse(bp);
1616 return (0);
1617 }
1618 cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
1619 ino %= fs->fs_ipg;
1620 if (isclr(cg_inosused(cgp, needswap), ino)) {
1621 printf("dev = 0x%x, ino = %d, fs = %s\n",
1622 pip->i_dev, ino, fs->fs_fsmnt);
1623 if (fs->fs_ronly == 0)
1624 panic("ifree: freeing free inode");
1625 }
1626 clrbit(cg_inosused(cgp, needswap), ino);
1627 if (ino < ufs_rw32(cgp->cg_irotor, needswap))
1628 cgp->cg_irotor = ufs_rw32(ino, needswap);
1629 ufs_add32(cgp->cg_cs.cs_nifree, 1, needswap);
1630 fs->fs_cstotal.cs_nifree++;
1631 fs->fs_cs(fs, cg).cs_nifree++;
1632 if ((ap->a_mode & IFMT) == IFDIR) {
1633 ufs_add32(cgp->cg_cs.cs_ndir, -1, needswap);
1634 fs->fs_cstotal.cs_ndir--;
1635 fs->fs_cs(fs, cg).cs_ndir--;
1636 }
1637 fs->fs_fmod = 1;
1638 bdwrite(bp);
1639 return (0);
1640 }
1641
1642 /*
1643 * Find a block of the specified size in the specified cylinder group.
1644 *
1645 * It is a panic if a request is made to find a block if none are
1646 * available.
1647 */
1648 static ufs_daddr_t
1649 ffs_mapsearch(fs, cgp, bpref, allocsiz)
1650 struct fs *fs;
1651 struct cg *cgp;
1652 ufs_daddr_t bpref;
1653 int allocsiz;
1654 {
1655 ufs_daddr_t bno;
1656 int start, len, loc, i;
1657 int blk, field, subfield, pos;
1658 int ostart, olen;
1659 #ifdef FFS_EI
1660 const int needswap = UFS_FSNEEDSWAP(fs);
1661 #endif
1662
1663 /*
1664 * find the fragment by searching through the free block
1665 * map for an appropriate bit pattern
1666 */
1667 if (bpref)
1668 start = dtogd(fs, bpref) / NBBY;
1669 else
1670 start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
1671 len = howmany(fs->fs_fpg, NBBY) - start;
1672 ostart = start;
1673 olen = len;
1674 loc = scanc((u_int)len,
1675 (const u_char *)&cg_blksfree(cgp, needswap)[start],
1676 (const u_char *)fragtbl[fs->fs_frag],
1677 (1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1678 if (loc == 0) {
1679 len = start + 1;
1680 start = 0;
1681 loc = scanc((u_int)len,
1682 (const u_char *)&cg_blksfree(cgp, needswap)[0],
1683 (const u_char *)fragtbl[fs->fs_frag],
1684 (1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1685 if (loc == 0) {
1686 printf("start = %d, len = %d, fs = %s\n",
1687 ostart, olen, fs->fs_fsmnt);
1688 printf("offset=%d %ld\n",
1689 ufs_rw32(cgp->cg_freeoff, needswap),
1690 (long)cg_blksfree(cgp, needswap) - (long)cgp);
1691 panic("ffs_alloccg: map corrupted");
1692 /* NOTREACHED */
1693 }
1694 }
1695 bno = (start + len - loc) * NBBY;
1696 cgp->cg_frotor = ufs_rw32(bno, needswap);
1697 /*
1698 * found the byte in the map
1699 * sift through the bits to find the selected frag
1700 */
1701 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1702 blk = blkmap(fs, cg_blksfree(cgp, needswap), bno);
1703 blk <<= 1;
1704 field = around[allocsiz];
1705 subfield = inside[allocsiz];
1706 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
1707 if ((blk & field) == subfield)
1708 return (bno + pos);
1709 field <<= 1;
1710 subfield <<= 1;
1711 }
1712 }
1713 printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
1714 panic("ffs_alloccg: block not in map");
1715 return (-1);
1716 }
1717
1718 /*
1719 * Update the cluster map because of an allocation or free.
1720 *
1721 * Cnt == 1 means free; cnt == -1 means allocating.
1722 */
1723 void
1724 ffs_clusteracct(fs, cgp, blkno, cnt)
1725 struct fs *fs;
1726 struct cg *cgp;
1727 ufs_daddr_t blkno;
1728 int cnt;
1729 {
1730 int32_t *sump;
1731 int32_t *lp;
1732 u_char *freemapp, *mapp;
1733 int i, start, end, forw, back, map, bit;
1734 #ifdef FFS_EI
1735 const int needswap = UFS_FSNEEDSWAP(fs);
1736 #endif
1737
1738 if (fs->fs_contigsumsize <= 0)
1739 return;
1740 freemapp = cg_clustersfree(cgp, needswap);
1741 sump = cg_clustersum(cgp, needswap);
1742 /*
1743 * Allocate or clear the actual block.
1744 */
1745 if (cnt > 0)
1746 setbit(freemapp, blkno);
1747 else
1748 clrbit(freemapp, blkno);
1749 /*
1750 * Find the size of the cluster going forward.
1751 */
1752 start = blkno + 1;
1753 end = start + fs->fs_contigsumsize;
1754 if (end >= ufs_rw32(cgp->cg_nclusterblks, needswap))
1755 end = ufs_rw32(cgp->cg_nclusterblks, needswap);
1756 mapp = &freemapp[start / NBBY];
1757 map = *mapp++;
1758 bit = 1 << (start % NBBY);
1759 for (i = start; i < end; i++) {
1760 if ((map & bit) == 0)
1761 break;
1762 if ((i & (NBBY - 1)) != (NBBY - 1)) {
1763 bit <<= 1;
1764 } else {
1765 map = *mapp++;
1766 bit = 1;
1767 }
1768 }
1769 forw = i - start;
1770 /*
1771 * Find the size of the cluster going backward.
1772 */
1773 start = blkno - 1;
1774 end = start - fs->fs_contigsumsize;
1775 if (end < 0)
1776 end = -1;
1777 mapp = &freemapp[start / NBBY];
1778 map = *mapp--;
1779 bit = 1 << (start % NBBY);
1780 for (i = start; i > end; i--) {
1781 if ((map & bit) == 0)
1782 break;
1783 if ((i & (NBBY - 1)) != 0) {
1784 bit >>= 1;
1785 } else {
1786 map = *mapp--;
1787 bit = 1 << (NBBY - 1);
1788 }
1789 }
1790 back = start - i;
1791 /*
1792 * Account for old cluster and the possibly new forward and
1793 * back clusters.
1794 */
1795 i = back + forw + 1;
1796 if (i > fs->fs_contigsumsize)
1797 i = fs->fs_contigsumsize;
1798 ufs_add32(sump[i], cnt, needswap);
1799 if (back > 0)
1800 ufs_add32(sump[back], -cnt, needswap);
1801 if (forw > 0)
1802 ufs_add32(sump[forw], -cnt, needswap);
1803
1804 /*
1805 * Update cluster summary information.
1806 */
1807 lp = &sump[fs->fs_contigsumsize];
1808 for (i = fs->fs_contigsumsize; i > 0; i--)
1809 if (ufs_rw32(*lp--, needswap) > 0)
1810 break;
1811 fs->fs_maxcluster[ufs_rw32(cgp->cg_cgx, needswap)] = i;
1812 }
1813
1814 /*
1815 * Fserr prints the name of a file system with an error diagnostic.
1816 *
1817 * The form of the error message is:
1818 * fs: error message
1819 */
1820 static void
1821 ffs_fserr(fs, uid, cp)
1822 struct fs *fs;
1823 u_int uid;
1824 char *cp;
1825 {
1826
1827 log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
1828 }
1829