2 * Copyright (c) 2002 Networks Associates Technology, Inc.
5 * This software was developed for the FreeBSD Project by Marshall
6 * Kirk McKusick and Network Associates Laboratories, the Security
7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Copyright (c) 1982, 1986, 1989, 1993
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)ffs_alloc.c 8.18 (Berkeley) 5/26/95
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
65 #include "opt_quota.h"
67 #include <sys/param.h>
68 #include <sys/systm.h>
73 #include <sys/filedesc.h>
75 #include <sys/vnode.h>
76 #include <sys/mount.h>
77 #include <sys/kernel.h>
78 #include <sys/sysctl.h>
79 #include <sys/syslog.h>
81 #include <ufs/ufs/extattr.h>
82 #include <ufs/ufs/quota.h>
83 #include <ufs/ufs/inode.h>
84 #include <ufs/ufs/ufs_extern.h>
85 #include <ufs/ufs/ufsmount.h>
87 #include <ufs/ffs/fs.h>
88 #include <ufs/ffs/ffs_extern.h>
90 typedef ufs2_daddr_t allocfcn_t(struct inode *ip, int cg, ufs2_daddr_t bpref,
93 static ufs2_daddr_t ffs_alloccg(struct inode *, int, ufs2_daddr_t, int);
95 ffs_alloccgblk(struct inode *, struct buf *, ufs2_daddr_t);
97 static int ffs_checkblk(struct inode *, ufs2_daddr_t, long);
99 static ufs2_daddr_t ffs_clusteralloc(struct inode *, int, ufs2_daddr_t, int);
100 static void ffs_clusteracct(struct ufsmount *, struct fs *, struct cg *,
102 static ino_t ffs_dirpref(struct inode *);
103 static ufs2_daddr_t ffs_fragextend(struct inode *, int, ufs2_daddr_t, int, int);
104 static void ffs_fserr(struct fs *, ino_t, char *);
105 static ufs2_daddr_t ffs_hashalloc
106 (struct inode *, int, ufs2_daddr_t, int, allocfcn_t *);
107 static ufs2_daddr_t ffs_nodealloccg(struct inode *, int, ufs2_daddr_t, int);
108 static ufs1_daddr_t ffs_mapsearch(struct fs *, struct cg *, ufs2_daddr_t, int);
109 static int ffs_reallocblks_ufs1(struct vop_reallocblks_args *);
110 static int ffs_reallocblks_ufs2(struct vop_reallocblks_args *);
113 * Allocate a block in the filesystem.
115 * The size of the requested block is given, which must be some
116 * multiple of fs_fsize and <= fs_bsize.
117 * A preference may be optionally specified. If a preference is given
118 * the following hierarchy is used to allocate a block:
119 * 1) allocate the requested block.
120 * 2) allocate a rotationally optimal block in the same cylinder.
121 * 3) allocate a block in the same cylinder group.
122 * 4) quadradically rehash into other cylinder groups, until an
123 * available block is located.
124 * If no block preference is given the following hierarchy is used
125 * to allocate a block:
126 * 1) allocate a block in the cylinder group that contains the
127 * inode for the file.
128 * 2) quadradically rehash into other cylinder groups, until an
129 * available block is located.
132 ffs_alloc(ip, lbn, bpref, size, cred, bnp)
134 ufs2_daddr_t lbn, bpref;
140 struct ufsmount *ump;
143 static struct timeval lastfail;
153 mtx_assert(UFS_MTX(ump), MA_OWNED);
155 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
156 printf("dev = %s, bsize = %ld, size = %d, fs = %s\n",
157 devtoname(ip->i_dev), (long)fs->fs_bsize, size,
159 panic("ffs_alloc: bad size");
162 panic("ffs_alloc: missing credential");
163 #endif /* INVARIANTS */
168 error = chkdq(ip, btodb(size), cred, 0);
173 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
175 if (suser_cred(cred, SUSER_ALLOWJAIL) &&
176 freespace(fs, fs->fs_minfree) - numfrags(fs, size) < 0)
178 if (bpref >= fs->fs_size)
181 cg = ino_to_cg(fs, ip->i_number);
183 cg = dtog(fs, bpref);
184 bno = ffs_hashalloc(ip, cg, bpref, size, ffs_alloccg);
187 if (ip->i_flag & IN_SPACECOUNTED) {
189 fs->fs_pendingblocks += delta;
192 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
193 ip->i_flag |= IN_CHANGE | IN_UPDATE;
201 * Restore user's disk quota because allocation failed.
203 (void) chkdq(ip, -btodb(size), cred, FORCE);
206 if (fs->fs_pendingblocks > 0 && reclaimed == 0) {
208 softdep_request_cleanup(fs, ITOV(ip));
212 if (ppsratecheck(&lastfail, &curfail, 1)) {
213 ffs_fserr(fs, ip->i_number, "filesystem full");
214 uprintf("\n%s: write failed, filesystem is full\n",
221 * Reallocate a fragment to a bigger size
223 * The number and size of the old block is given, and a preference
224 * and new size is also specified. The allocator attempts to extend
225 * the original block. Failing that, the regular block allocator is
226 * invoked to get an appropriate block.
229 ffs_realloccg(ip, lbprev, bprev, bpref, osize, nsize, cred, bpp)
241 struct ufsmount *ump;
242 int cg, request, error, reclaimed;
244 static struct timeval lastfail;
253 mtx_assert(UFS_MTX(ump), MA_OWNED);
255 if (vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
256 panic("ffs_realloccg: allocation on suspended filesystem");
257 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
258 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
260 "dev = %s, bsize = %ld, osize = %d, nsize = %d, fs = %s\n",
261 devtoname(ip->i_dev), (long)fs->fs_bsize, osize,
262 nsize, fs->fs_fsmnt);
263 panic("ffs_realloccg: bad size");
266 panic("ffs_realloccg: missing credential");
267 #endif /* INVARIANTS */
270 if (suser_cred(cred, SUSER_ALLOWJAIL) &&
271 freespace(fs, fs->fs_minfree) - numfrags(fs, nsize - osize) < 0) {
275 printf("dev = %s, bsize = %ld, bprev = %jd, fs = %s\n",
276 devtoname(ip->i_dev), (long)fs->fs_bsize, (intmax_t)bprev,
278 panic("ffs_realloccg: bad bprev");
282 * Allocate the extra space in the buffer.
284 error = bread(vp, lbprev, osize, NOCRED, &bp);
290 if (bp->b_blkno == bp->b_lblkno) {
291 if (lbprev >= NDADDR)
292 panic("ffs_realloccg: lbprev out of range");
293 bp->b_blkno = fsbtodb(fs, bprev);
297 error = chkdq(ip, btodb(nsize - osize), cred, 0);
304 * Check for extension in the existing location.
306 cg = dtog(fs, bprev);
308 bno = ffs_fragextend(ip, cg, bprev, osize, nsize);
310 if (bp->b_blkno != fsbtodb(fs, bno))
311 panic("ffs_realloccg: bad blockno");
312 delta = btodb(nsize - osize);
313 if (ip->i_flag & IN_SPACECOUNTED) {
315 fs->fs_pendingblocks += delta;
318 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
319 ip->i_flag |= IN_CHANGE | IN_UPDATE;
321 bp->b_flags |= B_DONE;
322 if ((bp->b_flags & (B_MALLOC | B_VMIO)) != B_VMIO)
323 bzero((char *)bp->b_data + osize, nsize - osize);
330 * Allocate a new disk location.
332 if (bpref >= fs->fs_size)
334 switch ((int)fs->fs_optim) {
337 * Allocate an exact sized fragment. Although this makes
338 * best use of space, we will waste time relocating it if
339 * the file continues to grow. If the fragmentation is
340 * less than half of the minimum free reserve, we choose
341 * to begin optimizing for time.
344 if (fs->fs_minfree <= 5 ||
345 fs->fs_cstotal.cs_nffree >
346 (off_t)fs->fs_dsize * fs->fs_minfree / (2 * 100))
348 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
350 fs->fs_optim = FS_OPTTIME;
354 * At this point we have discovered a file that is trying to
355 * grow a small fragment to a larger fragment. To save time,
356 * we allocate a full sized block, then free the unused portion.
357 * If the file continues to grow, the `ffs_fragextend' call
358 * above will be able to grow it in place without further
359 * copying. If aberrant programs cause disk fragmentation to
360 * grow within 2% of the free reserve, we choose to begin
361 * optimizing for space.
363 request = fs->fs_bsize;
364 if (fs->fs_cstotal.cs_nffree <
365 (off_t)fs->fs_dsize * (fs->fs_minfree - 2) / 100)
367 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
369 fs->fs_optim = FS_OPTSPACE;
372 printf("dev = %s, optim = %ld, fs = %s\n",
373 devtoname(ip->i_dev), (long)fs->fs_optim, fs->fs_fsmnt);
374 panic("ffs_realloccg: bad optim");
377 bno = ffs_hashalloc(ip, cg, bpref, request, ffs_alloccg);
379 bp->b_blkno = fsbtodb(fs, bno);
380 if (!DOINGSOFTDEP(vp))
381 ffs_blkfree(ump, fs, ip->i_devvp, bprev, (long)osize,
384 ffs_blkfree(ump, fs, ip->i_devvp,
385 bno + numfrags(fs, nsize),
386 (long)(request - nsize), ip->i_number);
387 delta = btodb(nsize - osize);
388 if (ip->i_flag & IN_SPACECOUNTED) {
390 fs->fs_pendingblocks += delta;
393 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
394 ip->i_flag |= IN_CHANGE | IN_UPDATE;
396 bp->b_flags |= B_DONE;
397 if ((bp->b_flags & (B_MALLOC | B_VMIO)) != B_VMIO)
398 bzero((char *)bp->b_data + osize, nsize - osize);
407 * Restore user's disk quota because allocation failed.
409 (void) chkdq(ip, -btodb(nsize - osize), cred, FORCE);
416 if (fs->fs_pendingblocks > 0 && reclaimed == 0) {
418 softdep_request_cleanup(fs, vp);
428 if (ppsratecheck(&lastfail, &curfail, 1)) {
429 ffs_fserr(fs, ip->i_number, "filesystem full");
430 uprintf("\n%s: write failed, filesystem is full\n",
437 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
439 * The vnode and an array of buffer pointers for a range of sequential
440 * logical blocks to be made contiguous is given. The allocator attempts
441 * to find a range of sequential blocks starting as close as possible
442 * from the end of the allocation for the logical block immediately
443 * preceding the current range. If successful, the physical block numbers
444 * in the buffer pointers and in the inode are changed to reflect the new
445 * allocation. If unsuccessful, the allocation is left unchanged. The
446 * success in doing the reallocation is returned. Note that the error
447 * return is not reflected back to the user. Rather the previous block
448 * allocation will be used.
451 SYSCTL_NODE(_vfs, OID_AUTO, ffs, CTLFLAG_RW, 0, "FFS filesystem");
453 static int doasyncfree = 1;
454 SYSCTL_INT(_vfs_ffs, OID_AUTO, doasyncfree, CTLFLAG_RW, &doasyncfree, 0, "");
456 static int doreallocblks = 1;
457 SYSCTL_INT(_vfs_ffs, OID_AUTO, doreallocblks, CTLFLAG_RW, &doreallocblks, 0, "");
460 static volatile int prtrealloc = 0;
465 struct vop_reallocblks_args /* {
467 struct cluster_save *a_buflist;
471 if (doreallocblks == 0)
473 if (VTOI(ap->a_vp)->i_ump->um_fstype == UFS1)
474 return (ffs_reallocblks_ufs1(ap));
475 return (ffs_reallocblks_ufs2(ap));
479 ffs_reallocblks_ufs1(ap)
480 struct vop_reallocblks_args /* {
482 struct cluster_save *a_buflist;
488 struct buf *sbp, *ebp;
489 ufs1_daddr_t *bap, *sbap, *ebap = 0;
490 struct cluster_save *buflist;
491 struct ufsmount *ump;
492 ufs_lbn_t start_lbn, end_lbn;
493 ufs1_daddr_t soff, newblk, blkno;
495 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
496 int i, len, start_lvl, end_lvl, ssize;
502 if (fs->fs_contigsumsize <= 0)
504 buflist = ap->a_buflist;
505 len = buflist->bs_nchildren;
506 start_lbn = buflist->bs_children[0]->b_lblkno;
507 end_lbn = start_lbn + len - 1;
509 for (i = 0; i < len; i++)
510 if (!ffs_checkblk(ip,
511 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
512 panic("ffs_reallocblks: unallocated block 1");
513 for (i = 1; i < len; i++)
514 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
515 panic("ffs_reallocblks: non-logical cluster");
516 blkno = buflist->bs_children[0]->b_blkno;
517 ssize = fsbtodb(fs, fs->fs_frag);
518 for (i = 1; i < len - 1; i++)
519 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
520 panic("ffs_reallocblks: non-physical cluster %d", i);
523 * If the latest allocation is in a new cylinder group, assume that
524 * the filesystem has decided to move and do not force it back to
525 * the previous cylinder group.
527 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
528 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
530 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
531 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
534 * Get the starting offset and block map for the first block.
536 if (start_lvl == 0) {
537 sbap = &ip->i_din1->di_db[0];
540 idp = &start_ap[start_lvl - 1];
541 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
545 sbap = (ufs1_daddr_t *)sbp->b_data;
549 * If the block range spans two block maps, get the second map.
551 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
556 start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
557 panic("ffs_reallocblk: start == end");
559 ssize = len - (idp->in_off + 1);
560 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
562 ebap = (ufs1_daddr_t *)ebp->b_data;
565 * Find the preferred location for the cluster.
568 pref = ffs_blkpref_ufs1(ip, start_lbn, soff, sbap);
570 * Search the block map looking for an allocation of the desired size.
572 if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref,
573 len, ffs_clusteralloc)) == 0) {
578 * We have found a new contiguous block.
580 * First we have to replace the old block pointers with the new
581 * block pointers in the inode and indirect blocks associated
586 printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
587 (intmax_t)start_lbn, (intmax_t)end_lbn);
590 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
596 if (!ffs_checkblk(ip,
597 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
598 panic("ffs_reallocblks: unallocated block 2");
599 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
600 panic("ffs_reallocblks: alloc mismatch");
604 printf(" %d,", *bap);
606 if (DOINGSOFTDEP(vp)) {
607 if (sbap == &ip->i_din1->di_db[0] && i < ssize)
608 softdep_setup_allocdirect(ip, start_lbn + i,
609 blkno, *bap, fs->fs_bsize, fs->fs_bsize,
610 buflist->bs_children[i]);
612 softdep_setup_allocindir_page(ip, start_lbn + i,
613 i < ssize ? sbp : ebp, soff + i, blkno,
614 *bap, buflist->bs_children[i]);
619 * Next we must write out the modified inode and indirect blocks.
620 * For strict correctness, the writes should be synchronous since
621 * the old block values may have been written to disk. In practise
622 * they are almost never written, but if we are concerned about
623 * strict correctness, the `doasyncfree' flag should be set to zero.
625 * The test on `doasyncfree' should be changed to test a flag
626 * that shows whether the associated buffers and inodes have
627 * been written. The flag should be set when the cluster is
628 * started and cleared whenever the buffer or inode is flushed.
629 * We can then check below to see if it is set, and do the
630 * synchronous write only when it has been cleared.
632 if (sbap != &ip->i_din1->di_db[0]) {
638 ip->i_flag |= IN_CHANGE | IN_UPDATE;
649 * Last, free the old blocks and assign the new blocks to the buffers.
655 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
656 if (!DOINGSOFTDEP(vp))
657 ffs_blkfree(ump, fs, ip->i_devvp,
658 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
659 fs->fs_bsize, ip->i_number);
660 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
662 if (!ffs_checkblk(ip,
663 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
664 panic("ffs_reallocblks: unallocated block 3");
668 printf(" %d,", blkno);
682 if (sbap != &ip->i_din1->di_db[0])
688 ffs_reallocblks_ufs2(ap)
689 struct vop_reallocblks_args /* {
691 struct cluster_save *a_buflist;
697 struct buf *sbp, *ebp;
698 ufs2_daddr_t *bap, *sbap, *ebap = 0;
699 struct cluster_save *buflist;
700 struct ufsmount *ump;
701 ufs_lbn_t start_lbn, end_lbn;
702 ufs2_daddr_t soff, newblk, blkno, pref;
703 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
704 int i, len, start_lvl, end_lvl, ssize;
710 if (fs->fs_contigsumsize <= 0)
712 buflist = ap->a_buflist;
713 len = buflist->bs_nchildren;
714 start_lbn = buflist->bs_children[0]->b_lblkno;
715 end_lbn = start_lbn + len - 1;
717 for (i = 0; i < len; i++)
718 if (!ffs_checkblk(ip,
719 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
720 panic("ffs_reallocblks: unallocated block 1");
721 for (i = 1; i < len; i++)
722 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
723 panic("ffs_reallocblks: non-logical cluster");
724 blkno = buflist->bs_children[0]->b_blkno;
725 ssize = fsbtodb(fs, fs->fs_frag);
726 for (i = 1; i < len - 1; i++)
727 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
728 panic("ffs_reallocblks: non-physical cluster %d", i);
731 * If the latest allocation is in a new cylinder group, assume that
732 * the filesystem has decided to move and do not force it back to
733 * the previous cylinder group.
735 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
736 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
738 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
739 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
742 * Get the starting offset and block map for the first block.
744 if (start_lvl == 0) {
745 sbap = &ip->i_din2->di_db[0];
748 idp = &start_ap[start_lvl - 1];
749 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
753 sbap = (ufs2_daddr_t *)sbp->b_data;
757 * If the block range spans two block maps, get the second map.
759 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
764 start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
765 panic("ffs_reallocblk: start == end");
767 ssize = len - (idp->in_off + 1);
768 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
770 ebap = (ufs2_daddr_t *)ebp->b_data;
773 * Find the preferred location for the cluster.
776 pref = ffs_blkpref_ufs2(ip, start_lbn, soff, sbap);
778 * Search the block map looking for an allocation of the desired size.
780 if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref,
781 len, ffs_clusteralloc)) == 0) {
786 * We have found a new contiguous block.
788 * First we have to replace the old block pointers with the new
789 * block pointers in the inode and indirect blocks associated
794 printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
795 (intmax_t)start_lbn, (intmax_t)end_lbn);
798 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
804 if (!ffs_checkblk(ip,
805 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
806 panic("ffs_reallocblks: unallocated block 2");
807 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
808 panic("ffs_reallocblks: alloc mismatch");
812 printf(" %jd,", (intmax_t)*bap);
814 if (DOINGSOFTDEP(vp)) {
815 if (sbap == &ip->i_din2->di_db[0] && i < ssize)
816 softdep_setup_allocdirect(ip, start_lbn + i,
817 blkno, *bap, fs->fs_bsize, fs->fs_bsize,
818 buflist->bs_children[i]);
820 softdep_setup_allocindir_page(ip, start_lbn + i,
821 i < ssize ? sbp : ebp, soff + i, blkno,
822 *bap, buflist->bs_children[i]);
827 * Next we must write out the modified inode and indirect blocks.
828 * For strict correctness, the writes should be synchronous since
829 * the old block values may have been written to disk. In practise
830 * they are almost never written, but if we are concerned about
831 * strict correctness, the `doasyncfree' flag should be set to zero.
833 * The test on `doasyncfree' should be changed to test a flag
834 * that shows whether the associated buffers and inodes have
835 * been written. The flag should be set when the cluster is
836 * started and cleared whenever the buffer or inode is flushed.
837 * We can then check below to see if it is set, and do the
838 * synchronous write only when it has been cleared.
840 if (sbap != &ip->i_din2->di_db[0]) {
846 ip->i_flag |= IN_CHANGE | IN_UPDATE;
857 * Last, free the old blocks and assign the new blocks to the buffers.
863 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
864 if (!DOINGSOFTDEP(vp))
865 ffs_blkfree(ump, fs, ip->i_devvp,
866 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
867 fs->fs_bsize, ip->i_number);
868 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
870 if (!ffs_checkblk(ip,
871 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
872 panic("ffs_reallocblks: unallocated block 3");
876 printf(" %jd,", (intmax_t)blkno);
890 if (sbap != &ip->i_din2->di_db[0])
896 * Allocate an inode in the filesystem.
898 * If allocating a directory, use ffs_dirpref to select the inode.
899 * If allocating in a directory, the following hierarchy is followed:
900 * 1) allocate the preferred inode.
901 * 2) allocate an inode in the same cylinder group.
902 * 3) quadradically rehash into other cylinder groups, until an
903 * available inode is located.
904 * If no inode preference is given the following hierarchy is used
905 * to allocate an inode:
906 * 1) allocate an inode in cylinder group 0.
907 * 2) quadradically rehash into other cylinder groups, until an
908 * available inode is located.
911 ffs_valloc(pvp, mode, cred, vpp)
921 struct ufsmount *ump;
924 static struct timeval lastfail;
933 if (fs->fs_cstotal.cs_nifree == 0)
936 if ((mode & IFMT) == IFDIR)
937 ipref = ffs_dirpref(pip);
939 ipref = pip->i_number;
940 if (ipref >= fs->fs_ncg * fs->fs_ipg)
942 cg = ino_to_cg(fs, ipref);
944 * Track number of dirs created one after another
945 * in a same cg without intervening by files.
947 if ((mode & IFMT) == IFDIR) {
948 if (fs->fs_contigdirs[cg] < 255)
949 fs->fs_contigdirs[cg]++;
951 if (fs->fs_contigdirs[cg] > 0)
952 fs->fs_contigdirs[cg]--;
954 ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode,
955 (allocfcn_t *)ffs_nodealloccg);
958 error = ffs_vget(pvp->v_mount, ino, LK_EXCLUSIVE, vpp);
960 ffs_vfree(pvp, ino, mode);
965 printf("mode = 0%o, inum = %lu, fs = %s\n",
966 ip->i_mode, (u_long)ip->i_number, fs->fs_fsmnt);
967 panic("ffs_valloc: dup alloc");
969 if (DIP(ip, i_blocks) && (fs->fs_flags & FS_UNCLEAN) == 0) { /* XXX */
970 printf("free inode %s/%lu had %ld blocks\n",
971 fs->fs_fsmnt, (u_long)ino, (long)DIP(ip, i_blocks));
972 DIP_SET(ip, i_blocks, 0);
975 DIP_SET(ip, i_flags, 0);
977 * Set up a new generation number for this inode.
979 if (ip->i_gen == 0 || ++ip->i_gen == 0)
980 ip->i_gen = arc4random() / 2 + 1;
981 DIP_SET(ip, i_gen, ip->i_gen);
982 if (fs->fs_magic == FS_UFS2_MAGIC) {
984 ip->i_din2->di_birthtime = ts.tv_sec;
985 ip->i_din2->di_birthnsec = ts.tv_nsec;
988 vnode_destroy_vobject(*vpp);
989 (*vpp)->v_type = VNON;
990 if (fs->fs_magic == FS_UFS2_MAGIC)
991 (*vpp)->v_op = &ffs_vnodeops2;
993 (*vpp)->v_op = &ffs_vnodeops1;
997 if (ppsratecheck(&lastfail, &curfail, 1)) {
998 ffs_fserr(fs, pip->i_number, "out of inodes");
999 uprintf("\n%s: create/symlink failed, no inodes free\n",
1006 * Find a cylinder group to place a directory.
1008 * The policy implemented by this algorithm is to allocate a
1009 * directory inode in the same cylinder group as its parent
1010 * directory, but also to reserve space for its files inodes
1011 * and data. Restrict the number of directories which may be
1012 * allocated one after another in the same cylinder group
1013 * without intervening allocation of files.
1015 * If we allocate a first level directory then force allocation
1016 * in another cylinder group.
1023 int cg, prefcg, dirsize, cgsize;
1024 int avgifree, avgbfree, avgndir, curdirsize;
1025 int minifree, minbfree, maxndir;
1029 mtx_assert(UFS_MTX(pip->i_ump), MA_OWNED);
1032 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
1033 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1034 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
1037 * Force allocation in another cg if creating a first level dir.
1039 ASSERT_VOP_LOCKED(ITOV(pip), "ffs_dirpref");
1040 if (ITOV(pip)->v_vflag & VV_ROOT) {
1041 prefcg = arc4random() % fs->fs_ncg;
1043 minndir = fs->fs_ipg;
1044 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1045 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1046 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1047 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1049 minndir = fs->fs_cs(fs, cg).cs_ndir;
1051 for (cg = 0; cg < prefcg; cg++)
1052 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1053 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1054 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1056 minndir = fs->fs_cs(fs, cg).cs_ndir;
1058 return ((ino_t)(fs->fs_ipg * mincg));
1062 * Count various limits which used for
1063 * optimal allocation of a directory inode.
1065 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
1066 minifree = avgifree - avgifree / 4;
1069 minbfree = avgbfree - avgbfree / 4;
1072 cgsize = fs->fs_fsize * fs->fs_fpg;
1073 dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
1074 curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
1075 if (dirsize < curdirsize)
1076 dirsize = curdirsize;
1078 maxcontigdirs = 0; /* dirsize overflowed */
1080 maxcontigdirs = min((avgbfree * fs->fs_bsize) / dirsize, 255);
1081 if (fs->fs_avgfpdir > 0)
1082 maxcontigdirs = min(maxcontigdirs,
1083 fs->fs_ipg / fs->fs_avgfpdir);
1084 if (maxcontigdirs == 0)
1088 * Limit number of dirs in one cg and reserve space for
1089 * regular files, but only if we have no deficit in
1092 prefcg = ino_to_cg(fs, pip->i_number);
1093 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1094 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1095 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1096 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1097 if (fs->fs_contigdirs[cg] < maxcontigdirs)
1098 return ((ino_t)(fs->fs_ipg * cg));
1100 for (cg = 0; cg < prefcg; cg++)
1101 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1102 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1103 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1104 if (fs->fs_contigdirs[cg] < maxcontigdirs)
1105 return ((ino_t)(fs->fs_ipg * cg));
1108 * This is a backstop when we have deficit in space.
1110 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1111 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1112 return ((ino_t)(fs->fs_ipg * cg));
1113 for (cg = 0; cg < prefcg; cg++)
1114 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1116 return ((ino_t)(fs->fs_ipg * cg));
1120 * Select the desired position for the next block in a file. The file is
1121 * logically divided into sections. The first section is composed of the
1122 * direct blocks. Each additional section contains fs_maxbpg blocks.
1124 * If no blocks have been allocated in the first section, the policy is to
1125 * request a block in the same cylinder group as the inode that describes
1126 * the file. If no blocks have been allocated in any other section, the
1127 * policy is to place the section in a cylinder group with a greater than
1128 * average number of free blocks. An appropriate cylinder group is found
1129 * by using a rotor that sweeps the cylinder groups. When a new group of
1130 * blocks is needed, the sweep begins in the cylinder group following the
1131 * cylinder group from which the previous allocation was made. The sweep
1132 * continues until a cylinder group with greater than the average number
1133 * of free blocks is found. If the allocation is for the first block in an
1134 * indirect block, the information on the previous allocation is unavailable;
1135 * here a best guess is made based upon the logical block number being
1138 * If a section is already partially allocated, the policy is to
1139 * contiguously allocate fs_maxcontig blocks. The end of one of these
1140 * contiguous blocks and the beginning of the next is laid out
1141 * contiguously if possible.
1144 ffs_blkpref_ufs1(ip, lbn, indx, bap)
1152 int avgbfree, startcg;
1154 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1156 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1157 if (lbn < NDADDR + NINDIR(fs)) {
1158 cg = ino_to_cg(fs, ip->i_number);
1159 return (cgbase(fs, cg) + fs->fs_frag);
1162 * Find a cylinder with greater than average number of
1163 * unused data blocks.
1165 if (indx == 0 || bap[indx - 1] == 0)
1167 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1169 startcg = dtog(fs, bap[indx - 1]) + 1;
1170 startcg %= fs->fs_ncg;
1171 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1172 for (cg = startcg; cg < fs->fs_ncg; cg++)
1173 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1174 fs->fs_cgrotor = cg;
1175 return (cgbase(fs, cg) + fs->fs_frag);
1177 for (cg = 0; cg <= startcg; cg++)
1178 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1179 fs->fs_cgrotor = cg;
1180 return (cgbase(fs, cg) + fs->fs_frag);
1185 * We just always try to lay things out contiguously.
1187 return (bap[indx - 1] + fs->fs_frag);
1191 * Same as above, but for UFS2
1194 ffs_blkpref_ufs2(ip, lbn, indx, bap)
1202 int avgbfree, startcg;
1204 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1206 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1207 if (lbn < NDADDR + NINDIR(fs)) {
1208 cg = ino_to_cg(fs, ip->i_number);
1209 return (cgbase(fs, cg) + fs->fs_frag);
1212 * Find a cylinder with greater than average number of
1213 * unused data blocks.
1215 if (indx == 0 || bap[indx - 1] == 0)
1217 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1219 startcg = dtog(fs, bap[indx - 1]) + 1;
1220 startcg %= fs->fs_ncg;
1221 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1222 for (cg = startcg; cg < fs->fs_ncg; cg++)
1223 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1224 fs->fs_cgrotor = cg;
1225 return (cgbase(fs, cg) + fs->fs_frag);
1227 for (cg = 0; cg <= startcg; cg++)
1228 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1229 fs->fs_cgrotor = cg;
1230 return (cgbase(fs, cg) + fs->fs_frag);
1235 * We just always try to lay things out contiguously.
1237 return (bap[indx - 1] + fs->fs_frag);
1241 * Implement the cylinder overflow algorithm.
1243 * The policy implemented by this algorithm is:
1244 * 1) allocate the block in its requested cylinder group.
1245 * 2) quadradically rehash on the cylinder group number.
1246 * 3) brute force search for a free block.
1248 * Must be called with the UFS lock held. Will release the lock on success
1249 * and return with it held on failure.
1253 ffs_hashalloc(ip, cg, pref, size, allocator)
1257 int size; /* size for data blocks, mode for inodes */
1258 allocfcn_t *allocator;
1261 ufs2_daddr_t result;
1264 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1266 if (ITOV(ip)->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
1267 panic("ffs_hashalloc: allocation on suspended filesystem");
1271 * 1: preferred cylinder group
1273 result = (*allocator)(ip, cg, pref, size);
1277 * 2: quadratic rehash
1279 for (i = 1; i < fs->fs_ncg; i *= 2) {
1281 if (cg >= fs->fs_ncg)
1283 result = (*allocator)(ip, cg, 0, size);
1288 * 3: brute force search
1289 * Note that we start at i == 2, since 0 was checked initially,
1290 * and 1 is always checked in the quadratic rehash.
1292 cg = (icg + 2) % fs->fs_ncg;
1293 for (i = 2; i < fs->fs_ncg; i++) {
1294 result = (*allocator)(ip, cg, 0, size);
1298 if (cg == fs->fs_ncg)
1305 * Determine whether a fragment can be extended.
1307 * Check to see if the necessary fragments are available, and
1308 * if they are, allocate them.
1311 ffs_fragextend(ip, cg, bprev, osize, nsize)
1320 struct ufsmount *ump;
1329 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
1331 frags = numfrags(fs, nsize);
1332 bbase = fragnum(fs, bprev);
1333 if (bbase > fragnum(fs, (bprev + frags - 1))) {
1334 /* cannot extend across a block boundary */
1338 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1339 (int)fs->fs_cgsize, NOCRED, &bp);
1342 cgp = (struct cg *)bp->b_data;
1343 if (!cg_chkmagic(cgp))
1345 bp->b_xflags |= BX_BKGRDWRITE;
1346 cgp->cg_old_time = cgp->cg_time = time_second;
1347 bno = dtogd(fs, bprev);
1348 blksfree = cg_blksfree(cgp);
1349 for (i = numfrags(fs, osize); i < frags; i++)
1350 if (isclr(blksfree, bno + i))
1353 * the current fragment can be extended
1354 * deduct the count on fragment being extended into
1355 * increase the count on the remaining fragment (if any)
1356 * allocate the extended piece
1358 for (i = frags; i < fs->fs_frag - bbase; i++)
1359 if (isclr(blksfree, bno + i))
1361 cgp->cg_frsum[i - numfrags(fs, osize)]--;
1363 cgp->cg_frsum[i - frags]++;
1364 for (i = numfrags(fs, osize), nffree = 0; i < frags; i++) {
1365 clrbit(blksfree, bno + i);
1366 cgp->cg_cs.cs_nffree--;
1370 fs->fs_cstotal.cs_nffree -= nffree;
1371 fs->fs_cs(fs, cg).cs_nffree -= nffree;
1373 ACTIVECLEAR(fs, cg);
1375 if (DOINGSOFTDEP(ITOV(ip)))
1376 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), bprev);
1388 * Determine whether a block can be allocated.
1390 * Check to see if a block of the appropriate size is available,
1391 * and if it is, allocate it.
1394 ffs_alloccg(ip, cg, bpref, size)
1403 struct ufsmount *ump;
1406 int i, allocsiz, error, frags;
1411 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1414 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1415 (int)fs->fs_cgsize, NOCRED, &bp);
1418 cgp = (struct cg *)bp->b_data;
1419 if (!cg_chkmagic(cgp) ||
1420 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize))
1422 bp->b_xflags |= BX_BKGRDWRITE;
1423 cgp->cg_old_time = cgp->cg_time = time_second;
1424 if (size == fs->fs_bsize) {
1426 blkno = ffs_alloccgblk(ip, bp, bpref);
1427 ACTIVECLEAR(fs, cg);
1433 * check to see if any fragments are already available
1434 * allocsiz is the size which will be allocated, hacking
1435 * it down to a smaller size if necessary
1437 blksfree = cg_blksfree(cgp);
1438 frags = numfrags(fs, size);
1439 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1440 if (cgp->cg_frsum[allocsiz] != 0)
1442 if (allocsiz == fs->fs_frag) {
1444 * no fragments were available, so a block will be
1445 * allocated, and hacked up
1447 if (cgp->cg_cs.cs_nbfree == 0)
1450 blkno = ffs_alloccgblk(ip, bp, bpref);
1451 bno = dtogd(fs, blkno);
1452 for (i = frags; i < fs->fs_frag; i++)
1453 setbit(blksfree, bno + i);
1454 i = fs->fs_frag - frags;
1455 cgp->cg_cs.cs_nffree += i;
1456 fs->fs_cstotal.cs_nffree += i;
1457 fs->fs_cs(fs, cg).cs_nffree += i;
1460 ACTIVECLEAR(fs, cg);
1465 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1468 for (i = 0; i < frags; i++)
1469 clrbit(blksfree, bno + i);
1470 cgp->cg_cs.cs_nffree -= frags;
1471 cgp->cg_frsum[allocsiz]--;
1472 if (frags != allocsiz)
1473 cgp->cg_frsum[allocsiz - frags]++;
1475 fs->fs_cstotal.cs_nffree -= frags;
1476 fs->fs_cs(fs, cg).cs_nffree -= frags;
1478 blkno = cgbase(fs, cg) + bno;
1479 ACTIVECLEAR(fs, cg);
1481 if (DOINGSOFTDEP(ITOV(ip)))
1482 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno);
1493 * Allocate a block in a cylinder group.
1495 * This algorithm implements the following policy:
1496 * 1) allocate the requested block.
1497 * 2) allocate a rotationally optimal block in the same cylinder.
1498 * 3) allocate the next available block on the block rotor for the
1499 * specified cylinder group.
1500 * Note that this routine only allocates fs_bsize blocks; these
1501 * blocks may be fragmented by the routine that allocates them.
1504 ffs_alloccgblk(ip, bp, bpref)
1511 struct ufsmount *ump;
1518 mtx_assert(UFS_MTX(ump), MA_OWNED);
1519 cgp = (struct cg *)bp->b_data;
1520 blksfree = cg_blksfree(cgp);
1521 if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
1522 bpref = cgp->cg_rotor;
1524 bpref = blknum(fs, bpref);
1525 bno = dtogd(fs, bpref);
1527 * if the requested block is available, use it
1529 if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
1533 * Take the next available block in this cylinder group.
1535 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1538 cgp->cg_rotor = bno;
1540 blkno = fragstoblks(fs, bno);
1541 ffs_clrblock(fs, blksfree, (long)blkno);
1542 ffs_clusteracct(ump, fs, cgp, blkno, -1);
1543 cgp->cg_cs.cs_nbfree--;
1544 fs->fs_cstotal.cs_nbfree--;
1545 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1547 blkno = cgbase(fs, cgp->cg_cgx) + bno;
1550 if (DOINGSOFTDEP(ITOV(ip)))
1551 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno);
1557 * Determine whether a cluster can be allocated.
1559 * We do not currently check for optimal rotational layout if there
1560 * are multiple choices in the same cylinder group. Instead we just
1561 * take the first one that we find following bpref.
1564 ffs_clusteralloc(ip, cg, bpref, len)
1573 struct ufsmount *ump;
1574 int i, run, bit, map, got;
1582 if (fs->fs_maxcluster[cg] < len)
1585 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1588 cgp = (struct cg *)bp->b_data;
1589 if (!cg_chkmagic(cgp))
1591 bp->b_xflags |= BX_BKGRDWRITE;
1593 * Check to see if a cluster of the needed size (or bigger) is
1594 * available in this cylinder group.
1596 lp = &cg_clustersum(cgp)[len];
1597 for (i = len; i <= fs->fs_contigsumsize; i++)
1600 if (i > fs->fs_contigsumsize) {
1602 * This is the first time looking for a cluster in this
1603 * cylinder group. Update the cluster summary information
1604 * to reflect the true maximum sized cluster so that
1605 * future cluster allocation requests can avoid reading
1606 * the cylinder group map only to find no clusters.
1608 lp = &cg_clustersum(cgp)[len - 1];
1609 for (i = len - 1; i > 0; i--)
1613 fs->fs_maxcluster[cg] = i;
1617 * Search the cluster map to find a big enough cluster.
1618 * We take the first one that we find, even if it is larger
1619 * than we need as we prefer to get one close to the previous
1620 * block allocation. We do not search before the current
1621 * preference point as we do not want to allocate a block
1622 * that is allocated before the previous one (as we will
1623 * then have to wait for another pass of the elevator
1624 * algorithm before it will be read). We prefer to fail and
1625 * be recalled to try an allocation in the next cylinder group.
1627 if (dtog(fs, bpref) != cg)
1630 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1631 mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1633 bit = 1 << (bpref % NBBY);
1634 for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
1635 if ((map & bit) == 0) {
1642 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1649 if (got >= cgp->cg_nclusterblks)
1652 * Allocate the cluster that we have found.
1654 blksfree = cg_blksfree(cgp);
1655 for (i = 1; i <= len; i++)
1656 if (!ffs_isblock(fs, blksfree, got - run + i))
1657 panic("ffs_clusteralloc: map mismatch");
1658 bno = cgbase(fs, cg) + blkstofrags(fs, got - run + 1);
1659 if (dtog(fs, bno) != cg)
1660 panic("ffs_clusteralloc: allocated out of group");
1661 len = blkstofrags(fs, len);
1663 for (i = 0; i < len; i += fs->fs_frag)
1664 if (ffs_alloccgblk(ip, bp, bno + i) != bno + i)
1665 panic("ffs_clusteralloc: lost block");
1666 ACTIVECLEAR(fs, cg);
1679 * Determine whether an inode can be allocated.
1681 * Check to see if an inode is available, and if it is,
1682 * allocate it using the following policy:
1683 * 1) allocate the requested inode.
1684 * 2) allocate the next available inode after the requested
1685 * inode in the specified cylinder group.
1688 ffs_nodealloccg(ip, cg, ipref, mode)
1696 struct buf *bp, *ibp;
1697 struct ufsmount *ump;
1699 struct ufs2_dinode *dp2;
1700 int error, start, len, loc, map, i;
1704 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1707 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1708 (int)fs->fs_cgsize, NOCRED, &bp);
1714 cgp = (struct cg *)bp->b_data;
1715 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
1720 bp->b_xflags |= BX_BKGRDWRITE;
1721 cgp->cg_old_time = cgp->cg_time = time_second;
1722 inosused = cg_inosused(cgp);
1724 ipref %= fs->fs_ipg;
1725 if (isclr(inosused, ipref))
1728 start = cgp->cg_irotor / NBBY;
1729 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
1730 loc = skpc(0xff, len, &inosused[start]);
1734 loc = skpc(0xff, len, &inosused[0]);
1736 printf("cg = %d, irotor = %ld, fs = %s\n",
1737 cg, (long)cgp->cg_irotor, fs->fs_fsmnt);
1738 panic("ffs_nodealloccg: map corrupted");
1742 i = start + len - loc;
1745 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1746 if ((map & i) == 0) {
1747 cgp->cg_irotor = ipref;
1751 printf("fs = %s\n", fs->fs_fsmnt);
1752 panic("ffs_nodealloccg: block not in map");
1756 * Check to see if we need to initialize more inodes.
1759 if (fs->fs_magic == FS_UFS2_MAGIC &&
1760 ipref + INOPB(fs) > cgp->cg_initediblk &&
1761 cgp->cg_initediblk < cgp->cg_niblk) {
1762 ibp = getblk(ip->i_devvp, fsbtodb(fs,
1763 ino_to_fsba(fs, cg * fs->fs_ipg + cgp->cg_initediblk)),
1764 (int)fs->fs_bsize, 0, 0, 0);
1765 bzero(ibp->b_data, (int)fs->fs_bsize);
1766 dp2 = (struct ufs2_dinode *)(ibp->b_data);
1767 for (i = 0; i < INOPB(fs); i++) {
1768 dp2->di_gen = arc4random() / 2 + 1;
1771 cgp->cg_initediblk += INOPB(fs);
1774 ACTIVECLEAR(fs, cg);
1775 setbit(inosused, ipref);
1776 cgp->cg_cs.cs_nifree--;
1777 fs->fs_cstotal.cs_nifree--;
1778 fs->fs_cs(fs, cg).cs_nifree--;
1780 if ((mode & IFMT) == IFDIR) {
1781 cgp->cg_cs.cs_ndir++;
1782 fs->fs_cstotal.cs_ndir++;
1783 fs->fs_cs(fs, cg).cs_ndir++;
1786 if (DOINGSOFTDEP(ITOV(ip)))
1787 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1791 return (cg * fs->fs_ipg + ipref);
1795 * check if a block is free
1798 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
1801 switch ((int)fs->fs_frag) {
1803 return (cp[h] == 0);
1805 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
1807 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
1809 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
1811 panic("ffs_isfreeblock");
1817 * Free a block or fragment.
1819 * The specified block or fragment is placed back in the
1820 * free map. If a fragment is deallocated, a possible
1821 * block reassembly is checked.
1824 ffs_blkfree(ump, fs, devvp, bno, size, inum)
1825 struct ufsmount *ump;
1827 struct vnode *devvp;
1834 ufs1_daddr_t fragno, cgbno;
1835 ufs2_daddr_t cgblkno;
1836 int i, cg, blk, frags, bbase;
1841 if (devvp->v_type != VCHR) {
1842 /* devvp is a snapshot */
1843 dev = VTOI(devvp)->i_devvp->v_rdev;
1844 cgblkno = fragstoblks(fs, cgtod(fs, cg));
1846 /* devvp is a normal disk device */
1847 dev = devvp->v_rdev;
1848 cgblkno = fsbtodb(fs, cgtod(fs, cg));
1849 ASSERT_VOP_LOCKED(devvp, "ffs_blkfree");
1850 if ((devvp->v_vflag & VV_COPYONWRITE) &&
1851 ffs_snapblkfree(fs, devvp, bno, size, inum))
1855 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1856 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1857 printf("dev=%s, bno = %jd, bsize = %ld, size = %ld, fs = %s\n",
1858 devtoname(dev), (intmax_t)bno, (long)fs->fs_bsize,
1859 size, fs->fs_fsmnt);
1860 panic("ffs_blkfree: bad size");
1863 if ((u_int)bno >= fs->fs_size) {
1864 printf("bad block %jd, ino %lu\n", (intmax_t)bno,
1866 ffs_fserr(fs, inum, "bad block");
1869 if (bread(devvp, cgblkno, (int)fs->fs_cgsize, NOCRED, &bp)) {
1873 cgp = (struct cg *)bp->b_data;
1874 if (!cg_chkmagic(cgp)) {
1878 bp->b_xflags |= BX_BKGRDWRITE;
1879 cgp->cg_old_time = cgp->cg_time = time_second;
1880 cgbno = dtogd(fs, bno);
1881 blksfree = cg_blksfree(cgp);
1883 if (size == fs->fs_bsize) {
1884 fragno = fragstoblks(fs, cgbno);
1885 if (!ffs_isfreeblock(fs, blksfree, fragno)) {
1886 if (devvp->v_type != VCHR) {
1888 /* devvp is a snapshot */
1892 printf("dev = %s, block = %jd, fs = %s\n",
1893 devtoname(dev), (intmax_t)bno, fs->fs_fsmnt);
1894 panic("ffs_blkfree: freeing free block");
1896 ffs_setblock(fs, blksfree, fragno);
1897 ffs_clusteracct(ump, fs, cgp, fragno, 1);
1898 cgp->cg_cs.cs_nbfree++;
1899 fs->fs_cstotal.cs_nbfree++;
1900 fs->fs_cs(fs, cg).cs_nbfree++;
1902 bbase = cgbno - fragnum(fs, cgbno);
1904 * decrement the counts associated with the old frags
1906 blk = blkmap(fs, blksfree, bbase);
1907 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
1909 * deallocate the fragment
1911 frags = numfrags(fs, size);
1912 for (i = 0; i < frags; i++) {
1913 if (isset(blksfree, cgbno + i)) {
1914 printf("dev = %s, block = %jd, fs = %s\n",
1915 devtoname(dev), (intmax_t)(bno + i),
1917 panic("ffs_blkfree: freeing free frag");
1919 setbit(blksfree, cgbno + i);
1921 cgp->cg_cs.cs_nffree += i;
1922 fs->fs_cstotal.cs_nffree += i;
1923 fs->fs_cs(fs, cg).cs_nffree += i;
1925 * add back in counts associated with the new frags
1927 blk = blkmap(fs, blksfree, bbase);
1928 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
1930 * if a complete block has been reassembled, account for it
1932 fragno = fragstoblks(fs, bbase);
1933 if (ffs_isblock(fs, blksfree, fragno)) {
1934 cgp->cg_cs.cs_nffree -= fs->fs_frag;
1935 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1936 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1937 ffs_clusteracct(ump, fs, cgp, fragno, 1);
1938 cgp->cg_cs.cs_nbfree++;
1939 fs->fs_cstotal.cs_nbfree++;
1940 fs->fs_cs(fs, cg).cs_nbfree++;
1944 ACTIVECLEAR(fs, cg);
1951 * Verify allocation of a block or fragment. Returns true if block or
1952 * fragment is allocated, false if it is free.
1955 ffs_checkblk(ip, bno, size)
1964 int i, error, frags, free;
1968 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1969 printf("bsize = %ld, size = %ld, fs = %s\n",
1970 (long)fs->fs_bsize, size, fs->fs_fsmnt);
1971 panic("ffs_checkblk: bad size");
1973 if ((u_int)bno >= fs->fs_size)
1974 panic("ffs_checkblk: bad block %jd", (intmax_t)bno);
1975 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
1976 (int)fs->fs_cgsize, NOCRED, &bp);
1978 panic("ffs_checkblk: cg bread failed");
1979 cgp = (struct cg *)bp->b_data;
1980 if (!cg_chkmagic(cgp))
1981 panic("ffs_checkblk: cg magic mismatch");
1982 bp->b_xflags |= BX_BKGRDWRITE;
1983 blksfree = cg_blksfree(cgp);
1984 cgbno = dtogd(fs, bno);
1985 if (size == fs->fs_bsize) {
1986 free = ffs_isblock(fs, blksfree, fragstoblks(fs, cgbno));
1988 frags = numfrags(fs, size);
1989 for (free = 0, i = 0; i < frags; i++)
1990 if (isset(blksfree, cgbno + i))
1992 if (free != 0 && free != frags)
1993 panic("ffs_checkblk: partially free fragment");
1998 #endif /* INVARIANTS */
2004 ffs_vfree(pvp, ino, mode)
2011 if (DOINGSOFTDEP(pvp)) {
2012 softdep_freefile(pvp, ino, mode);
2016 return (ffs_freefile(ip->i_ump, ip->i_fs, ip->i_devvp, ino, mode));
2020 * Do the actual free operation.
2021 * The specified inode is placed back in the free map.
2024 ffs_freefile(ump, fs, devvp, ino, mode)
2025 struct ufsmount *ump;
2027 struct vnode *devvp;
2038 cg = ino_to_cg(fs, ino);
2039 if (devvp->v_type != VCHR) {
2040 /* devvp is a snapshot */
2041 dev = VTOI(devvp)->i_devvp->v_rdev;
2042 cgbno = fragstoblks(fs, cgtod(fs, cg));
2044 /* devvp is a normal disk device */
2045 dev = devvp->v_rdev;
2046 cgbno = fsbtodb(fs, cgtod(fs, cg));
2048 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
2049 panic("ffs_freefile: range: dev = %s, ino = %lu, fs = %s",
2050 devtoname(dev), (u_long)ino, fs->fs_fsmnt);
2051 if ((error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp))) {
2055 cgp = (struct cg *)bp->b_data;
2056 if (!cg_chkmagic(cgp)) {
2060 bp->b_xflags |= BX_BKGRDWRITE;
2061 cgp->cg_old_time = cgp->cg_time = time_second;
2062 inosused = cg_inosused(cgp);
2064 if (isclr(inosused, ino)) {
2065 printf("dev = %s, ino = %lu, fs = %s\n", devtoname(dev),
2066 (u_long)ino + cg * fs->fs_ipg, fs->fs_fsmnt);
2067 if (fs->fs_ronly == 0)
2068 panic("ffs_freefile: freeing free inode");
2070 clrbit(inosused, ino);
2071 if (ino < cgp->cg_irotor)
2072 cgp->cg_irotor = ino;
2073 cgp->cg_cs.cs_nifree++;
2075 fs->fs_cstotal.cs_nifree++;
2076 fs->fs_cs(fs, cg).cs_nifree++;
2077 if ((mode & IFMT) == IFDIR) {
2078 cgp->cg_cs.cs_ndir--;
2079 fs->fs_cstotal.cs_ndir--;
2080 fs->fs_cs(fs, cg).cs_ndir--;
2083 ACTIVECLEAR(fs, cg);
2090 * Check to see if a file is free.
2093 ffs_checkfreefile(fs, devvp, ino)
2095 struct vnode *devvp;
2104 cg = ino_to_cg(fs, ino);
2105 if (devvp->v_type != VCHR) {
2106 /* devvp is a snapshot */
2107 cgbno = fragstoblks(fs, cgtod(fs, cg));
2109 /* devvp is a normal disk device */
2110 cgbno = fsbtodb(fs, cgtod(fs, cg));
2112 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
2114 if (bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp)) {
2118 cgp = (struct cg *)bp->b_data;
2119 if (!cg_chkmagic(cgp)) {
2123 inosused = cg_inosused(cgp);
2125 ret = isclr(inosused, ino);
2131 * Find a block of the specified size in the specified cylinder group.
2133 * It is a panic if a request is made to find a block if none are
2137 ffs_mapsearch(fs, cgp, bpref, allocsiz)
2144 int start, len, loc, i;
2145 int blk, field, subfield, pos;
2149 * find the fragment by searching through the free block
2150 * map for an appropriate bit pattern
2153 start = dtogd(fs, bpref) / NBBY;
2155 start = cgp->cg_frotor / NBBY;
2156 blksfree = cg_blksfree(cgp);
2157 len = howmany(fs->fs_fpg, NBBY) - start;
2158 loc = scanc((u_int)len, (u_char *)&blksfree[start],
2159 fragtbl[fs->fs_frag],
2160 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2164 loc = scanc((u_int)len, (u_char *)&blksfree[0],
2165 fragtbl[fs->fs_frag],
2166 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2168 printf("start = %d, len = %d, fs = %s\n",
2169 start, len, fs->fs_fsmnt);
2170 panic("ffs_alloccg: map corrupted");
2174 bno = (start + len - loc) * NBBY;
2175 cgp->cg_frotor = bno;
2177 * found the byte in the map
2178 * sift through the bits to find the selected frag
2180 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
2181 blk = blkmap(fs, blksfree, bno);
2183 field = around[allocsiz];
2184 subfield = inside[allocsiz];
2185 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
2186 if ((blk & field) == subfield)
2192 printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt);
2193 panic("ffs_alloccg: block not in map");
2198 * Update the cluster map because of an allocation or free.
2200 * Cnt == 1 means free; cnt == -1 means allocating.
2203 ffs_clusteracct(ump, fs, cgp, blkno, cnt)
2204 struct ufsmount *ump;
2212 u_char *freemapp, *mapp;
2213 int i, start, end, forw, back, map, bit;
2215 mtx_assert(UFS_MTX(ump), MA_OWNED);
2217 if (fs->fs_contigsumsize <= 0)
2219 freemapp = cg_clustersfree(cgp);
2220 sump = cg_clustersum(cgp);
2222 * Allocate or clear the actual block.
2225 setbit(freemapp, blkno);
2227 clrbit(freemapp, blkno);
2229 * Find the size of the cluster going forward.
2232 end = start + fs->fs_contigsumsize;
2233 if (end >= cgp->cg_nclusterblks)
2234 end = cgp->cg_nclusterblks;
2235 mapp = &freemapp[start / NBBY];
2237 bit = 1 << (start % NBBY);
2238 for (i = start; i < end; i++) {
2239 if ((map & bit) == 0)
2241 if ((i & (NBBY - 1)) != (NBBY - 1)) {
2250 * Find the size of the cluster going backward.
2253 end = start - fs->fs_contigsumsize;
2256 mapp = &freemapp[start / NBBY];
2258 bit = 1 << (start % NBBY);
2259 for (i = start; i > end; i--) {
2260 if ((map & bit) == 0)
2262 if ((i & (NBBY - 1)) != 0) {
2266 bit = 1 << (NBBY - 1);
2271 * Account for old cluster and the possibly new forward and
2274 i = back + forw + 1;
2275 if (i > fs->fs_contigsumsize)
2276 i = fs->fs_contigsumsize;
2283 * Update cluster summary information.
2285 lp = &sump[fs->fs_contigsumsize];
2286 for (i = fs->fs_contigsumsize; i > 0; i--)
2289 fs->fs_maxcluster[cgp->cg_cgx] = i;
2293 * Fserr prints the name of a filesystem with an error diagnostic.
2295 * The form of the error message is:
2299 ffs_fserr(fs, inum, cp)
2304 struct thread *td = curthread; /* XXX */
2305 struct proc *p = td->td_proc;
2307 log(LOG_ERR, "pid %d (%s), uid %d inumber %d on %s: %s\n",
2308 p->p_pid, p->p_comm, td->td_ucred->cr_uid, inum, fs->fs_fsmnt, cp);
2312 * This function provides the capability for the fsck program to
2313 * update an active filesystem. Eleven operations are provided:
2315 * adjrefcnt(inode, amt) - adjusts the reference count on the
2316 * specified inode by the specified amount. Under normal
2317 * operation the count should always go down. Decrementing
2318 * the count to zero will cause the inode to be freed.
2319 * adjblkcnt(inode, amt) - adjust the number of blocks used to
2320 * by the specifed amount.
2321 * adjndir, adjbfree, adjifree, adjffree, adjnumclusters(amt) -
2322 * adjust the superblock summary.
2323 * freedirs(inode, count) - directory inodes [inode..inode + count - 1]
2324 * are marked as free. Inodes should never have to be marked
2326 * freefiles(inode, count) - file inodes [inode..inode + count - 1]
2327 * are marked as free. Inodes should never have to be marked
2329 * freeblks(blockno, size) - blocks [blockno..blockno + size - 1]
2330 * are marked as free. Blocks should never have to be marked
2332 * setflags(flags, set/clear) - the fs_flags field has the specified
2333 * flags set (second parameter +1) or cleared (second parameter -1).
2336 static int sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS);
2338 SYSCTL_PROC(_vfs_ffs, FFS_ADJ_REFCNT, adjrefcnt, CTLFLAG_WR|CTLTYPE_STRUCT,
2339 0, 0, sysctl_ffs_fsck, "S,fsck", "Adjust Inode Reference Count");
2341 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_BLKCNT, adjblkcnt, CTLFLAG_WR,
2342 sysctl_ffs_fsck, "Adjust Inode Used Blocks Count");
2344 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NDIR, adjndir, CTLFLAG_WR,
2345 sysctl_ffs_fsck, "Adjust number of directories");
2347 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NBFREE, adjnbfree, CTLFLAG_WR,
2348 sysctl_ffs_fsck, "Adjust number of free blocks");
2350 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NIFREE, adjnifree, CTLFLAG_WR,
2351 sysctl_ffs_fsck, "Adjust number of free inodes");
2353 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NFFREE, adjnffree, CTLFLAG_WR,
2354 sysctl_ffs_fsck, "Adjust number of free frags");
2356 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NUMCLUSTERS, adjnumclusters, CTLFLAG_WR,
2357 sysctl_ffs_fsck, "Adjust number of free clusters");
2359 static SYSCTL_NODE(_vfs_ffs, FFS_DIR_FREE, freedirs, CTLFLAG_WR,
2360 sysctl_ffs_fsck, "Free Range of Directory Inodes");
2362 static SYSCTL_NODE(_vfs_ffs, FFS_FILE_FREE, freefiles, CTLFLAG_WR,
2363 sysctl_ffs_fsck, "Free Range of File Inodes");
2365 static SYSCTL_NODE(_vfs_ffs, FFS_BLK_FREE, freeblks, CTLFLAG_WR,
2366 sysctl_ffs_fsck, "Free Range of Blocks");
2368 static SYSCTL_NODE(_vfs_ffs, FFS_SET_FLAGS, setflags, CTLFLAG_WR,
2369 sysctl_ffs_fsck, "Change Filesystem Flags");
2372 static int fsckcmds = 0;
2373 SYSCTL_INT(_debug, OID_AUTO, fsckcmds, CTLFLAG_RW, &fsckcmds, 0, "");
2377 sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS)
2379 struct fsck_cmd cmd;
2380 struct ufsmount *ump;
2386 long blkcnt, blksize;
2388 int filetype, error;
2390 if (req->newlen > sizeof cmd)
2392 if ((error = SYSCTL_IN(req, &cmd, sizeof cmd)) != 0)
2394 if (cmd.version != FFS_CMD_VERSION)
2395 return (ERPCMISMATCH);
2396 if ((error = getvnode(curproc->p_fd, cmd.handle, &fp)) != 0)
2398 vn_start_write(fp->f_data, &mp, V_WAIT);
2399 if (mp == 0 || strncmp(mp->mnt_stat.f_fstypename, "ufs", MFSNAMELEN)) {
2400 vn_finished_write(mp);
2401 fdrop(fp, curthread);
2404 if (mp->mnt_flag & MNT_RDONLY) {
2405 vn_finished_write(mp);
2406 fdrop(fp, curthread);
2413 switch (oidp->oid_number) {
2418 printf("%s: %s flags\n", mp->mnt_stat.f_mntonname,
2419 cmd.size > 0 ? "set" : "clear");
2422 fs->fs_flags |= (long)cmd.value;
2424 fs->fs_flags &= ~(long)cmd.value;
2427 case FFS_ADJ_REFCNT:
2430 printf("%s: adjust inode %jd count by %jd\n",
2431 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2432 (intmax_t)cmd.size);
2435 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2438 ip->i_nlink += cmd.size;
2439 DIP_SET(ip, i_nlink, ip->i_nlink);
2440 ip->i_effnlink += cmd.size;
2441 ip->i_flag |= IN_CHANGE;
2442 if (DOINGSOFTDEP(vp))
2443 softdep_change_linkcnt(ip);
2447 case FFS_ADJ_BLKCNT:
2450 printf("%s: adjust inode %jd block count by %jd\n",
2451 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2452 (intmax_t)cmd.size);
2455 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2458 if (ip->i_flag & IN_SPACECOUNTED) {
2460 fs->fs_pendingblocks += cmd.size;
2463 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + cmd.size);
2464 ip->i_flag |= IN_CHANGE;
2476 printf("%s: free %s inode %d\n",
2477 mp->mnt_stat.f_mntonname,
2478 filetype == IFDIR ? "directory" : "file",
2481 printf("%s: free %s inodes %d-%d\n",
2482 mp->mnt_stat.f_mntonname,
2483 filetype == IFDIR ? "directory" : "file",
2485 (ino_t)(cmd.value + cmd.size - 1));
2488 while (cmd.size > 0) {
2489 if ((error = ffs_freefile(ump, fs, ump->um_devvp,
2490 cmd.value, filetype)))
2501 printf("%s: free block %jd\n",
2502 mp->mnt_stat.f_mntonname,
2503 (intmax_t)cmd.value);
2505 printf("%s: free blocks %jd-%jd\n",
2506 mp->mnt_stat.f_mntonname,
2507 (intmax_t)cmd.value,
2508 (intmax_t)cmd.value + cmd.size - 1);
2513 blksize = fs->fs_frag - (blkno % fs->fs_frag);
2514 while (blkcnt > 0) {
2515 if (blksize > blkcnt)
2517 ffs_blkfree(ump, fs, ump->um_devvp, blkno,
2518 blksize * fs->fs_fsize, ROOTINO);
2521 blksize = fs->fs_frag;
2526 * Adjust superblock summaries. fsck(8) is expected to
2527 * submit deltas when necessary.
2532 printf("%s: adjust number of directories by %jd\n",
2533 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2536 fs->fs_cstotal.cs_ndir += cmd.value;
2538 case FFS_ADJ_NBFREE:
2541 printf("%s: adjust number of free blocks by %+jd\n",
2542 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2545 fs->fs_cstotal.cs_nbfree += cmd.value;
2547 case FFS_ADJ_NIFREE:
2550 printf("%s: adjust number of free inodes by %+jd\n",
2551 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2554 fs->fs_cstotal.cs_nifree += cmd.value;
2556 case FFS_ADJ_NFFREE:
2559 printf("%s: adjust number of free frags by %+jd\n",
2560 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2563 fs->fs_cstotal.cs_nffree += cmd.value;
2565 case FFS_ADJ_NUMCLUSTERS:
2568 printf("%s: adjust number of free clusters by %+jd\n",
2569 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2572 fs->fs_cstotal.cs_numclusters += cmd.value;
2578 printf("Invalid request %d from fsck\n",
2586 fdrop(fp, curthread);
2587 vn_finished_write(mp);