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>
76 #include <sys/vnode.h>
77 #include <sys/mount.h>
78 #include <sys/kernel.h>
79 #include <sys/sysctl.h>
80 #include <sys/syslog.h>
82 #include <ufs/ufs/extattr.h>
83 #include <ufs/ufs/quota.h>
84 #include <ufs/ufs/inode.h>
85 #include <ufs/ufs/ufs_extern.h>
86 #include <ufs/ufs/ufsmount.h>
88 #include <ufs/ffs/fs.h>
89 #include <ufs/ffs/ffs_extern.h>
91 typedef ufs2_daddr_t allocfcn_t(struct inode *ip, int cg, ufs2_daddr_t bpref,
94 static ufs2_daddr_t ffs_alloccg(struct inode *, int, ufs2_daddr_t, int);
96 ffs_alloccgblk(struct inode *, struct buf *, ufs2_daddr_t);
98 static int ffs_checkblk(struct inode *, ufs2_daddr_t, long);
100 static ufs2_daddr_t ffs_clusteralloc(struct inode *, int, ufs2_daddr_t, int);
101 static void ffs_clusteracct(struct ufsmount *, struct fs *, struct cg *,
103 static ino_t ffs_dirpref(struct inode *);
104 static ufs2_daddr_t ffs_fragextend(struct inode *, int, ufs2_daddr_t, int, int);
105 static void ffs_fserr(struct fs *, ino_t, char *);
106 static ufs2_daddr_t ffs_hashalloc
107 (struct inode *, int, ufs2_daddr_t, int, allocfcn_t *);
108 static ufs2_daddr_t ffs_nodealloccg(struct inode *, int, ufs2_daddr_t, int);
109 static ufs1_daddr_t ffs_mapsearch(struct fs *, struct cg *, ufs2_daddr_t, int);
110 static int ffs_reallocblks_ufs1(struct vop_reallocblks_args *);
111 static int ffs_reallocblks_ufs2(struct vop_reallocblks_args *);
114 * Allocate a block in the filesystem.
116 * The size of the requested block is given, which must be some
117 * multiple of fs_fsize and <= fs_bsize.
118 * A preference may be optionally specified. If a preference is given
119 * the following hierarchy is used to allocate a block:
120 * 1) allocate the requested block.
121 * 2) allocate a rotationally optimal block in the same cylinder.
122 * 3) allocate a block in the same cylinder group.
123 * 4) quadradically rehash into other cylinder groups, until an
124 * available block is located.
125 * If no block preference is given the following hierarchy is used
126 * to allocate a block:
127 * 1) allocate a block in the cylinder group that contains the
128 * inode for the file.
129 * 2) quadradically rehash into other cylinder groups, until an
130 * available block is located.
133 ffs_alloc(ip, lbn, bpref, size, cred, bnp)
135 ufs2_daddr_t lbn, bpref;
141 struct ufsmount *ump;
144 static struct timeval lastfail;
154 mtx_assert(UFS_MTX(ump), MA_OWNED);
156 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
157 printf("dev = %s, bsize = %ld, size = %d, fs = %s\n",
158 devtoname(ip->i_dev), (long)fs->fs_bsize, size,
160 panic("ffs_alloc: bad size");
163 panic("ffs_alloc: missing credential");
164 #endif /* INVARIANTS */
169 error = chkdq(ip, btodb(size), cred, 0);
174 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
176 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0) &&
177 freespace(fs, fs->fs_minfree) - numfrags(fs, size) < 0)
179 if (bpref >= fs->fs_size)
182 cg = ino_to_cg(fs, ip->i_number);
184 cg = dtog(fs, bpref);
185 bno = ffs_hashalloc(ip, cg, bpref, size, ffs_alloccg);
188 if (ip->i_flag & IN_SPACECOUNTED) {
190 fs->fs_pendingblocks += delta;
193 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
194 ip->i_flag |= IN_CHANGE | IN_UPDATE;
202 * Restore user's disk quota because allocation failed.
204 (void) chkdq(ip, -btodb(size), cred, FORCE);
207 if (fs->fs_pendingblocks > 0 && reclaimed == 0) {
209 softdep_request_cleanup(fs, ITOV(ip));
213 if (ppsratecheck(&lastfail, &curfail, 1)) {
214 ffs_fserr(fs, ip->i_number, "filesystem full");
215 uprintf("\n%s: write failed, filesystem is full\n",
222 * Reallocate a fragment to a bigger size
224 * The number and size of the old block is given, and a preference
225 * and new size is also specified. The allocator attempts to extend
226 * the original block. Failing that, the regular block allocator is
227 * invoked to get an appropriate block.
230 ffs_realloccg(ip, lbprev, bprev, bpref, osize, nsize, cred, bpp)
242 struct ufsmount *ump;
243 int cg, request, error, reclaimed;
245 static struct timeval lastfail;
254 mtx_assert(UFS_MTX(ump), MA_OWNED);
256 if (vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
257 panic("ffs_realloccg: allocation on suspended filesystem");
258 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
259 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
261 "dev = %s, bsize = %ld, osize = %d, nsize = %d, fs = %s\n",
262 devtoname(ip->i_dev), (long)fs->fs_bsize, osize,
263 nsize, fs->fs_fsmnt);
264 panic("ffs_realloccg: bad size");
267 panic("ffs_realloccg: missing credential");
268 #endif /* INVARIANTS */
271 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0) &&
272 freespace(fs, fs->fs_minfree) - numfrags(fs, nsize - osize) < 0) {
276 printf("dev = %s, bsize = %ld, bprev = %jd, fs = %s\n",
277 devtoname(ip->i_dev), (long)fs->fs_bsize, (intmax_t)bprev,
279 panic("ffs_realloccg: bad bprev");
283 * Allocate the extra space in the buffer.
285 error = bread(vp, lbprev, osize, NOCRED, &bp);
291 if (bp->b_blkno == bp->b_lblkno) {
292 if (lbprev >= NDADDR)
293 panic("ffs_realloccg: lbprev out of range");
294 bp->b_blkno = fsbtodb(fs, bprev);
298 error = chkdq(ip, btodb(nsize - osize), cred, 0);
305 * Check for extension in the existing location.
307 cg = dtog(fs, bprev);
309 bno = ffs_fragextend(ip, cg, bprev, osize, nsize);
311 if (bp->b_blkno != fsbtodb(fs, bno))
312 panic("ffs_realloccg: bad blockno");
313 delta = btodb(nsize - osize);
314 if (ip->i_flag & IN_SPACECOUNTED) {
316 fs->fs_pendingblocks += delta;
319 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
320 ip->i_flag |= IN_CHANGE | IN_UPDATE;
322 bp->b_flags |= B_DONE;
323 if ((bp->b_flags & (B_MALLOC | B_VMIO)) != B_VMIO)
324 bzero((char *)bp->b_data + osize, nsize - osize);
331 * Allocate a new disk location.
333 if (bpref >= fs->fs_size)
335 switch ((int)fs->fs_optim) {
338 * Allocate an exact sized fragment. Although this makes
339 * best use of space, we will waste time relocating it if
340 * the file continues to grow. If the fragmentation is
341 * less than half of the minimum free reserve, we choose
342 * to begin optimizing for time.
345 if (fs->fs_minfree <= 5 ||
346 fs->fs_cstotal.cs_nffree >
347 (off_t)fs->fs_dsize * fs->fs_minfree / (2 * 100))
349 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
351 fs->fs_optim = FS_OPTTIME;
355 * At this point we have discovered a file that is trying to
356 * grow a small fragment to a larger fragment. To save time,
357 * we allocate a full sized block, then free the unused portion.
358 * If the file continues to grow, the `ffs_fragextend' call
359 * above will be able to grow it in place without further
360 * copying. If aberrant programs cause disk fragmentation to
361 * grow within 2% of the free reserve, we choose to begin
362 * optimizing for space.
364 request = fs->fs_bsize;
365 if (fs->fs_cstotal.cs_nffree <
366 (off_t)fs->fs_dsize * (fs->fs_minfree - 2) / 100)
368 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
370 fs->fs_optim = FS_OPTSPACE;
373 printf("dev = %s, optim = %ld, fs = %s\n",
374 devtoname(ip->i_dev), (long)fs->fs_optim, fs->fs_fsmnt);
375 panic("ffs_realloccg: bad optim");
378 bno = ffs_hashalloc(ip, cg, bpref, request, ffs_alloccg);
380 bp->b_blkno = fsbtodb(fs, bno);
381 if (!DOINGSOFTDEP(vp))
382 ffs_blkfree(ump, fs, ip->i_devvp, bprev, (long)osize,
385 ffs_blkfree(ump, fs, ip->i_devvp,
386 bno + numfrags(fs, nsize),
387 (long)(request - nsize), ip->i_number);
388 delta = btodb(nsize - osize);
389 if (ip->i_flag & IN_SPACECOUNTED) {
391 fs->fs_pendingblocks += delta;
394 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
395 ip->i_flag |= IN_CHANGE | IN_UPDATE;
397 bp->b_flags |= B_DONE;
398 if ((bp->b_flags & (B_MALLOC | B_VMIO)) != B_VMIO)
399 bzero((char *)bp->b_data + osize, nsize - osize);
408 * Restore user's disk quota because allocation failed.
410 (void) chkdq(ip, -btodb(nsize - osize), cred, FORCE);
417 if (fs->fs_pendingblocks > 0 && reclaimed == 0) {
419 softdep_request_cleanup(fs, vp);
429 if (ppsratecheck(&lastfail, &curfail, 1)) {
430 ffs_fserr(fs, ip->i_number, "filesystem full");
431 uprintf("\n%s: write failed, filesystem is full\n",
438 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
440 * The vnode and an array of buffer pointers for a range of sequential
441 * logical blocks to be made contiguous is given. The allocator attempts
442 * to find a range of sequential blocks starting as close as possible
443 * from the end of the allocation for the logical block immediately
444 * preceding the current range. If successful, the physical block numbers
445 * in the buffer pointers and in the inode are changed to reflect the new
446 * allocation. If unsuccessful, the allocation is left unchanged. The
447 * success in doing the reallocation is returned. Note that the error
448 * return is not reflected back to the user. Rather the previous block
449 * allocation will be used.
452 SYSCTL_NODE(_vfs, OID_AUTO, ffs, CTLFLAG_RW, 0, "FFS filesystem");
454 static int doasyncfree = 1;
455 SYSCTL_INT(_vfs_ffs, OID_AUTO, doasyncfree, CTLFLAG_RW, &doasyncfree, 0, "");
457 static int doreallocblks = 1;
458 SYSCTL_INT(_vfs_ffs, OID_AUTO, doreallocblks, CTLFLAG_RW, &doreallocblks, 0, "");
461 static volatile int prtrealloc = 0;
466 struct vop_reallocblks_args /* {
468 struct cluster_save *a_buflist;
472 if (doreallocblks == 0)
474 if (VTOI(ap->a_vp)->i_ump->um_fstype == UFS1)
475 return (ffs_reallocblks_ufs1(ap));
476 return (ffs_reallocblks_ufs2(ap));
480 ffs_reallocblks_ufs1(ap)
481 struct vop_reallocblks_args /* {
483 struct cluster_save *a_buflist;
489 struct buf *sbp, *ebp;
490 ufs1_daddr_t *bap, *sbap, *ebap = 0;
491 struct cluster_save *buflist;
492 struct ufsmount *ump;
493 ufs_lbn_t start_lbn, end_lbn;
494 ufs1_daddr_t soff, newblk, blkno;
496 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
497 int i, len, start_lvl, end_lvl, ssize;
503 if (fs->fs_contigsumsize <= 0)
505 buflist = ap->a_buflist;
506 len = buflist->bs_nchildren;
507 start_lbn = buflist->bs_children[0]->b_lblkno;
508 end_lbn = start_lbn + len - 1;
510 for (i = 0; i < len; i++)
511 if (!ffs_checkblk(ip,
512 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
513 panic("ffs_reallocblks: unallocated block 1");
514 for (i = 1; i < len; i++)
515 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
516 panic("ffs_reallocblks: non-logical cluster");
517 blkno = buflist->bs_children[0]->b_blkno;
518 ssize = fsbtodb(fs, fs->fs_frag);
519 for (i = 1; i < len - 1; i++)
520 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
521 panic("ffs_reallocblks: non-physical cluster %d", i);
524 * If the latest allocation is in a new cylinder group, assume that
525 * the filesystem has decided to move and do not force it back to
526 * the previous cylinder group.
528 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
529 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
531 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
532 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
535 * Get the starting offset and block map for the first block.
537 if (start_lvl == 0) {
538 sbap = &ip->i_din1->di_db[0];
541 idp = &start_ap[start_lvl - 1];
542 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
546 sbap = (ufs1_daddr_t *)sbp->b_data;
550 * If the block range spans two block maps, get the second map.
552 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
557 start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
558 panic("ffs_reallocblk: start == end");
560 ssize = len - (idp->in_off + 1);
561 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
563 ebap = (ufs1_daddr_t *)ebp->b_data;
566 * Find the preferred location for the cluster.
569 pref = ffs_blkpref_ufs1(ip, start_lbn, soff, sbap);
571 * Search the block map looking for an allocation of the desired size.
573 if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref,
574 len, ffs_clusteralloc)) == 0) {
579 * We have found a new contiguous block.
581 * First we have to replace the old block pointers with the new
582 * block pointers in the inode and indirect blocks associated
587 printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
588 (intmax_t)start_lbn, (intmax_t)end_lbn);
591 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
597 if (!ffs_checkblk(ip,
598 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
599 panic("ffs_reallocblks: unallocated block 2");
600 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
601 panic("ffs_reallocblks: alloc mismatch");
605 printf(" %d,", *bap);
607 if (DOINGSOFTDEP(vp)) {
608 if (sbap == &ip->i_din1->di_db[0] && i < ssize)
609 softdep_setup_allocdirect(ip, start_lbn + i,
610 blkno, *bap, fs->fs_bsize, fs->fs_bsize,
611 buflist->bs_children[i]);
613 softdep_setup_allocindir_page(ip, start_lbn + i,
614 i < ssize ? sbp : ebp, soff + i, blkno,
615 *bap, buflist->bs_children[i]);
620 * Next we must write out the modified inode and indirect blocks.
621 * For strict correctness, the writes should be synchronous since
622 * the old block values may have been written to disk. In practise
623 * they are almost never written, but if we are concerned about
624 * strict correctness, the `doasyncfree' flag should be set to zero.
626 * The test on `doasyncfree' should be changed to test a flag
627 * that shows whether the associated buffers and inodes have
628 * been written. The flag should be set when the cluster is
629 * started and cleared whenever the buffer or inode is flushed.
630 * We can then check below to see if it is set, and do the
631 * synchronous write only when it has been cleared.
633 if (sbap != &ip->i_din1->di_db[0]) {
639 ip->i_flag |= IN_CHANGE | IN_UPDATE;
650 * Last, free the old blocks and assign the new blocks to the buffers.
656 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
657 if (!DOINGSOFTDEP(vp))
658 ffs_blkfree(ump, fs, ip->i_devvp,
659 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
660 fs->fs_bsize, ip->i_number);
661 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
663 if (!ffs_checkblk(ip,
664 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
665 panic("ffs_reallocblks: unallocated block 3");
669 printf(" %d,", blkno);
683 if (sbap != &ip->i_din1->di_db[0])
689 ffs_reallocblks_ufs2(ap)
690 struct vop_reallocblks_args /* {
692 struct cluster_save *a_buflist;
698 struct buf *sbp, *ebp;
699 ufs2_daddr_t *bap, *sbap, *ebap = 0;
700 struct cluster_save *buflist;
701 struct ufsmount *ump;
702 ufs_lbn_t start_lbn, end_lbn;
703 ufs2_daddr_t soff, newblk, blkno, pref;
704 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
705 int i, len, start_lvl, end_lvl, ssize;
711 if (fs->fs_contigsumsize <= 0)
713 buflist = ap->a_buflist;
714 len = buflist->bs_nchildren;
715 start_lbn = buflist->bs_children[0]->b_lblkno;
716 end_lbn = start_lbn + len - 1;
718 for (i = 0; i < len; i++)
719 if (!ffs_checkblk(ip,
720 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
721 panic("ffs_reallocblks: unallocated block 1");
722 for (i = 1; i < len; i++)
723 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
724 panic("ffs_reallocblks: non-logical cluster");
725 blkno = buflist->bs_children[0]->b_blkno;
726 ssize = fsbtodb(fs, fs->fs_frag);
727 for (i = 1; i < len - 1; i++)
728 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
729 panic("ffs_reallocblks: non-physical cluster %d", i);
732 * If the latest allocation is in a new cylinder group, assume that
733 * the filesystem has decided to move and do not force it back to
734 * the previous cylinder group.
736 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
737 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
739 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
740 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
743 * Get the starting offset and block map for the first block.
745 if (start_lvl == 0) {
746 sbap = &ip->i_din2->di_db[0];
749 idp = &start_ap[start_lvl - 1];
750 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
754 sbap = (ufs2_daddr_t *)sbp->b_data;
758 * If the block range spans two block maps, get the second map.
760 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
765 start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
766 panic("ffs_reallocblk: start == end");
768 ssize = len - (idp->in_off + 1);
769 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
771 ebap = (ufs2_daddr_t *)ebp->b_data;
774 * Find the preferred location for the cluster.
777 pref = ffs_blkpref_ufs2(ip, start_lbn, soff, sbap);
779 * Search the block map looking for an allocation of the desired size.
781 if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref,
782 len, ffs_clusteralloc)) == 0) {
787 * We have found a new contiguous block.
789 * First we have to replace the old block pointers with the new
790 * block pointers in the inode and indirect blocks associated
795 printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
796 (intmax_t)start_lbn, (intmax_t)end_lbn);
799 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
805 if (!ffs_checkblk(ip,
806 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
807 panic("ffs_reallocblks: unallocated block 2");
808 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
809 panic("ffs_reallocblks: alloc mismatch");
813 printf(" %jd,", (intmax_t)*bap);
815 if (DOINGSOFTDEP(vp)) {
816 if (sbap == &ip->i_din2->di_db[0] && i < ssize)
817 softdep_setup_allocdirect(ip, start_lbn + i,
818 blkno, *bap, fs->fs_bsize, fs->fs_bsize,
819 buflist->bs_children[i]);
821 softdep_setup_allocindir_page(ip, start_lbn + i,
822 i < ssize ? sbp : ebp, soff + i, blkno,
823 *bap, buflist->bs_children[i]);
828 * Next we must write out the modified inode and indirect blocks.
829 * For strict correctness, the writes should be synchronous since
830 * the old block values may have been written to disk. In practise
831 * they are almost never written, but if we are concerned about
832 * strict correctness, the `doasyncfree' flag should be set to zero.
834 * The test on `doasyncfree' should be changed to test a flag
835 * that shows whether the associated buffers and inodes have
836 * been written. The flag should be set when the cluster is
837 * started and cleared whenever the buffer or inode is flushed.
838 * We can then check below to see if it is set, and do the
839 * synchronous write only when it has been cleared.
841 if (sbap != &ip->i_din2->di_db[0]) {
847 ip->i_flag |= IN_CHANGE | IN_UPDATE;
858 * Last, free the old blocks and assign the new blocks to the buffers.
864 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
865 if (!DOINGSOFTDEP(vp))
866 ffs_blkfree(ump, fs, ip->i_devvp,
867 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
868 fs->fs_bsize, ip->i_number);
869 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
871 if (!ffs_checkblk(ip,
872 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
873 panic("ffs_reallocblks: unallocated block 3");
877 printf(" %jd,", (intmax_t)blkno);
891 if (sbap != &ip->i_din2->di_db[0])
897 * Allocate an inode in the filesystem.
899 * If allocating a directory, use ffs_dirpref to select the inode.
900 * If allocating in a directory, the following hierarchy is followed:
901 * 1) allocate the preferred inode.
902 * 2) allocate an inode in the same cylinder group.
903 * 3) quadradically rehash into other cylinder groups, until an
904 * available inode is located.
905 * If no inode preference is given the following hierarchy is used
906 * to allocate an inode:
907 * 1) allocate an inode in cylinder group 0.
908 * 2) quadradically rehash into other cylinder groups, until an
909 * available inode is located.
912 ffs_valloc(pvp, mode, cred, vpp)
922 struct ufsmount *ump;
925 static struct timeval lastfail;
934 if (fs->fs_cstotal.cs_nifree == 0)
937 if ((mode & IFMT) == IFDIR)
938 ipref = ffs_dirpref(pip);
940 ipref = pip->i_number;
941 if (ipref >= fs->fs_ncg * fs->fs_ipg)
943 cg = ino_to_cg(fs, ipref);
945 * Track number of dirs created one after another
946 * in a same cg without intervening by files.
948 if ((mode & IFMT) == IFDIR) {
949 if (fs->fs_contigdirs[cg] < 255)
950 fs->fs_contigdirs[cg]++;
952 if (fs->fs_contigdirs[cg] > 0)
953 fs->fs_contigdirs[cg]--;
955 ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode,
956 (allocfcn_t *)ffs_nodealloccg);
959 error = ffs_vget(pvp->v_mount, ino, LK_EXCLUSIVE, vpp);
961 ffs_vfree(pvp, ino, mode);
966 printf("mode = 0%o, inum = %lu, fs = %s\n",
967 ip->i_mode, (u_long)ip->i_number, fs->fs_fsmnt);
968 panic("ffs_valloc: dup alloc");
970 if (DIP(ip, i_blocks) && (fs->fs_flags & FS_UNCLEAN) == 0) { /* XXX */
971 printf("free inode %s/%lu had %ld blocks\n",
972 fs->fs_fsmnt, (u_long)ino, (long)DIP(ip, i_blocks));
973 DIP_SET(ip, i_blocks, 0);
976 DIP_SET(ip, i_flags, 0);
978 * Set up a new generation number for this inode.
980 if (ip->i_gen == 0 || ++ip->i_gen == 0)
981 ip->i_gen = arc4random() / 2 + 1;
982 DIP_SET(ip, i_gen, ip->i_gen);
983 if (fs->fs_magic == FS_UFS2_MAGIC) {
985 ip->i_din2->di_birthtime = ts.tv_sec;
986 ip->i_din2->di_birthnsec = ts.tv_nsec;
989 vnode_destroy_vobject(*vpp);
990 (*vpp)->v_type = VNON;
991 if (fs->fs_magic == FS_UFS2_MAGIC)
992 (*vpp)->v_op = &ffs_vnodeops2;
994 (*vpp)->v_op = &ffs_vnodeops1;
998 if (ppsratecheck(&lastfail, &curfail, 1)) {
999 ffs_fserr(fs, pip->i_number, "out of inodes");
1000 uprintf("\n%s: create/symlink failed, no inodes free\n",
1007 * Find a cylinder group to place a directory.
1009 * The policy implemented by this algorithm is to allocate a
1010 * directory inode in the same cylinder group as its parent
1011 * directory, but also to reserve space for its files inodes
1012 * and data. Restrict the number of directories which may be
1013 * allocated one after another in the same cylinder group
1014 * without intervening allocation of files.
1016 * If we allocate a first level directory then force allocation
1017 * in another cylinder group.
1024 int cg, prefcg, dirsize, cgsize;
1025 int avgifree, avgbfree, avgndir, curdirsize;
1026 int minifree, minbfree, maxndir;
1030 mtx_assert(UFS_MTX(pip->i_ump), MA_OWNED);
1033 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
1034 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1035 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
1038 * Force allocation in another cg if creating a first level dir.
1040 ASSERT_VOP_LOCKED(ITOV(pip), "ffs_dirpref");
1041 if (ITOV(pip)->v_vflag & VV_ROOT) {
1042 prefcg = arc4random() % fs->fs_ncg;
1044 minndir = fs->fs_ipg;
1045 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1046 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1047 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1048 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1050 minndir = fs->fs_cs(fs, cg).cs_ndir;
1052 for (cg = 0; cg < prefcg; cg++)
1053 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1054 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1055 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1057 minndir = fs->fs_cs(fs, cg).cs_ndir;
1059 return ((ino_t)(fs->fs_ipg * mincg));
1063 * Count various limits which used for
1064 * optimal allocation of a directory inode.
1066 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
1067 minifree = avgifree - avgifree / 4;
1070 minbfree = avgbfree - avgbfree / 4;
1073 cgsize = fs->fs_fsize * fs->fs_fpg;
1074 dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
1075 curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
1076 if (dirsize < curdirsize)
1077 dirsize = curdirsize;
1079 maxcontigdirs = 0; /* dirsize overflowed */
1081 maxcontigdirs = min((avgbfree * fs->fs_bsize) / dirsize, 255);
1082 if (fs->fs_avgfpdir > 0)
1083 maxcontigdirs = min(maxcontigdirs,
1084 fs->fs_ipg / fs->fs_avgfpdir);
1085 if (maxcontigdirs == 0)
1089 * Limit number of dirs in one cg and reserve space for
1090 * regular files, but only if we have no deficit in
1093 prefcg = ino_to_cg(fs, pip->i_number);
1094 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1095 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1096 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1097 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1098 if (fs->fs_contigdirs[cg] < maxcontigdirs)
1099 return ((ino_t)(fs->fs_ipg * cg));
1101 for (cg = 0; cg < prefcg; cg++)
1102 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1103 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1104 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1105 if (fs->fs_contigdirs[cg] < maxcontigdirs)
1106 return ((ino_t)(fs->fs_ipg * cg));
1109 * This is a backstop when we have deficit in space.
1111 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1112 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1113 return ((ino_t)(fs->fs_ipg * cg));
1114 for (cg = 0; cg < prefcg; cg++)
1115 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1117 return ((ino_t)(fs->fs_ipg * cg));
1121 * Select the desired position for the next block in a file. The file is
1122 * logically divided into sections. The first section is composed of the
1123 * direct blocks. Each additional section contains fs_maxbpg blocks.
1125 * If no blocks have been allocated in the first section, the policy is to
1126 * request a block in the same cylinder group as the inode that describes
1127 * the file. If no blocks have been allocated in any other section, the
1128 * policy is to place the section in a cylinder group with a greater than
1129 * average number of free blocks. An appropriate cylinder group is found
1130 * by using a rotor that sweeps the cylinder groups. When a new group of
1131 * blocks is needed, the sweep begins in the cylinder group following the
1132 * cylinder group from which the previous allocation was made. The sweep
1133 * continues until a cylinder group with greater than the average number
1134 * of free blocks is found. If the allocation is for the first block in an
1135 * indirect block, the information on the previous allocation is unavailable;
1136 * here a best guess is made based upon the logical block number being
1139 * If a section is already partially allocated, the policy is to
1140 * contiguously allocate fs_maxcontig blocks. The end of one of these
1141 * contiguous blocks and the beginning of the next is laid out
1142 * contiguously if possible.
1145 ffs_blkpref_ufs1(ip, lbn, indx, bap)
1153 int avgbfree, startcg;
1155 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1157 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1158 if (lbn < NDADDR + NINDIR(fs)) {
1159 cg = ino_to_cg(fs, ip->i_number);
1160 return (cgbase(fs, cg) + fs->fs_frag);
1163 * Find a cylinder with greater than average number of
1164 * unused data blocks.
1166 if (indx == 0 || bap[indx - 1] == 0)
1168 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1170 startcg = dtog(fs, bap[indx - 1]) + 1;
1171 startcg %= fs->fs_ncg;
1172 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1173 for (cg = startcg; cg < fs->fs_ncg; cg++)
1174 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1175 fs->fs_cgrotor = cg;
1176 return (cgbase(fs, cg) + fs->fs_frag);
1178 for (cg = 0; cg <= startcg; cg++)
1179 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1180 fs->fs_cgrotor = cg;
1181 return (cgbase(fs, cg) + fs->fs_frag);
1186 * We just always try to lay things out contiguously.
1188 return (bap[indx - 1] + fs->fs_frag);
1192 * Same as above, but for UFS2
1195 ffs_blkpref_ufs2(ip, lbn, indx, bap)
1203 int avgbfree, startcg;
1205 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1207 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1208 if (lbn < NDADDR + NINDIR(fs)) {
1209 cg = ino_to_cg(fs, ip->i_number);
1210 return (cgbase(fs, cg) + fs->fs_frag);
1213 * Find a cylinder with greater than average number of
1214 * unused data blocks.
1216 if (indx == 0 || bap[indx - 1] == 0)
1218 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1220 startcg = dtog(fs, bap[indx - 1]) + 1;
1221 startcg %= fs->fs_ncg;
1222 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1223 for (cg = startcg; cg < fs->fs_ncg; cg++)
1224 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1225 fs->fs_cgrotor = cg;
1226 return (cgbase(fs, cg) + fs->fs_frag);
1228 for (cg = 0; cg <= startcg; cg++)
1229 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1230 fs->fs_cgrotor = cg;
1231 return (cgbase(fs, cg) + fs->fs_frag);
1236 * We just always try to lay things out contiguously.
1238 return (bap[indx - 1] + fs->fs_frag);
1242 * Implement the cylinder overflow algorithm.
1244 * The policy implemented by this algorithm is:
1245 * 1) allocate the block in its requested cylinder group.
1246 * 2) quadradically rehash on the cylinder group number.
1247 * 3) brute force search for a free block.
1249 * Must be called with the UFS lock held. Will release the lock on success
1250 * and return with it held on failure.
1254 ffs_hashalloc(ip, cg, pref, size, allocator)
1258 int size; /* size for data blocks, mode for inodes */
1259 allocfcn_t *allocator;
1262 ufs2_daddr_t result;
1265 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1267 if (ITOV(ip)->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
1268 panic("ffs_hashalloc: allocation on suspended filesystem");
1272 * 1: preferred cylinder group
1274 result = (*allocator)(ip, cg, pref, size);
1278 * 2: quadratic rehash
1280 for (i = 1; i < fs->fs_ncg; i *= 2) {
1282 if (cg >= fs->fs_ncg)
1284 result = (*allocator)(ip, cg, 0, size);
1289 * 3: brute force search
1290 * Note that we start at i == 2, since 0 was checked initially,
1291 * and 1 is always checked in the quadratic rehash.
1293 cg = (icg + 2) % fs->fs_ncg;
1294 for (i = 2; i < fs->fs_ncg; i++) {
1295 result = (*allocator)(ip, cg, 0, size);
1299 if (cg == fs->fs_ncg)
1306 * Determine whether a fragment can be extended.
1308 * Check to see if the necessary fragments are available, and
1309 * if they are, allocate them.
1312 ffs_fragextend(ip, cg, bprev, osize, nsize)
1321 struct ufsmount *ump;
1330 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
1332 frags = numfrags(fs, nsize);
1333 bbase = fragnum(fs, bprev);
1334 if (bbase > fragnum(fs, (bprev + frags - 1))) {
1335 /* cannot extend across a block boundary */
1339 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1340 (int)fs->fs_cgsize, NOCRED, &bp);
1343 cgp = (struct cg *)bp->b_data;
1344 if (!cg_chkmagic(cgp))
1346 bp->b_xflags |= BX_BKGRDWRITE;
1347 cgp->cg_old_time = cgp->cg_time = time_second;
1348 bno = dtogd(fs, bprev);
1349 blksfree = cg_blksfree(cgp);
1350 for (i = numfrags(fs, osize); i < frags; i++)
1351 if (isclr(blksfree, bno + i))
1354 * the current fragment can be extended
1355 * deduct the count on fragment being extended into
1356 * increase the count on the remaining fragment (if any)
1357 * allocate the extended piece
1359 for (i = frags; i < fs->fs_frag - bbase; i++)
1360 if (isclr(blksfree, bno + i))
1362 cgp->cg_frsum[i - numfrags(fs, osize)]--;
1364 cgp->cg_frsum[i - frags]++;
1365 for (i = numfrags(fs, osize), nffree = 0; i < frags; i++) {
1366 clrbit(blksfree, bno + i);
1367 cgp->cg_cs.cs_nffree--;
1371 fs->fs_cstotal.cs_nffree -= nffree;
1372 fs->fs_cs(fs, cg).cs_nffree -= nffree;
1374 ACTIVECLEAR(fs, cg);
1376 if (DOINGSOFTDEP(ITOV(ip)))
1377 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), bprev);
1389 * Determine whether a block can be allocated.
1391 * Check to see if a block of the appropriate size is available,
1392 * and if it is, allocate it.
1395 ffs_alloccg(ip, cg, bpref, size)
1404 struct ufsmount *ump;
1407 int i, allocsiz, error, frags;
1412 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1415 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1416 (int)fs->fs_cgsize, NOCRED, &bp);
1419 cgp = (struct cg *)bp->b_data;
1420 if (!cg_chkmagic(cgp) ||
1421 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize))
1423 bp->b_xflags |= BX_BKGRDWRITE;
1424 cgp->cg_old_time = cgp->cg_time = time_second;
1425 if (size == fs->fs_bsize) {
1427 blkno = ffs_alloccgblk(ip, bp, bpref);
1428 ACTIVECLEAR(fs, cg);
1434 * check to see if any fragments are already available
1435 * allocsiz is the size which will be allocated, hacking
1436 * it down to a smaller size if necessary
1438 blksfree = cg_blksfree(cgp);
1439 frags = numfrags(fs, size);
1440 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1441 if (cgp->cg_frsum[allocsiz] != 0)
1443 if (allocsiz == fs->fs_frag) {
1445 * no fragments were available, so a block will be
1446 * allocated, and hacked up
1448 if (cgp->cg_cs.cs_nbfree == 0)
1451 blkno = ffs_alloccgblk(ip, bp, bpref);
1452 bno = dtogd(fs, blkno);
1453 for (i = frags; i < fs->fs_frag; i++)
1454 setbit(blksfree, bno + i);
1455 i = fs->fs_frag - frags;
1456 cgp->cg_cs.cs_nffree += i;
1457 fs->fs_cstotal.cs_nffree += i;
1458 fs->fs_cs(fs, cg).cs_nffree += i;
1461 ACTIVECLEAR(fs, cg);
1466 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1469 for (i = 0; i < frags; i++)
1470 clrbit(blksfree, bno + i);
1471 cgp->cg_cs.cs_nffree -= frags;
1472 cgp->cg_frsum[allocsiz]--;
1473 if (frags != allocsiz)
1474 cgp->cg_frsum[allocsiz - frags]++;
1476 fs->fs_cstotal.cs_nffree -= frags;
1477 fs->fs_cs(fs, cg).cs_nffree -= frags;
1479 blkno = cgbase(fs, cg) + bno;
1480 ACTIVECLEAR(fs, cg);
1482 if (DOINGSOFTDEP(ITOV(ip)))
1483 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno);
1494 * Allocate a block in a cylinder group.
1496 * This algorithm implements the following policy:
1497 * 1) allocate the requested block.
1498 * 2) allocate a rotationally optimal block in the same cylinder.
1499 * 3) allocate the next available block on the block rotor for the
1500 * specified cylinder group.
1501 * Note that this routine only allocates fs_bsize blocks; these
1502 * blocks may be fragmented by the routine that allocates them.
1505 ffs_alloccgblk(ip, bp, bpref)
1512 struct ufsmount *ump;
1519 mtx_assert(UFS_MTX(ump), MA_OWNED);
1520 cgp = (struct cg *)bp->b_data;
1521 blksfree = cg_blksfree(cgp);
1522 if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
1523 bpref = cgp->cg_rotor;
1525 bpref = blknum(fs, bpref);
1526 bno = dtogd(fs, bpref);
1528 * if the requested block is available, use it
1530 if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
1534 * Take the next available block in this cylinder group.
1536 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1539 cgp->cg_rotor = bno;
1541 blkno = fragstoblks(fs, bno);
1542 ffs_clrblock(fs, blksfree, (long)blkno);
1543 ffs_clusteracct(ump, fs, cgp, blkno, -1);
1544 cgp->cg_cs.cs_nbfree--;
1545 fs->fs_cstotal.cs_nbfree--;
1546 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1548 blkno = cgbase(fs, cgp->cg_cgx) + bno;
1551 if (DOINGSOFTDEP(ITOV(ip)))
1552 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno);
1558 * Determine whether a cluster can be allocated.
1560 * We do not currently check for optimal rotational layout if there
1561 * are multiple choices in the same cylinder group. Instead we just
1562 * take the first one that we find following bpref.
1565 ffs_clusteralloc(ip, cg, bpref, len)
1574 struct ufsmount *ump;
1575 int i, run, bit, map, got;
1583 if (fs->fs_maxcluster[cg] < len)
1586 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1589 cgp = (struct cg *)bp->b_data;
1590 if (!cg_chkmagic(cgp))
1592 bp->b_xflags |= BX_BKGRDWRITE;
1594 * Check to see if a cluster of the needed size (or bigger) is
1595 * available in this cylinder group.
1597 lp = &cg_clustersum(cgp)[len];
1598 for (i = len; i <= fs->fs_contigsumsize; i++)
1601 if (i > fs->fs_contigsumsize) {
1603 * This is the first time looking for a cluster in this
1604 * cylinder group. Update the cluster summary information
1605 * to reflect the true maximum sized cluster so that
1606 * future cluster allocation requests can avoid reading
1607 * the cylinder group map only to find no clusters.
1609 lp = &cg_clustersum(cgp)[len - 1];
1610 for (i = len - 1; i > 0; i--)
1614 fs->fs_maxcluster[cg] = i;
1618 * Search the cluster map to find a big enough cluster.
1619 * We take the first one that we find, even if it is larger
1620 * than we need as we prefer to get one close to the previous
1621 * block allocation. We do not search before the current
1622 * preference point as we do not want to allocate a block
1623 * that is allocated before the previous one (as we will
1624 * then have to wait for another pass of the elevator
1625 * algorithm before it will be read). We prefer to fail and
1626 * be recalled to try an allocation in the next cylinder group.
1628 if (dtog(fs, bpref) != cg)
1631 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1632 mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1634 bit = 1 << (bpref % NBBY);
1635 for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
1636 if ((map & bit) == 0) {
1643 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1650 if (got >= cgp->cg_nclusterblks)
1653 * Allocate the cluster that we have found.
1655 blksfree = cg_blksfree(cgp);
1656 for (i = 1; i <= len; i++)
1657 if (!ffs_isblock(fs, blksfree, got - run + i))
1658 panic("ffs_clusteralloc: map mismatch");
1659 bno = cgbase(fs, cg) + blkstofrags(fs, got - run + 1);
1660 if (dtog(fs, bno) != cg)
1661 panic("ffs_clusteralloc: allocated out of group");
1662 len = blkstofrags(fs, len);
1664 for (i = 0; i < len; i += fs->fs_frag)
1665 if (ffs_alloccgblk(ip, bp, bno + i) != bno + i)
1666 panic("ffs_clusteralloc: lost block");
1667 ACTIVECLEAR(fs, cg);
1680 * Determine whether an inode can be allocated.
1682 * Check to see if an inode is available, and if it is,
1683 * allocate it using the following policy:
1684 * 1) allocate the requested inode.
1685 * 2) allocate the next available inode after the requested
1686 * inode in the specified cylinder group.
1689 ffs_nodealloccg(ip, cg, ipref, mode)
1697 struct buf *bp, *ibp;
1698 struct ufsmount *ump;
1700 struct ufs2_dinode *dp2;
1701 int error, start, len, loc, map, i;
1705 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1708 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1709 (int)fs->fs_cgsize, NOCRED, &bp);
1715 cgp = (struct cg *)bp->b_data;
1716 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
1721 bp->b_xflags |= BX_BKGRDWRITE;
1722 cgp->cg_old_time = cgp->cg_time = time_second;
1723 inosused = cg_inosused(cgp);
1725 ipref %= fs->fs_ipg;
1726 if (isclr(inosused, ipref))
1729 start = cgp->cg_irotor / NBBY;
1730 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
1731 loc = skpc(0xff, len, &inosused[start]);
1735 loc = skpc(0xff, len, &inosused[0]);
1737 printf("cg = %d, irotor = %ld, fs = %s\n",
1738 cg, (long)cgp->cg_irotor, fs->fs_fsmnt);
1739 panic("ffs_nodealloccg: map corrupted");
1743 i = start + len - loc;
1746 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1747 if ((map & i) == 0) {
1748 cgp->cg_irotor = ipref;
1752 printf("fs = %s\n", fs->fs_fsmnt);
1753 panic("ffs_nodealloccg: block not in map");
1757 * Check to see if we need to initialize more inodes.
1760 if (fs->fs_magic == FS_UFS2_MAGIC &&
1761 ipref + INOPB(fs) > cgp->cg_initediblk &&
1762 cgp->cg_initediblk < cgp->cg_niblk) {
1763 ibp = getblk(ip->i_devvp, fsbtodb(fs,
1764 ino_to_fsba(fs, cg * fs->fs_ipg + cgp->cg_initediblk)),
1765 (int)fs->fs_bsize, 0, 0, 0);
1766 bzero(ibp->b_data, (int)fs->fs_bsize);
1767 dp2 = (struct ufs2_dinode *)(ibp->b_data);
1768 for (i = 0; i < INOPB(fs); i++) {
1769 dp2->di_gen = arc4random() / 2 + 1;
1772 cgp->cg_initediblk += INOPB(fs);
1775 ACTIVECLEAR(fs, cg);
1776 setbit(inosused, ipref);
1777 cgp->cg_cs.cs_nifree--;
1778 fs->fs_cstotal.cs_nifree--;
1779 fs->fs_cs(fs, cg).cs_nifree--;
1781 if ((mode & IFMT) == IFDIR) {
1782 cgp->cg_cs.cs_ndir++;
1783 fs->fs_cstotal.cs_ndir++;
1784 fs->fs_cs(fs, cg).cs_ndir++;
1787 if (DOINGSOFTDEP(ITOV(ip)))
1788 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1792 return (cg * fs->fs_ipg + ipref);
1796 * check if a block is free
1799 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
1802 switch ((int)fs->fs_frag) {
1804 return (cp[h] == 0);
1806 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
1808 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
1810 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
1812 panic("ffs_isfreeblock");
1818 * Free a block or fragment.
1820 * The specified block or fragment is placed back in the
1821 * free map. If a fragment is deallocated, a possible
1822 * block reassembly is checked.
1825 ffs_blkfree(ump, fs, devvp, bno, size, inum)
1826 struct ufsmount *ump;
1828 struct vnode *devvp;
1835 ufs1_daddr_t fragno, cgbno;
1836 ufs2_daddr_t cgblkno;
1837 int i, cg, blk, frags, bbase;
1842 if (devvp->v_type != VCHR) {
1843 /* devvp is a snapshot */
1844 dev = VTOI(devvp)->i_devvp->v_rdev;
1845 cgblkno = fragstoblks(fs, cgtod(fs, cg));
1847 /* devvp is a normal disk device */
1848 dev = devvp->v_rdev;
1849 cgblkno = fsbtodb(fs, cgtod(fs, cg));
1850 ASSERT_VOP_LOCKED(devvp, "ffs_blkfree");
1851 if ((devvp->v_vflag & VV_COPYONWRITE) &&
1852 ffs_snapblkfree(fs, devvp, bno, size, inum))
1856 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1857 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1858 printf("dev=%s, bno = %jd, bsize = %ld, size = %ld, fs = %s\n",
1859 devtoname(dev), (intmax_t)bno, (long)fs->fs_bsize,
1860 size, fs->fs_fsmnt);
1861 panic("ffs_blkfree: bad size");
1864 if ((u_int)bno >= fs->fs_size) {
1865 printf("bad block %jd, ino %lu\n", (intmax_t)bno,
1867 ffs_fserr(fs, inum, "bad block");
1870 if (bread(devvp, cgblkno, (int)fs->fs_cgsize, NOCRED, &bp)) {
1874 cgp = (struct cg *)bp->b_data;
1875 if (!cg_chkmagic(cgp)) {
1879 bp->b_xflags |= BX_BKGRDWRITE;
1880 cgp->cg_old_time = cgp->cg_time = time_second;
1881 cgbno = dtogd(fs, bno);
1882 blksfree = cg_blksfree(cgp);
1884 if (size == fs->fs_bsize) {
1885 fragno = fragstoblks(fs, cgbno);
1886 if (!ffs_isfreeblock(fs, blksfree, fragno)) {
1887 if (devvp->v_type != VCHR) {
1889 /* devvp is a snapshot */
1893 printf("dev = %s, block = %jd, fs = %s\n",
1894 devtoname(dev), (intmax_t)bno, fs->fs_fsmnt);
1895 panic("ffs_blkfree: freeing free block");
1897 ffs_setblock(fs, blksfree, fragno);
1898 ffs_clusteracct(ump, fs, cgp, fragno, 1);
1899 cgp->cg_cs.cs_nbfree++;
1900 fs->fs_cstotal.cs_nbfree++;
1901 fs->fs_cs(fs, cg).cs_nbfree++;
1903 bbase = cgbno - fragnum(fs, cgbno);
1905 * decrement the counts associated with the old frags
1907 blk = blkmap(fs, blksfree, bbase);
1908 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
1910 * deallocate the fragment
1912 frags = numfrags(fs, size);
1913 for (i = 0; i < frags; i++) {
1914 if (isset(blksfree, cgbno + i)) {
1915 printf("dev = %s, block = %jd, fs = %s\n",
1916 devtoname(dev), (intmax_t)(bno + i),
1918 panic("ffs_blkfree: freeing free frag");
1920 setbit(blksfree, cgbno + i);
1922 cgp->cg_cs.cs_nffree += i;
1923 fs->fs_cstotal.cs_nffree += i;
1924 fs->fs_cs(fs, cg).cs_nffree += i;
1926 * add back in counts associated with the new frags
1928 blk = blkmap(fs, blksfree, bbase);
1929 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
1931 * if a complete block has been reassembled, account for it
1933 fragno = fragstoblks(fs, bbase);
1934 if (ffs_isblock(fs, blksfree, fragno)) {
1935 cgp->cg_cs.cs_nffree -= fs->fs_frag;
1936 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1937 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1938 ffs_clusteracct(ump, fs, cgp, fragno, 1);
1939 cgp->cg_cs.cs_nbfree++;
1940 fs->fs_cstotal.cs_nbfree++;
1941 fs->fs_cs(fs, cg).cs_nbfree++;
1945 ACTIVECLEAR(fs, cg);
1952 * Verify allocation of a block or fragment. Returns true if block or
1953 * fragment is allocated, false if it is free.
1956 ffs_checkblk(ip, bno, size)
1965 int i, error, frags, free;
1969 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1970 printf("bsize = %ld, size = %ld, fs = %s\n",
1971 (long)fs->fs_bsize, size, fs->fs_fsmnt);
1972 panic("ffs_checkblk: bad size");
1974 if ((u_int)bno >= fs->fs_size)
1975 panic("ffs_checkblk: bad block %jd", (intmax_t)bno);
1976 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
1977 (int)fs->fs_cgsize, NOCRED, &bp);
1979 panic("ffs_checkblk: cg bread failed");
1980 cgp = (struct cg *)bp->b_data;
1981 if (!cg_chkmagic(cgp))
1982 panic("ffs_checkblk: cg magic mismatch");
1983 bp->b_xflags |= BX_BKGRDWRITE;
1984 blksfree = cg_blksfree(cgp);
1985 cgbno = dtogd(fs, bno);
1986 if (size == fs->fs_bsize) {
1987 free = ffs_isblock(fs, blksfree, fragstoblks(fs, cgbno));
1989 frags = numfrags(fs, size);
1990 for (free = 0, i = 0; i < frags; i++)
1991 if (isset(blksfree, cgbno + i))
1993 if (free != 0 && free != frags)
1994 panic("ffs_checkblk: partially free fragment");
1999 #endif /* INVARIANTS */
2005 ffs_vfree(pvp, ino, mode)
2012 if (DOINGSOFTDEP(pvp)) {
2013 softdep_freefile(pvp, ino, mode);
2017 return (ffs_freefile(ip->i_ump, ip->i_fs, ip->i_devvp, ino, mode));
2021 * Do the actual free operation.
2022 * The specified inode is placed back in the free map.
2025 ffs_freefile(ump, fs, devvp, ino, mode)
2026 struct ufsmount *ump;
2028 struct vnode *devvp;
2039 cg = ino_to_cg(fs, ino);
2040 if (devvp->v_type != VCHR) {
2041 /* devvp is a snapshot */
2042 dev = VTOI(devvp)->i_devvp->v_rdev;
2043 cgbno = fragstoblks(fs, cgtod(fs, cg));
2045 /* devvp is a normal disk device */
2046 dev = devvp->v_rdev;
2047 cgbno = fsbtodb(fs, cgtod(fs, cg));
2049 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
2050 panic("ffs_freefile: range: dev = %s, ino = %lu, fs = %s",
2051 devtoname(dev), (u_long)ino, fs->fs_fsmnt);
2052 if ((error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp))) {
2056 cgp = (struct cg *)bp->b_data;
2057 if (!cg_chkmagic(cgp)) {
2061 bp->b_xflags |= BX_BKGRDWRITE;
2062 cgp->cg_old_time = cgp->cg_time = time_second;
2063 inosused = cg_inosused(cgp);
2065 if (isclr(inosused, ino)) {
2066 printf("dev = %s, ino = %lu, fs = %s\n", devtoname(dev),
2067 (u_long)ino + cg * fs->fs_ipg, fs->fs_fsmnt);
2068 if (fs->fs_ronly == 0)
2069 panic("ffs_freefile: freeing free inode");
2071 clrbit(inosused, ino);
2072 if (ino < cgp->cg_irotor)
2073 cgp->cg_irotor = ino;
2074 cgp->cg_cs.cs_nifree++;
2076 fs->fs_cstotal.cs_nifree++;
2077 fs->fs_cs(fs, cg).cs_nifree++;
2078 if ((mode & IFMT) == IFDIR) {
2079 cgp->cg_cs.cs_ndir--;
2080 fs->fs_cstotal.cs_ndir--;
2081 fs->fs_cs(fs, cg).cs_ndir--;
2084 ACTIVECLEAR(fs, cg);
2091 * Check to see if a file is free.
2094 ffs_checkfreefile(fs, devvp, ino)
2096 struct vnode *devvp;
2105 cg = ino_to_cg(fs, ino);
2106 if (devvp->v_type != VCHR) {
2107 /* devvp is a snapshot */
2108 cgbno = fragstoblks(fs, cgtod(fs, cg));
2110 /* devvp is a normal disk device */
2111 cgbno = fsbtodb(fs, cgtod(fs, cg));
2113 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
2115 if (bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp)) {
2119 cgp = (struct cg *)bp->b_data;
2120 if (!cg_chkmagic(cgp)) {
2124 inosused = cg_inosused(cgp);
2126 ret = isclr(inosused, ino);
2132 * Find a block of the specified size in the specified cylinder group.
2134 * It is a panic if a request is made to find a block if none are
2138 ffs_mapsearch(fs, cgp, bpref, allocsiz)
2145 int start, len, loc, i;
2146 int blk, field, subfield, pos;
2150 * find the fragment by searching through the free block
2151 * map for an appropriate bit pattern
2154 start = dtogd(fs, bpref) / NBBY;
2156 start = cgp->cg_frotor / NBBY;
2157 blksfree = cg_blksfree(cgp);
2158 len = howmany(fs->fs_fpg, NBBY) - start;
2159 loc = scanc((u_int)len, (u_char *)&blksfree[start],
2160 fragtbl[fs->fs_frag],
2161 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2165 loc = scanc((u_int)len, (u_char *)&blksfree[0],
2166 fragtbl[fs->fs_frag],
2167 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2169 printf("start = %d, len = %d, fs = %s\n",
2170 start, len, fs->fs_fsmnt);
2171 panic("ffs_alloccg: map corrupted");
2175 bno = (start + len - loc) * NBBY;
2176 cgp->cg_frotor = bno;
2178 * found the byte in the map
2179 * sift through the bits to find the selected frag
2181 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
2182 blk = blkmap(fs, blksfree, bno);
2184 field = around[allocsiz];
2185 subfield = inside[allocsiz];
2186 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
2187 if ((blk & field) == subfield)
2193 printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt);
2194 panic("ffs_alloccg: block not in map");
2199 * Update the cluster map because of an allocation or free.
2201 * Cnt == 1 means free; cnt == -1 means allocating.
2204 ffs_clusteracct(ump, fs, cgp, blkno, cnt)
2205 struct ufsmount *ump;
2213 u_char *freemapp, *mapp;
2214 int i, start, end, forw, back, map, bit;
2216 mtx_assert(UFS_MTX(ump), MA_OWNED);
2218 if (fs->fs_contigsumsize <= 0)
2220 freemapp = cg_clustersfree(cgp);
2221 sump = cg_clustersum(cgp);
2223 * Allocate or clear the actual block.
2226 setbit(freemapp, blkno);
2228 clrbit(freemapp, blkno);
2230 * Find the size of the cluster going forward.
2233 end = start + fs->fs_contigsumsize;
2234 if (end >= cgp->cg_nclusterblks)
2235 end = cgp->cg_nclusterblks;
2236 mapp = &freemapp[start / NBBY];
2238 bit = 1 << (start % NBBY);
2239 for (i = start; i < end; i++) {
2240 if ((map & bit) == 0)
2242 if ((i & (NBBY - 1)) != (NBBY - 1)) {
2251 * Find the size of the cluster going backward.
2254 end = start - fs->fs_contigsumsize;
2257 mapp = &freemapp[start / NBBY];
2259 bit = 1 << (start % NBBY);
2260 for (i = start; i > end; i--) {
2261 if ((map & bit) == 0)
2263 if ((i & (NBBY - 1)) != 0) {
2267 bit = 1 << (NBBY - 1);
2272 * Account for old cluster and the possibly new forward and
2275 i = back + forw + 1;
2276 if (i > fs->fs_contigsumsize)
2277 i = fs->fs_contigsumsize;
2284 * Update cluster summary information.
2286 lp = &sump[fs->fs_contigsumsize];
2287 for (i = fs->fs_contigsumsize; i > 0; i--)
2290 fs->fs_maxcluster[cgp->cg_cgx] = i;
2294 * Fserr prints the name of a filesystem with an error diagnostic.
2296 * The form of the error message is:
2300 ffs_fserr(fs, inum, cp)
2305 struct thread *td = curthread; /* XXX */
2306 struct proc *p = td->td_proc;
2308 log(LOG_ERR, "pid %d (%s), uid %d inumber %d on %s: %s\n",
2309 p->p_pid, p->p_comm, td->td_ucred->cr_uid, inum, fs->fs_fsmnt, cp);
2313 * This function provides the capability for the fsck program to
2314 * update an active filesystem. Eleven operations are provided:
2316 * adjrefcnt(inode, amt) - adjusts the reference count on the
2317 * specified inode by the specified amount. Under normal
2318 * operation the count should always go down. Decrementing
2319 * the count to zero will cause the inode to be freed.
2320 * adjblkcnt(inode, amt) - adjust the number of blocks used to
2321 * by the specifed amount.
2322 * adjndir, adjbfree, adjifree, adjffree, adjnumclusters(amt) -
2323 * adjust the superblock summary.
2324 * freedirs(inode, count) - directory inodes [inode..inode + count - 1]
2325 * are marked as free. Inodes should never have to be marked
2327 * freefiles(inode, count) - file inodes [inode..inode + count - 1]
2328 * are marked as free. Inodes should never have to be marked
2330 * freeblks(blockno, size) - blocks [blockno..blockno + size - 1]
2331 * are marked as free. Blocks should never have to be marked
2333 * setflags(flags, set/clear) - the fs_flags field has the specified
2334 * flags set (second parameter +1) or cleared (second parameter -1).
2337 static int sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS);
2339 SYSCTL_PROC(_vfs_ffs, FFS_ADJ_REFCNT, adjrefcnt, CTLFLAG_WR|CTLTYPE_STRUCT,
2340 0, 0, sysctl_ffs_fsck, "S,fsck", "Adjust Inode Reference Count");
2342 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_BLKCNT, adjblkcnt, CTLFLAG_WR,
2343 sysctl_ffs_fsck, "Adjust Inode Used Blocks Count");
2345 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NDIR, adjndir, CTLFLAG_WR,
2346 sysctl_ffs_fsck, "Adjust number of directories");
2348 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NBFREE, adjnbfree, CTLFLAG_WR,
2349 sysctl_ffs_fsck, "Adjust number of free blocks");
2351 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NIFREE, adjnifree, CTLFLAG_WR,
2352 sysctl_ffs_fsck, "Adjust number of free inodes");
2354 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NFFREE, adjnffree, CTLFLAG_WR,
2355 sysctl_ffs_fsck, "Adjust number of free frags");
2357 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NUMCLUSTERS, adjnumclusters, CTLFLAG_WR,
2358 sysctl_ffs_fsck, "Adjust number of free clusters");
2360 static SYSCTL_NODE(_vfs_ffs, FFS_DIR_FREE, freedirs, CTLFLAG_WR,
2361 sysctl_ffs_fsck, "Free Range of Directory Inodes");
2363 static SYSCTL_NODE(_vfs_ffs, FFS_FILE_FREE, freefiles, CTLFLAG_WR,
2364 sysctl_ffs_fsck, "Free Range of File Inodes");
2366 static SYSCTL_NODE(_vfs_ffs, FFS_BLK_FREE, freeblks, CTLFLAG_WR,
2367 sysctl_ffs_fsck, "Free Range of Blocks");
2369 static SYSCTL_NODE(_vfs_ffs, FFS_SET_FLAGS, setflags, CTLFLAG_WR,
2370 sysctl_ffs_fsck, "Change Filesystem Flags");
2373 static int fsckcmds = 0;
2374 SYSCTL_INT(_debug, OID_AUTO, fsckcmds, CTLFLAG_RW, &fsckcmds, 0, "");
2378 sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS)
2380 struct fsck_cmd cmd;
2381 struct ufsmount *ump;
2387 long blkcnt, blksize;
2389 int filetype, error;
2391 if (req->newlen > sizeof cmd)
2393 if ((error = SYSCTL_IN(req, &cmd, sizeof cmd)) != 0)
2395 if (cmd.version != FFS_CMD_VERSION)
2396 return (ERPCMISMATCH);
2397 if ((error = getvnode(curproc->p_fd, cmd.handle, &fp)) != 0)
2399 vn_start_write(fp->f_data, &mp, V_WAIT);
2400 if (mp == 0 || strncmp(mp->mnt_stat.f_fstypename, "ufs", MFSNAMELEN)) {
2401 vn_finished_write(mp);
2402 fdrop(fp, curthread);
2405 if (mp->mnt_flag & MNT_RDONLY) {
2406 vn_finished_write(mp);
2407 fdrop(fp, curthread);
2414 switch (oidp->oid_number) {
2419 printf("%s: %s flags\n", mp->mnt_stat.f_mntonname,
2420 cmd.size > 0 ? "set" : "clear");
2423 fs->fs_flags |= (long)cmd.value;
2425 fs->fs_flags &= ~(long)cmd.value;
2428 case FFS_ADJ_REFCNT:
2431 printf("%s: adjust inode %jd count by %jd\n",
2432 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2433 (intmax_t)cmd.size);
2436 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2439 ip->i_nlink += cmd.size;
2440 DIP_SET(ip, i_nlink, ip->i_nlink);
2441 ip->i_effnlink += cmd.size;
2442 ip->i_flag |= IN_CHANGE;
2443 if (DOINGSOFTDEP(vp))
2444 softdep_change_linkcnt(ip);
2448 case FFS_ADJ_BLKCNT:
2451 printf("%s: adjust inode %jd block count by %jd\n",
2452 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2453 (intmax_t)cmd.size);
2456 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2459 if (ip->i_flag & IN_SPACECOUNTED) {
2461 fs->fs_pendingblocks += cmd.size;
2464 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + cmd.size);
2465 ip->i_flag |= IN_CHANGE;
2477 printf("%s: free %s inode %d\n",
2478 mp->mnt_stat.f_mntonname,
2479 filetype == IFDIR ? "directory" : "file",
2482 printf("%s: free %s inodes %d-%d\n",
2483 mp->mnt_stat.f_mntonname,
2484 filetype == IFDIR ? "directory" : "file",
2486 (ino_t)(cmd.value + cmd.size - 1));
2489 while (cmd.size > 0) {
2490 if ((error = ffs_freefile(ump, fs, ump->um_devvp,
2491 cmd.value, filetype)))
2502 printf("%s: free block %jd\n",
2503 mp->mnt_stat.f_mntonname,
2504 (intmax_t)cmd.value);
2506 printf("%s: free blocks %jd-%jd\n",
2507 mp->mnt_stat.f_mntonname,
2508 (intmax_t)cmd.value,
2509 (intmax_t)cmd.value + cmd.size - 1);
2514 blksize = fs->fs_frag - (blkno % fs->fs_frag);
2515 while (blkcnt > 0) {
2516 if (blksize > blkcnt)
2518 ffs_blkfree(ump, fs, ump->um_devvp, blkno,
2519 blksize * fs->fs_fsize, ROOTINO);
2522 blksize = fs->fs_frag;
2527 * Adjust superblock summaries. fsck(8) is expected to
2528 * submit deltas when necessary.
2533 printf("%s: adjust number of directories by %jd\n",
2534 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2537 fs->fs_cstotal.cs_ndir += cmd.value;
2539 case FFS_ADJ_NBFREE:
2542 printf("%s: adjust number of free blocks by %+jd\n",
2543 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2546 fs->fs_cstotal.cs_nbfree += cmd.value;
2548 case FFS_ADJ_NIFREE:
2551 printf("%s: adjust number of free inodes by %+jd\n",
2552 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2555 fs->fs_cstotal.cs_nifree += cmd.value;
2557 case FFS_ADJ_NFFREE:
2560 printf("%s: adjust number of free frags by %+jd\n",
2561 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2564 fs->fs_cstotal.cs_nffree += cmd.value;
2566 case FFS_ADJ_NUMCLUSTERS:
2569 printf("%s: adjust number of free clusters by %+jd\n",
2570 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2573 fs->fs_cstotal.cs_numclusters += cmd.value;
2579 printf("Invalid request %d from fsck\n",
2587 fdrop(fp, curthread);
2588 vn_finished_write(mp);