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, flags, 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);
195 ip->i_flag |= IN_CHANGE;
197 ip->i_flag |= IN_CHANGE | IN_UPDATE;
205 * Restore user's disk quota because allocation failed.
207 (void) chkdq(ip, -btodb(size), cred, FORCE);
210 if (fs->fs_pendingblocks > 0 && reclaimed == 0) {
212 softdep_request_cleanup(fs, ITOV(ip));
216 if (ppsratecheck(&lastfail, &curfail, 1)) {
217 ffs_fserr(fs, ip->i_number, "filesystem full");
218 uprintf("\n%s: write failed, filesystem is full\n",
225 * Reallocate a fragment to a bigger size
227 * The number and size of the old block is given, and a preference
228 * and new size is also specified. The allocator attempts to extend
229 * the original block. Failing that, the regular block allocator is
230 * invoked to get an appropriate block.
233 ffs_realloccg(ip, lbprev, bprev, bpref, osize, nsize, flags, cred, bpp)
238 int osize, nsize, flags;
245 struct ufsmount *ump;
246 int cg, request, error, reclaimed;
248 static struct timeval lastfail;
257 mtx_assert(UFS_MTX(ump), MA_OWNED);
259 if (vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
260 panic("ffs_realloccg: allocation on suspended filesystem");
261 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
262 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
264 "dev = %s, bsize = %ld, osize = %d, nsize = %d, fs = %s\n",
265 devtoname(ip->i_dev), (long)fs->fs_bsize, osize,
266 nsize, fs->fs_fsmnt);
267 panic("ffs_realloccg: bad size");
270 panic("ffs_realloccg: missing credential");
271 #endif /* INVARIANTS */
274 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0) &&
275 freespace(fs, fs->fs_minfree) - numfrags(fs, nsize - osize) < 0) {
279 printf("dev = %s, bsize = %ld, bprev = %jd, fs = %s\n",
280 devtoname(ip->i_dev), (long)fs->fs_bsize, (intmax_t)bprev,
282 panic("ffs_realloccg: bad bprev");
286 * Allocate the extra space in the buffer.
288 error = bread(vp, lbprev, osize, NOCRED, &bp);
294 if (bp->b_blkno == bp->b_lblkno) {
295 if (lbprev >= NDADDR)
296 panic("ffs_realloccg: lbprev out of range");
297 bp->b_blkno = fsbtodb(fs, bprev);
301 error = chkdq(ip, btodb(nsize - osize), cred, 0);
308 * Check for extension in the existing location.
310 cg = dtog(fs, bprev);
312 bno = ffs_fragextend(ip, cg, bprev, osize, nsize);
314 if (bp->b_blkno != fsbtodb(fs, bno))
315 panic("ffs_realloccg: bad blockno");
316 delta = btodb(nsize - osize);
317 if (ip->i_flag & IN_SPACECOUNTED) {
319 fs->fs_pendingblocks += delta;
322 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
324 ip->i_flag |= IN_CHANGE;
326 ip->i_flag |= IN_CHANGE | IN_UPDATE;
328 bp->b_flags |= B_DONE;
329 bzero(bp->b_data + osize, nsize - osize);
330 if ((bp->b_flags & (B_MALLOC | B_VMIO)) == B_VMIO)
331 vfs_bio_set_valid(bp, osize, nsize - osize);
336 * Allocate a new disk location.
338 if (bpref >= fs->fs_size)
340 switch ((int)fs->fs_optim) {
343 * Allocate an exact sized fragment. Although this makes
344 * best use of space, we will waste time relocating it if
345 * the file continues to grow. If the fragmentation is
346 * less than half of the minimum free reserve, we choose
347 * to begin optimizing for time.
350 if (fs->fs_minfree <= 5 ||
351 fs->fs_cstotal.cs_nffree >
352 (off_t)fs->fs_dsize * fs->fs_minfree / (2 * 100))
354 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
356 fs->fs_optim = FS_OPTTIME;
360 * At this point we have discovered a file that is trying to
361 * grow a small fragment to a larger fragment. To save time,
362 * we allocate a full sized block, then free the unused portion.
363 * If the file continues to grow, the `ffs_fragextend' call
364 * above will be able to grow it in place without further
365 * copying. If aberrant programs cause disk fragmentation to
366 * grow within 2% of the free reserve, we choose to begin
367 * optimizing for space.
369 request = fs->fs_bsize;
370 if (fs->fs_cstotal.cs_nffree <
371 (off_t)fs->fs_dsize * (fs->fs_minfree - 2) / 100)
373 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
375 fs->fs_optim = FS_OPTSPACE;
378 printf("dev = %s, optim = %ld, fs = %s\n",
379 devtoname(ip->i_dev), (long)fs->fs_optim, fs->fs_fsmnt);
380 panic("ffs_realloccg: bad optim");
383 bno = ffs_hashalloc(ip, cg, bpref, request, ffs_alloccg);
385 bp->b_blkno = fsbtodb(fs, bno);
386 if (!DOINGSOFTDEP(vp))
387 ffs_blkfree(ump, fs, ip->i_devvp, bprev, (long)osize,
390 ffs_blkfree(ump, fs, ip->i_devvp,
391 bno + numfrags(fs, nsize),
392 (long)(request - nsize), ip->i_number);
393 delta = btodb(nsize - osize);
394 if (ip->i_flag & IN_SPACECOUNTED) {
396 fs->fs_pendingblocks += delta;
399 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
401 ip->i_flag |= IN_CHANGE;
403 ip->i_flag |= IN_CHANGE | IN_UPDATE;
405 bp->b_flags |= B_DONE;
406 bzero(bp->b_data + osize, nsize - osize);
407 if ((bp->b_flags & (B_MALLOC | B_VMIO)) == B_VMIO)
408 vfs_bio_set_valid(bp, osize, nsize - osize);
415 * Restore user's disk quota because allocation failed.
417 (void) chkdq(ip, -btodb(nsize - osize), cred, FORCE);
424 if (fs->fs_pendingblocks > 0 && reclaimed == 0) {
426 softdep_request_cleanup(fs, vp);
438 if (ppsratecheck(&lastfail, &curfail, 1)) {
439 ffs_fserr(fs, ip->i_number, "filesystem full");
440 uprintf("\n%s: write failed, filesystem is full\n",
447 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
449 * The vnode and an array of buffer pointers for a range of sequential
450 * logical blocks to be made contiguous is given. The allocator attempts
451 * to find a range of sequential blocks starting as close as possible
452 * from the end of the allocation for the logical block immediately
453 * preceding the current range. If successful, the physical block numbers
454 * in the buffer pointers and in the inode are changed to reflect the new
455 * allocation. If unsuccessful, the allocation is left unchanged. The
456 * success in doing the reallocation is returned. Note that the error
457 * return is not reflected back to the user. Rather the previous block
458 * allocation will be used.
461 SYSCTL_NODE(_vfs, OID_AUTO, ffs, CTLFLAG_RW, 0, "FFS filesystem");
463 static int doasyncfree = 1;
464 SYSCTL_INT(_vfs_ffs, OID_AUTO, doasyncfree, CTLFLAG_RW, &doasyncfree, 0, "");
466 static int doreallocblks = 1;
467 SYSCTL_INT(_vfs_ffs, OID_AUTO, doreallocblks, CTLFLAG_RW, &doreallocblks, 0, "");
470 static volatile int prtrealloc = 0;
475 struct vop_reallocblks_args /* {
477 struct cluster_save *a_buflist;
481 if (doreallocblks == 0)
483 if (VTOI(ap->a_vp)->i_ump->um_fstype == UFS1)
484 return (ffs_reallocblks_ufs1(ap));
485 return (ffs_reallocblks_ufs2(ap));
489 ffs_reallocblks_ufs1(ap)
490 struct vop_reallocblks_args /* {
492 struct cluster_save *a_buflist;
498 struct buf *sbp, *ebp;
499 ufs1_daddr_t *bap, *sbap, *ebap = 0;
500 struct cluster_save *buflist;
501 struct ufsmount *ump;
502 ufs_lbn_t start_lbn, end_lbn;
503 ufs1_daddr_t soff, newblk, blkno;
505 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
506 int i, len, start_lvl, end_lvl, ssize;
512 if (fs->fs_contigsumsize <= 0)
514 buflist = ap->a_buflist;
515 len = buflist->bs_nchildren;
516 start_lbn = buflist->bs_children[0]->b_lblkno;
517 end_lbn = start_lbn + len - 1;
519 for (i = 0; i < len; i++)
520 if (!ffs_checkblk(ip,
521 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
522 panic("ffs_reallocblks: unallocated block 1");
523 for (i = 1; i < len; i++)
524 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
525 panic("ffs_reallocblks: non-logical cluster");
526 blkno = buflist->bs_children[0]->b_blkno;
527 ssize = fsbtodb(fs, fs->fs_frag);
528 for (i = 1; i < len - 1; i++)
529 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
530 panic("ffs_reallocblks: non-physical cluster %d", i);
533 * If the latest allocation is in a new cylinder group, assume that
534 * the filesystem has decided to move and do not force it back to
535 * the previous cylinder group.
537 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
538 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
540 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
541 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
544 * Get the starting offset and block map for the first block.
546 if (start_lvl == 0) {
547 sbap = &ip->i_din1->di_db[0];
550 idp = &start_ap[start_lvl - 1];
551 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
555 sbap = (ufs1_daddr_t *)sbp->b_data;
559 * If the block range spans two block maps, get the second map.
561 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
566 start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
567 panic("ffs_reallocblk: start == end");
569 ssize = len - (idp->in_off + 1);
570 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
572 ebap = (ufs1_daddr_t *)ebp->b_data;
575 * Find the preferred location for the cluster.
578 pref = ffs_blkpref_ufs1(ip, start_lbn, soff, sbap);
580 * Search the block map looking for an allocation of the desired size.
582 if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref,
583 len, ffs_clusteralloc)) == 0) {
588 * We have found a new contiguous block.
590 * First we have to replace the old block pointers with the new
591 * block pointers in the inode and indirect blocks associated
596 printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
597 (intmax_t)start_lbn, (intmax_t)end_lbn);
600 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
606 if (!ffs_checkblk(ip,
607 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
608 panic("ffs_reallocblks: unallocated block 2");
609 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
610 panic("ffs_reallocblks: alloc mismatch");
614 printf(" %d,", *bap);
616 if (DOINGSOFTDEP(vp)) {
617 if (sbap == &ip->i_din1->di_db[0] && i < ssize)
618 softdep_setup_allocdirect(ip, start_lbn + i,
619 blkno, *bap, fs->fs_bsize, fs->fs_bsize,
620 buflist->bs_children[i]);
622 softdep_setup_allocindir_page(ip, start_lbn + i,
623 i < ssize ? sbp : ebp, soff + i, blkno,
624 *bap, buflist->bs_children[i]);
629 * Next we must write out the modified inode and indirect blocks.
630 * For strict correctness, the writes should be synchronous since
631 * the old block values may have been written to disk. In practise
632 * they are almost never written, but if we are concerned about
633 * strict correctness, the `doasyncfree' flag should be set to zero.
635 * The test on `doasyncfree' should be changed to test a flag
636 * that shows whether the associated buffers and inodes have
637 * been written. The flag should be set when the cluster is
638 * started and cleared whenever the buffer or inode is flushed.
639 * We can then check below to see if it is set, and do the
640 * synchronous write only when it has been cleared.
642 if (sbap != &ip->i_din1->di_db[0]) {
648 ip->i_flag |= IN_CHANGE | IN_UPDATE;
659 * Last, free the old blocks and assign the new blocks to the buffers.
665 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
666 if (!DOINGSOFTDEP(vp))
667 ffs_blkfree(ump, fs, ip->i_devvp,
668 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
669 fs->fs_bsize, ip->i_number);
670 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
672 if (!ffs_checkblk(ip,
673 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
674 panic("ffs_reallocblks: unallocated block 3");
678 printf(" %d,", blkno);
692 if (sbap != &ip->i_din1->di_db[0])
698 ffs_reallocblks_ufs2(ap)
699 struct vop_reallocblks_args /* {
701 struct cluster_save *a_buflist;
707 struct buf *sbp, *ebp;
708 ufs2_daddr_t *bap, *sbap, *ebap = 0;
709 struct cluster_save *buflist;
710 struct ufsmount *ump;
711 ufs_lbn_t start_lbn, end_lbn;
712 ufs2_daddr_t soff, newblk, blkno, pref;
713 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
714 int i, len, start_lvl, end_lvl, ssize;
720 if (fs->fs_contigsumsize <= 0)
722 buflist = ap->a_buflist;
723 len = buflist->bs_nchildren;
724 start_lbn = buflist->bs_children[0]->b_lblkno;
725 end_lbn = start_lbn + len - 1;
727 for (i = 0; i < len; i++)
728 if (!ffs_checkblk(ip,
729 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
730 panic("ffs_reallocblks: unallocated block 1");
731 for (i = 1; i < len; i++)
732 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
733 panic("ffs_reallocblks: non-logical cluster");
734 blkno = buflist->bs_children[0]->b_blkno;
735 ssize = fsbtodb(fs, fs->fs_frag);
736 for (i = 1; i < len - 1; i++)
737 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
738 panic("ffs_reallocblks: non-physical cluster %d", i);
741 * If the latest allocation is in a new cylinder group, assume that
742 * the filesystem has decided to move and do not force it back to
743 * the previous cylinder group.
745 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
746 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
748 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
749 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
752 * Get the starting offset and block map for the first block.
754 if (start_lvl == 0) {
755 sbap = &ip->i_din2->di_db[0];
758 idp = &start_ap[start_lvl - 1];
759 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
763 sbap = (ufs2_daddr_t *)sbp->b_data;
767 * If the block range spans two block maps, get the second map.
769 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
774 start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
775 panic("ffs_reallocblk: start == end");
777 ssize = len - (idp->in_off + 1);
778 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
780 ebap = (ufs2_daddr_t *)ebp->b_data;
783 * Find the preferred location for the cluster.
786 pref = ffs_blkpref_ufs2(ip, start_lbn, soff, sbap);
788 * Search the block map looking for an allocation of the desired size.
790 if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref,
791 len, ffs_clusteralloc)) == 0) {
796 * We have found a new contiguous block.
798 * First we have to replace the old block pointers with the new
799 * block pointers in the inode and indirect blocks associated
804 printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
805 (intmax_t)start_lbn, (intmax_t)end_lbn);
808 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
814 if (!ffs_checkblk(ip,
815 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
816 panic("ffs_reallocblks: unallocated block 2");
817 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
818 panic("ffs_reallocblks: alloc mismatch");
822 printf(" %jd,", (intmax_t)*bap);
824 if (DOINGSOFTDEP(vp)) {
825 if (sbap == &ip->i_din2->di_db[0] && i < ssize)
826 softdep_setup_allocdirect(ip, start_lbn + i,
827 blkno, *bap, fs->fs_bsize, fs->fs_bsize,
828 buflist->bs_children[i]);
830 softdep_setup_allocindir_page(ip, start_lbn + i,
831 i < ssize ? sbp : ebp, soff + i, blkno,
832 *bap, buflist->bs_children[i]);
837 * Next we must write out the modified inode and indirect blocks.
838 * For strict correctness, the writes should be synchronous since
839 * the old block values may have been written to disk. In practise
840 * they are almost never written, but if we are concerned about
841 * strict correctness, the `doasyncfree' flag should be set to zero.
843 * The test on `doasyncfree' should be changed to test a flag
844 * that shows whether the associated buffers and inodes have
845 * been written. The flag should be set when the cluster is
846 * started and cleared whenever the buffer or inode is flushed.
847 * We can then check below to see if it is set, and do the
848 * synchronous write only when it has been cleared.
850 if (sbap != &ip->i_din2->di_db[0]) {
856 ip->i_flag |= IN_CHANGE | IN_UPDATE;
867 * Last, free the old blocks and assign the new blocks to the buffers.
873 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
874 if (!DOINGSOFTDEP(vp))
875 ffs_blkfree(ump, fs, ip->i_devvp,
876 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
877 fs->fs_bsize, ip->i_number);
878 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
880 if (!ffs_checkblk(ip,
881 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
882 panic("ffs_reallocblks: unallocated block 3");
886 printf(" %jd,", (intmax_t)blkno);
900 if (sbap != &ip->i_din2->di_db[0])
906 * Allocate an inode in the filesystem.
908 * If allocating a directory, use ffs_dirpref to select the inode.
909 * If allocating in a directory, the following hierarchy is followed:
910 * 1) allocate the preferred inode.
911 * 2) allocate an inode in the same cylinder group.
912 * 3) quadradically rehash into other cylinder groups, until an
913 * available inode is located.
914 * If no inode preference is given the following hierarchy is used
915 * to allocate an inode:
916 * 1) allocate an inode in cylinder group 0.
917 * 2) quadradically rehash into other cylinder groups, until an
918 * available inode is located.
921 ffs_valloc(pvp, mode, cred, vpp)
931 struct ufsmount *ump;
933 int cg, error, error1;
934 static struct timeval lastfail;
943 if (fs->fs_cstotal.cs_nifree == 0)
946 if ((mode & IFMT) == IFDIR)
947 ipref = ffs_dirpref(pip);
949 ipref = pip->i_number;
950 if (ipref >= fs->fs_ncg * fs->fs_ipg)
952 cg = ino_to_cg(fs, ipref);
954 * Track number of dirs created one after another
955 * in a same cg without intervening by files.
957 if ((mode & IFMT) == IFDIR) {
958 if (fs->fs_contigdirs[cg] < 255)
959 fs->fs_contigdirs[cg]++;
961 if (fs->fs_contigdirs[cg] > 0)
962 fs->fs_contigdirs[cg]--;
964 ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode,
965 (allocfcn_t *)ffs_nodealloccg);
968 error = ffs_vget(pvp->v_mount, ino, LK_EXCLUSIVE, vpp);
970 error1 = ffs_vgetf(pvp->v_mount, ino, LK_EXCLUSIVE, vpp,
972 ffs_vfree(pvp, ino, mode);
977 ip->i_flag |= IN_MODIFIED;
985 printf("mode = 0%o, inum = %lu, fs = %s\n",
986 ip->i_mode, (u_long)ip->i_number, fs->fs_fsmnt);
987 panic("ffs_valloc: dup alloc");
989 if (DIP(ip, i_blocks) && (fs->fs_flags & FS_UNCLEAN) == 0) { /* XXX */
990 printf("free inode %s/%lu had %ld blocks\n",
991 fs->fs_fsmnt, (u_long)ino, (long)DIP(ip, i_blocks));
992 DIP_SET(ip, i_blocks, 0);
995 DIP_SET(ip, i_flags, 0);
997 * Set up a new generation number for this inode.
999 if (ip->i_gen == 0 || ++ip->i_gen == 0)
1000 ip->i_gen = arc4random() / 2 + 1;
1001 DIP_SET(ip, i_gen, ip->i_gen);
1002 if (fs->fs_magic == FS_UFS2_MAGIC) {
1004 ip->i_din2->di_birthtime = ts.tv_sec;
1005 ip->i_din2->di_birthnsec = ts.tv_nsec;
1008 vnode_destroy_vobject(*vpp);
1009 (*vpp)->v_type = VNON;
1010 if (fs->fs_magic == FS_UFS2_MAGIC)
1011 (*vpp)->v_op = &ffs_vnodeops2;
1013 (*vpp)->v_op = &ffs_vnodeops1;
1017 if (ppsratecheck(&lastfail, &curfail, 1)) {
1018 ffs_fserr(fs, pip->i_number, "out of inodes");
1019 uprintf("\n%s: create/symlink failed, no inodes free\n",
1026 * Find a cylinder group to place a directory.
1028 * The policy implemented by this algorithm is to allocate a
1029 * directory inode in the same cylinder group as its parent
1030 * directory, but also to reserve space for its files inodes
1031 * and data. Restrict the number of directories which may be
1032 * allocated one after another in the same cylinder group
1033 * without intervening allocation of files.
1035 * If we allocate a first level directory then force allocation
1036 * in another cylinder group.
1043 int cg, prefcg, dirsize, cgsize;
1044 int avgifree, avgbfree, avgndir, curdirsize;
1045 int minifree, minbfree, maxndir;
1049 mtx_assert(UFS_MTX(pip->i_ump), MA_OWNED);
1052 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
1053 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1054 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
1057 * Force allocation in another cg if creating a first level dir.
1059 ASSERT_VOP_LOCKED(ITOV(pip), "ffs_dirpref");
1060 if (ITOV(pip)->v_vflag & VV_ROOT) {
1061 prefcg = arc4random() % fs->fs_ncg;
1063 minndir = fs->fs_ipg;
1064 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1065 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1066 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1067 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1069 minndir = fs->fs_cs(fs, cg).cs_ndir;
1071 for (cg = 0; cg < prefcg; cg++)
1072 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1073 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1074 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1076 minndir = fs->fs_cs(fs, cg).cs_ndir;
1078 return ((ino_t)(fs->fs_ipg * mincg));
1082 * Count various limits which used for
1083 * optimal allocation of a directory inode.
1085 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
1086 minifree = avgifree - avgifree / 4;
1089 minbfree = avgbfree - avgbfree / 4;
1092 cgsize = fs->fs_fsize * fs->fs_fpg;
1093 dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
1094 curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
1095 if (dirsize < curdirsize)
1096 dirsize = curdirsize;
1098 maxcontigdirs = 0; /* dirsize overflowed */
1100 maxcontigdirs = min((avgbfree * fs->fs_bsize) / dirsize, 255);
1101 if (fs->fs_avgfpdir > 0)
1102 maxcontigdirs = min(maxcontigdirs,
1103 fs->fs_ipg / fs->fs_avgfpdir);
1104 if (maxcontigdirs == 0)
1108 * Limit number of dirs in one cg and reserve space for
1109 * regular files, but only if we have no deficit in
1112 prefcg = ino_to_cg(fs, pip->i_number);
1113 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1114 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1115 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1116 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1117 if (fs->fs_contigdirs[cg] < maxcontigdirs)
1118 return ((ino_t)(fs->fs_ipg * cg));
1120 for (cg = 0; cg < prefcg; cg++)
1121 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1122 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1123 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1124 if (fs->fs_contigdirs[cg] < maxcontigdirs)
1125 return ((ino_t)(fs->fs_ipg * cg));
1128 * This is a backstop when we have deficit in space.
1130 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1131 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1132 return ((ino_t)(fs->fs_ipg * cg));
1133 for (cg = 0; cg < prefcg; cg++)
1134 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1136 return ((ino_t)(fs->fs_ipg * cg));
1140 * Select the desired position for the next block in a file. The file is
1141 * logically divided into sections. The first section is composed of the
1142 * direct blocks. Each additional section contains fs_maxbpg blocks.
1144 * If no blocks have been allocated in the first section, the policy is to
1145 * request a block in the same cylinder group as the inode that describes
1146 * the file. If no blocks have been allocated in any other section, the
1147 * policy is to place the section in a cylinder group with a greater than
1148 * average number of free blocks. An appropriate cylinder group is found
1149 * by using a rotor that sweeps the cylinder groups. When a new group of
1150 * blocks is needed, the sweep begins in the cylinder group following the
1151 * cylinder group from which the previous allocation was made. The sweep
1152 * continues until a cylinder group with greater than the average number
1153 * of free blocks is found. If the allocation is for the first block in an
1154 * indirect block, the information on the previous allocation is unavailable;
1155 * here a best guess is made based upon the logical block number being
1158 * If a section is already partially allocated, the policy is to
1159 * contiguously allocate fs_maxcontig blocks. The end of one of these
1160 * contiguous blocks and the beginning of the next is laid out
1161 * contiguously if possible.
1164 ffs_blkpref_ufs1(ip, lbn, indx, bap)
1172 int avgbfree, startcg;
1174 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1176 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1177 if (lbn < NDADDR + NINDIR(fs)) {
1178 cg = ino_to_cg(fs, ip->i_number);
1179 return (cgbase(fs, cg) + fs->fs_frag);
1182 * Find a cylinder with greater than average number of
1183 * unused data blocks.
1185 if (indx == 0 || bap[indx - 1] == 0)
1187 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1189 startcg = dtog(fs, bap[indx - 1]) + 1;
1190 startcg %= fs->fs_ncg;
1191 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1192 for (cg = startcg; cg < fs->fs_ncg; cg++)
1193 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1194 fs->fs_cgrotor = cg;
1195 return (cgbase(fs, cg) + fs->fs_frag);
1197 for (cg = 0; cg <= startcg; cg++)
1198 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1199 fs->fs_cgrotor = cg;
1200 return (cgbase(fs, cg) + fs->fs_frag);
1205 * We just always try to lay things out contiguously.
1207 return (bap[indx - 1] + fs->fs_frag);
1211 * Same as above, but for UFS2
1214 ffs_blkpref_ufs2(ip, lbn, indx, bap)
1222 int avgbfree, startcg;
1224 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1226 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1227 if (lbn < NDADDR + NINDIR(fs)) {
1228 cg = ino_to_cg(fs, ip->i_number);
1229 return (cgbase(fs, cg) + fs->fs_frag);
1232 * Find a cylinder with greater than average number of
1233 * unused data blocks.
1235 if (indx == 0 || bap[indx - 1] == 0)
1237 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1239 startcg = dtog(fs, bap[indx - 1]) + 1;
1240 startcg %= fs->fs_ncg;
1241 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1242 for (cg = startcg; cg < fs->fs_ncg; cg++)
1243 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1244 fs->fs_cgrotor = cg;
1245 return (cgbase(fs, cg) + fs->fs_frag);
1247 for (cg = 0; cg <= startcg; cg++)
1248 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1249 fs->fs_cgrotor = cg;
1250 return (cgbase(fs, cg) + fs->fs_frag);
1255 * We just always try to lay things out contiguously.
1257 return (bap[indx - 1] + fs->fs_frag);
1261 * Implement the cylinder overflow algorithm.
1263 * The policy implemented by this algorithm is:
1264 * 1) allocate the block in its requested cylinder group.
1265 * 2) quadradically rehash on the cylinder group number.
1266 * 3) brute force search for a free block.
1268 * Must be called with the UFS lock held. Will release the lock on success
1269 * and return with it held on failure.
1273 ffs_hashalloc(ip, cg, pref, size, allocator)
1277 int size; /* size for data blocks, mode for inodes */
1278 allocfcn_t *allocator;
1281 ufs2_daddr_t result;
1284 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1286 if (ITOV(ip)->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
1287 panic("ffs_hashalloc: allocation on suspended filesystem");
1291 * 1: preferred cylinder group
1293 result = (*allocator)(ip, cg, pref, size);
1297 * 2: quadratic rehash
1299 for (i = 1; i < fs->fs_ncg; i *= 2) {
1301 if (cg >= fs->fs_ncg)
1303 result = (*allocator)(ip, cg, 0, size);
1308 * 3: brute force search
1309 * Note that we start at i == 2, since 0 was checked initially,
1310 * and 1 is always checked in the quadratic rehash.
1312 cg = (icg + 2) % fs->fs_ncg;
1313 for (i = 2; i < fs->fs_ncg; i++) {
1314 result = (*allocator)(ip, cg, 0, size);
1318 if (cg == fs->fs_ncg)
1325 * Determine whether a fragment can be extended.
1327 * Check to see if the necessary fragments are available, and
1328 * if they are, allocate them.
1331 ffs_fragextend(ip, cg, bprev, osize, nsize)
1340 struct ufsmount *ump;
1349 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
1351 frags = numfrags(fs, nsize);
1352 bbase = fragnum(fs, bprev);
1353 if (bbase > fragnum(fs, (bprev + frags - 1))) {
1354 /* cannot extend across a block boundary */
1358 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1359 (int)fs->fs_cgsize, NOCRED, &bp);
1362 cgp = (struct cg *)bp->b_data;
1363 if (!cg_chkmagic(cgp))
1365 bp->b_xflags |= BX_BKGRDWRITE;
1366 cgp->cg_old_time = cgp->cg_time = time_second;
1367 bno = dtogd(fs, bprev);
1368 blksfree = cg_blksfree(cgp);
1369 for (i = numfrags(fs, osize); i < frags; i++)
1370 if (isclr(blksfree, bno + i))
1373 * the current fragment can be extended
1374 * deduct the count on fragment being extended into
1375 * increase the count on the remaining fragment (if any)
1376 * allocate the extended piece
1378 for (i = frags; i < fs->fs_frag - bbase; i++)
1379 if (isclr(blksfree, bno + i))
1381 cgp->cg_frsum[i - numfrags(fs, osize)]--;
1383 cgp->cg_frsum[i - frags]++;
1384 for (i = numfrags(fs, osize), nffree = 0; i < frags; i++) {
1385 clrbit(blksfree, bno + i);
1386 cgp->cg_cs.cs_nffree--;
1390 fs->fs_cstotal.cs_nffree -= nffree;
1391 fs->fs_cs(fs, cg).cs_nffree -= nffree;
1393 ACTIVECLEAR(fs, cg);
1395 if (DOINGSOFTDEP(ITOV(ip)))
1396 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), bprev);
1408 * Determine whether a block can be allocated.
1410 * Check to see if a block of the appropriate size is available,
1411 * and if it is, allocate it.
1414 ffs_alloccg(ip, cg, bpref, size)
1423 struct ufsmount *ump;
1426 int i, allocsiz, error, frags;
1431 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1434 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1435 (int)fs->fs_cgsize, NOCRED, &bp);
1438 cgp = (struct cg *)bp->b_data;
1439 if (!cg_chkmagic(cgp) ||
1440 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize))
1442 bp->b_xflags |= BX_BKGRDWRITE;
1443 cgp->cg_old_time = cgp->cg_time = time_second;
1444 if (size == fs->fs_bsize) {
1446 blkno = ffs_alloccgblk(ip, bp, bpref);
1447 ACTIVECLEAR(fs, cg);
1453 * check to see if any fragments are already available
1454 * allocsiz is the size which will be allocated, hacking
1455 * it down to a smaller size if necessary
1457 blksfree = cg_blksfree(cgp);
1458 frags = numfrags(fs, size);
1459 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1460 if (cgp->cg_frsum[allocsiz] != 0)
1462 if (allocsiz == fs->fs_frag) {
1464 * no fragments were available, so a block will be
1465 * allocated, and hacked up
1467 if (cgp->cg_cs.cs_nbfree == 0)
1470 blkno = ffs_alloccgblk(ip, bp, bpref);
1471 bno = dtogd(fs, blkno);
1472 for (i = frags; i < fs->fs_frag; i++)
1473 setbit(blksfree, bno + i);
1474 i = fs->fs_frag - frags;
1475 cgp->cg_cs.cs_nffree += i;
1476 fs->fs_cstotal.cs_nffree += i;
1477 fs->fs_cs(fs, cg).cs_nffree += i;
1480 ACTIVECLEAR(fs, cg);
1485 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1488 for (i = 0; i < frags; i++)
1489 clrbit(blksfree, bno + i);
1490 cgp->cg_cs.cs_nffree -= frags;
1491 cgp->cg_frsum[allocsiz]--;
1492 if (frags != allocsiz)
1493 cgp->cg_frsum[allocsiz - frags]++;
1495 fs->fs_cstotal.cs_nffree -= frags;
1496 fs->fs_cs(fs, cg).cs_nffree -= frags;
1498 blkno = cgbase(fs, cg) + bno;
1499 ACTIVECLEAR(fs, cg);
1501 if (DOINGSOFTDEP(ITOV(ip)))
1502 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno);
1513 * Allocate a block in a cylinder group.
1515 * This algorithm implements the following policy:
1516 * 1) allocate the requested block.
1517 * 2) allocate a rotationally optimal block in the same cylinder.
1518 * 3) allocate the next available block on the block rotor for the
1519 * specified cylinder group.
1520 * Note that this routine only allocates fs_bsize blocks; these
1521 * blocks may be fragmented by the routine that allocates them.
1524 ffs_alloccgblk(ip, bp, bpref)
1531 struct ufsmount *ump;
1538 mtx_assert(UFS_MTX(ump), MA_OWNED);
1539 cgp = (struct cg *)bp->b_data;
1540 blksfree = cg_blksfree(cgp);
1541 if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
1542 bpref = cgp->cg_rotor;
1544 bpref = blknum(fs, bpref);
1545 bno = dtogd(fs, bpref);
1547 * if the requested block is available, use it
1549 if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
1553 * Take the next available block in this cylinder group.
1555 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1558 cgp->cg_rotor = bno;
1560 blkno = fragstoblks(fs, bno);
1561 ffs_clrblock(fs, blksfree, (long)blkno);
1562 ffs_clusteracct(ump, fs, cgp, blkno, -1);
1563 cgp->cg_cs.cs_nbfree--;
1564 fs->fs_cstotal.cs_nbfree--;
1565 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1567 blkno = cgbase(fs, cgp->cg_cgx) + bno;
1570 if (DOINGSOFTDEP(ITOV(ip)))
1571 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno);
1577 * Determine whether a cluster can be allocated.
1579 * We do not currently check for optimal rotational layout if there
1580 * are multiple choices in the same cylinder group. Instead we just
1581 * take the first one that we find following bpref.
1584 ffs_clusteralloc(ip, cg, bpref, len)
1593 struct ufsmount *ump;
1594 int i, run, bit, map, got;
1602 if (fs->fs_maxcluster[cg] < len)
1605 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1608 cgp = (struct cg *)bp->b_data;
1609 if (!cg_chkmagic(cgp))
1611 bp->b_xflags |= BX_BKGRDWRITE;
1613 * Check to see if a cluster of the needed size (or bigger) is
1614 * available in this cylinder group.
1616 lp = &cg_clustersum(cgp)[len];
1617 for (i = len; i <= fs->fs_contigsumsize; i++)
1620 if (i > fs->fs_contigsumsize) {
1622 * This is the first time looking for a cluster in this
1623 * cylinder group. Update the cluster summary information
1624 * to reflect the true maximum sized cluster so that
1625 * future cluster allocation requests can avoid reading
1626 * the cylinder group map only to find no clusters.
1628 lp = &cg_clustersum(cgp)[len - 1];
1629 for (i = len - 1; i > 0; i--)
1633 fs->fs_maxcluster[cg] = i;
1637 * Search the cluster map to find a big enough cluster.
1638 * We take the first one that we find, even if it is larger
1639 * than we need as we prefer to get one close to the previous
1640 * block allocation. We do not search before the current
1641 * preference point as we do not want to allocate a block
1642 * that is allocated before the previous one (as we will
1643 * then have to wait for another pass of the elevator
1644 * algorithm before it will be read). We prefer to fail and
1645 * be recalled to try an allocation in the next cylinder group.
1647 if (dtog(fs, bpref) != cg)
1650 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1651 mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1653 bit = 1 << (bpref % NBBY);
1654 for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
1655 if ((map & bit) == 0) {
1662 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1669 if (got >= cgp->cg_nclusterblks)
1672 * Allocate the cluster that we have found.
1674 blksfree = cg_blksfree(cgp);
1675 for (i = 1; i <= len; i++)
1676 if (!ffs_isblock(fs, blksfree, got - run + i))
1677 panic("ffs_clusteralloc: map mismatch");
1678 bno = cgbase(fs, cg) + blkstofrags(fs, got - run + 1);
1679 if (dtog(fs, bno) != cg)
1680 panic("ffs_clusteralloc: allocated out of group");
1681 len = blkstofrags(fs, len);
1683 for (i = 0; i < len; i += fs->fs_frag)
1684 if (ffs_alloccgblk(ip, bp, bno + i) != bno + i)
1685 panic("ffs_clusteralloc: lost block");
1686 ACTIVECLEAR(fs, cg);
1699 * Determine whether an inode can be allocated.
1701 * Check to see if an inode is available, and if it is,
1702 * allocate it using the following policy:
1703 * 1) allocate the requested inode.
1704 * 2) allocate the next available inode after the requested
1705 * inode in the specified cylinder group.
1708 ffs_nodealloccg(ip, cg, ipref, mode)
1716 struct buf *bp, *ibp;
1717 struct ufsmount *ump;
1719 struct ufs2_dinode *dp2;
1720 int error, start, len, loc, map, i;
1724 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1727 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1728 (int)fs->fs_cgsize, NOCRED, &bp);
1734 cgp = (struct cg *)bp->b_data;
1735 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
1740 bp->b_xflags |= BX_BKGRDWRITE;
1741 cgp->cg_old_time = cgp->cg_time = time_second;
1742 inosused = cg_inosused(cgp);
1744 ipref %= fs->fs_ipg;
1745 if (isclr(inosused, ipref))
1748 start = cgp->cg_irotor / NBBY;
1749 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
1750 loc = skpc(0xff, len, &inosused[start]);
1754 loc = skpc(0xff, len, &inosused[0]);
1756 printf("cg = %d, irotor = %ld, fs = %s\n",
1757 cg, (long)cgp->cg_irotor, fs->fs_fsmnt);
1758 panic("ffs_nodealloccg: map corrupted");
1762 i = start + len - loc;
1765 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1766 if ((map & i) == 0) {
1767 cgp->cg_irotor = ipref;
1771 printf("fs = %s\n", fs->fs_fsmnt);
1772 panic("ffs_nodealloccg: block not in map");
1776 * Check to see if we need to initialize more inodes.
1779 if (fs->fs_magic == FS_UFS2_MAGIC &&
1780 ipref + INOPB(fs) > cgp->cg_initediblk &&
1781 cgp->cg_initediblk < cgp->cg_niblk) {
1782 ibp = getblk(ip->i_devvp, fsbtodb(fs,
1783 ino_to_fsba(fs, cg * fs->fs_ipg + cgp->cg_initediblk)),
1784 (int)fs->fs_bsize, 0, 0, 0);
1785 bzero(ibp->b_data, (int)fs->fs_bsize);
1786 dp2 = (struct ufs2_dinode *)(ibp->b_data);
1787 for (i = 0; i < INOPB(fs); i++) {
1788 dp2->di_gen = arc4random() / 2 + 1;
1791 cgp->cg_initediblk += INOPB(fs);
1794 ACTIVECLEAR(fs, cg);
1795 setbit(inosused, ipref);
1796 cgp->cg_cs.cs_nifree--;
1797 fs->fs_cstotal.cs_nifree--;
1798 fs->fs_cs(fs, cg).cs_nifree--;
1800 if ((mode & IFMT) == IFDIR) {
1801 cgp->cg_cs.cs_ndir++;
1802 fs->fs_cstotal.cs_ndir++;
1803 fs->fs_cs(fs, cg).cs_ndir++;
1806 if (DOINGSOFTDEP(ITOV(ip)))
1807 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1811 return (cg * fs->fs_ipg + ipref);
1815 * check if a block is free
1818 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
1821 switch ((int)fs->fs_frag) {
1823 return (cp[h] == 0);
1825 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
1827 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
1829 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
1831 panic("ffs_isfreeblock");
1837 * Free a block or fragment.
1839 * The specified block or fragment is placed back in the
1840 * free map. If a fragment is deallocated, a possible
1841 * block reassembly is checked.
1844 ffs_blkfree(ump, fs, devvp, bno, size, inum)
1845 struct ufsmount *ump;
1847 struct vnode *devvp;
1854 ufs1_daddr_t fragno, cgbno;
1855 ufs2_daddr_t cgblkno;
1856 int i, cg, blk, frags, bbase;
1861 if (devvp->v_type == VREG) {
1862 /* devvp is a snapshot */
1863 dev = VTOI(devvp)->i_devvp->v_rdev;
1864 cgblkno = fragstoblks(fs, cgtod(fs, cg));
1866 /* devvp is a normal disk device */
1867 dev = devvp->v_rdev;
1868 cgblkno = fsbtodb(fs, cgtod(fs, cg));
1869 ASSERT_VOP_LOCKED(devvp, "ffs_blkfree");
1870 if ((devvp->v_vflag & VV_COPYONWRITE) &&
1871 ffs_snapblkfree(fs, devvp, bno, size, inum))
1875 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1876 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1877 printf("dev=%s, bno = %jd, bsize = %ld, size = %ld, fs = %s\n",
1878 devtoname(dev), (intmax_t)bno, (long)fs->fs_bsize,
1879 size, fs->fs_fsmnt);
1880 panic("ffs_blkfree: bad size");
1883 if ((u_int)bno >= fs->fs_size) {
1884 printf("bad block %jd, ino %lu\n", (intmax_t)bno,
1886 ffs_fserr(fs, inum, "bad block");
1889 if (bread(devvp, cgblkno, (int)fs->fs_cgsize, NOCRED, &bp)) {
1893 cgp = (struct cg *)bp->b_data;
1894 if (!cg_chkmagic(cgp)) {
1898 bp->b_xflags |= BX_BKGRDWRITE;
1899 cgp->cg_old_time = cgp->cg_time = time_second;
1900 cgbno = dtogd(fs, bno);
1901 blksfree = cg_blksfree(cgp);
1903 if (size == fs->fs_bsize) {
1904 fragno = fragstoblks(fs, cgbno);
1905 if (!ffs_isfreeblock(fs, blksfree, fragno)) {
1906 if (devvp->v_type == VREG) {
1908 /* devvp is a snapshot */
1912 printf("dev = %s, block = %jd, fs = %s\n",
1913 devtoname(dev), (intmax_t)bno, fs->fs_fsmnt);
1914 panic("ffs_blkfree: freeing free block");
1916 ffs_setblock(fs, blksfree, fragno);
1917 ffs_clusteracct(ump, fs, cgp, fragno, 1);
1918 cgp->cg_cs.cs_nbfree++;
1919 fs->fs_cstotal.cs_nbfree++;
1920 fs->fs_cs(fs, cg).cs_nbfree++;
1922 bbase = cgbno - fragnum(fs, cgbno);
1924 * decrement the counts associated with the old frags
1926 blk = blkmap(fs, blksfree, bbase);
1927 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
1929 * deallocate the fragment
1931 frags = numfrags(fs, size);
1932 for (i = 0; i < frags; i++) {
1933 if (isset(blksfree, cgbno + i)) {
1934 printf("dev = %s, block = %jd, fs = %s\n",
1935 devtoname(dev), (intmax_t)(bno + i),
1937 panic("ffs_blkfree: freeing free frag");
1939 setbit(blksfree, cgbno + i);
1941 cgp->cg_cs.cs_nffree += i;
1942 fs->fs_cstotal.cs_nffree += i;
1943 fs->fs_cs(fs, cg).cs_nffree += i;
1945 * add back in counts associated with the new frags
1947 blk = blkmap(fs, blksfree, bbase);
1948 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
1950 * if a complete block has been reassembled, account for it
1952 fragno = fragstoblks(fs, bbase);
1953 if (ffs_isblock(fs, blksfree, fragno)) {
1954 cgp->cg_cs.cs_nffree -= fs->fs_frag;
1955 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1956 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1957 ffs_clusteracct(ump, fs, cgp, fragno, 1);
1958 cgp->cg_cs.cs_nbfree++;
1959 fs->fs_cstotal.cs_nbfree++;
1960 fs->fs_cs(fs, cg).cs_nbfree++;
1964 ACTIVECLEAR(fs, cg);
1971 * Verify allocation of a block or fragment. Returns true if block or
1972 * fragment is allocated, false if it is free.
1975 ffs_checkblk(ip, bno, size)
1984 int i, error, frags, free;
1988 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1989 printf("bsize = %ld, size = %ld, fs = %s\n",
1990 (long)fs->fs_bsize, size, fs->fs_fsmnt);
1991 panic("ffs_checkblk: bad size");
1993 if ((u_int)bno >= fs->fs_size)
1994 panic("ffs_checkblk: bad block %jd", (intmax_t)bno);
1995 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
1996 (int)fs->fs_cgsize, NOCRED, &bp);
1998 panic("ffs_checkblk: cg bread failed");
1999 cgp = (struct cg *)bp->b_data;
2000 if (!cg_chkmagic(cgp))
2001 panic("ffs_checkblk: cg magic mismatch");
2002 bp->b_xflags |= BX_BKGRDWRITE;
2003 blksfree = cg_blksfree(cgp);
2004 cgbno = dtogd(fs, bno);
2005 if (size == fs->fs_bsize) {
2006 free = ffs_isblock(fs, blksfree, fragstoblks(fs, cgbno));
2008 frags = numfrags(fs, size);
2009 for (free = 0, i = 0; i < frags; i++)
2010 if (isset(blksfree, cgbno + i))
2012 if (free != 0 && free != frags)
2013 panic("ffs_checkblk: partially free fragment");
2018 #endif /* INVARIANTS */
2024 ffs_vfree(pvp, ino, mode)
2031 if (DOINGSOFTDEP(pvp)) {
2032 softdep_freefile(pvp, ino, mode);
2036 return (ffs_freefile(ip->i_ump, ip->i_fs, ip->i_devvp, ino, mode));
2040 * Do the actual free operation.
2041 * The specified inode is placed back in the free map.
2044 ffs_freefile(ump, fs, devvp, ino, mode)
2045 struct ufsmount *ump;
2047 struct vnode *devvp;
2058 cg = ino_to_cg(fs, ino);
2059 if (devvp->v_type == VREG) {
2060 /* devvp is a snapshot */
2061 dev = VTOI(devvp)->i_devvp->v_rdev;
2062 cgbno = fragstoblks(fs, cgtod(fs, cg));
2064 /* devvp is a normal disk device */
2065 dev = devvp->v_rdev;
2066 cgbno = fsbtodb(fs, cgtod(fs, cg));
2068 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
2069 panic("ffs_freefile: range: dev = %s, ino = %lu, fs = %s",
2070 devtoname(dev), (u_long)ino, fs->fs_fsmnt);
2071 if ((error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp))) {
2075 cgp = (struct cg *)bp->b_data;
2076 if (!cg_chkmagic(cgp)) {
2080 bp->b_xflags |= BX_BKGRDWRITE;
2081 cgp->cg_old_time = cgp->cg_time = time_second;
2082 inosused = cg_inosused(cgp);
2084 if (isclr(inosused, ino)) {
2085 printf("dev = %s, ino = %lu, fs = %s\n", devtoname(dev),
2086 (u_long)ino + cg * fs->fs_ipg, fs->fs_fsmnt);
2087 if (fs->fs_ronly == 0)
2088 panic("ffs_freefile: freeing free inode");
2090 clrbit(inosused, ino);
2091 if (ino < cgp->cg_irotor)
2092 cgp->cg_irotor = ino;
2093 cgp->cg_cs.cs_nifree++;
2095 fs->fs_cstotal.cs_nifree++;
2096 fs->fs_cs(fs, cg).cs_nifree++;
2097 if ((mode & IFMT) == IFDIR) {
2098 cgp->cg_cs.cs_ndir--;
2099 fs->fs_cstotal.cs_ndir--;
2100 fs->fs_cs(fs, cg).cs_ndir--;
2103 ACTIVECLEAR(fs, cg);
2110 * Check to see if a file is free.
2113 ffs_checkfreefile(fs, devvp, ino)
2115 struct vnode *devvp;
2124 cg = ino_to_cg(fs, ino);
2125 if (devvp->v_type == VREG) {
2126 /* devvp is a snapshot */
2127 cgbno = fragstoblks(fs, cgtod(fs, cg));
2129 /* devvp is a normal disk device */
2130 cgbno = fsbtodb(fs, cgtod(fs, cg));
2132 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
2134 if (bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp)) {
2138 cgp = (struct cg *)bp->b_data;
2139 if (!cg_chkmagic(cgp)) {
2143 inosused = cg_inosused(cgp);
2145 ret = isclr(inosused, ino);
2151 * Find a block of the specified size in the specified cylinder group.
2153 * It is a panic if a request is made to find a block if none are
2157 ffs_mapsearch(fs, cgp, bpref, allocsiz)
2164 int start, len, loc, i;
2165 int blk, field, subfield, pos;
2169 * find the fragment by searching through the free block
2170 * map for an appropriate bit pattern
2173 start = dtogd(fs, bpref) / NBBY;
2175 start = cgp->cg_frotor / NBBY;
2176 blksfree = cg_blksfree(cgp);
2177 len = howmany(fs->fs_fpg, NBBY) - start;
2178 loc = scanc((u_int)len, (u_char *)&blksfree[start],
2179 fragtbl[fs->fs_frag],
2180 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2184 loc = scanc((u_int)len, (u_char *)&blksfree[0],
2185 fragtbl[fs->fs_frag],
2186 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2188 printf("start = %d, len = %d, fs = %s\n",
2189 start, len, fs->fs_fsmnt);
2190 panic("ffs_alloccg: map corrupted");
2194 bno = (start + len - loc) * NBBY;
2195 cgp->cg_frotor = bno;
2197 * found the byte in the map
2198 * sift through the bits to find the selected frag
2200 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
2201 blk = blkmap(fs, blksfree, bno);
2203 field = around[allocsiz];
2204 subfield = inside[allocsiz];
2205 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
2206 if ((blk & field) == subfield)
2212 printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt);
2213 panic("ffs_alloccg: block not in map");
2218 * Update the cluster map because of an allocation or free.
2220 * Cnt == 1 means free; cnt == -1 means allocating.
2223 ffs_clusteracct(ump, fs, cgp, blkno, cnt)
2224 struct ufsmount *ump;
2232 u_char *freemapp, *mapp;
2233 int i, start, end, forw, back, map, bit;
2235 mtx_assert(UFS_MTX(ump), MA_OWNED);
2237 if (fs->fs_contigsumsize <= 0)
2239 freemapp = cg_clustersfree(cgp);
2240 sump = cg_clustersum(cgp);
2242 * Allocate or clear the actual block.
2245 setbit(freemapp, blkno);
2247 clrbit(freemapp, blkno);
2249 * Find the size of the cluster going forward.
2252 end = start + fs->fs_contigsumsize;
2253 if (end >= cgp->cg_nclusterblks)
2254 end = cgp->cg_nclusterblks;
2255 mapp = &freemapp[start / NBBY];
2257 bit = 1 << (start % NBBY);
2258 for (i = start; i < end; i++) {
2259 if ((map & bit) == 0)
2261 if ((i & (NBBY - 1)) != (NBBY - 1)) {
2270 * Find the size of the cluster going backward.
2273 end = start - fs->fs_contigsumsize;
2276 mapp = &freemapp[start / NBBY];
2278 bit = 1 << (start % NBBY);
2279 for (i = start; i > end; i--) {
2280 if ((map & bit) == 0)
2282 if ((i & (NBBY - 1)) != 0) {
2286 bit = 1 << (NBBY - 1);
2291 * Account for old cluster and the possibly new forward and
2294 i = back + forw + 1;
2295 if (i > fs->fs_contigsumsize)
2296 i = fs->fs_contigsumsize;
2303 * Update cluster summary information.
2305 lp = &sump[fs->fs_contigsumsize];
2306 for (i = fs->fs_contigsumsize; i > 0; i--)
2309 fs->fs_maxcluster[cgp->cg_cgx] = i;
2313 * Fserr prints the name of a filesystem with an error diagnostic.
2315 * The form of the error message is:
2319 ffs_fserr(fs, inum, cp)
2324 struct thread *td = curthread; /* XXX */
2325 struct proc *p = td->td_proc;
2327 log(LOG_ERR, "pid %d (%s), uid %d inumber %d on %s: %s\n",
2328 p->p_pid, p->p_comm, td->td_ucred->cr_uid, inum, fs->fs_fsmnt, cp);
2332 * This function provides the capability for the fsck program to
2333 * update an active filesystem. Eleven operations are provided:
2335 * adjrefcnt(inode, amt) - adjusts the reference count on the
2336 * specified inode by the specified amount. Under normal
2337 * operation the count should always go down. Decrementing
2338 * the count to zero will cause the inode to be freed.
2339 * adjblkcnt(inode, amt) - adjust the number of blocks used to
2340 * by the specifed amount.
2341 * adjndir, adjbfree, adjifree, adjffree, adjnumclusters(amt) -
2342 * adjust the superblock summary.
2343 * freedirs(inode, count) - directory inodes [inode..inode + count - 1]
2344 * are marked as free. Inodes should never have to be marked
2346 * freefiles(inode, count) - file inodes [inode..inode + count - 1]
2347 * are marked as free. Inodes should never have to be marked
2349 * freeblks(blockno, size) - blocks [blockno..blockno + size - 1]
2350 * are marked as free. Blocks should never have to be marked
2352 * setflags(flags, set/clear) - the fs_flags field has the specified
2353 * flags set (second parameter +1) or cleared (second parameter -1).
2356 static int sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS);
2358 SYSCTL_PROC(_vfs_ffs, FFS_ADJ_REFCNT, adjrefcnt, CTLFLAG_WR|CTLTYPE_STRUCT,
2359 0, 0, sysctl_ffs_fsck, "S,fsck", "Adjust Inode Reference Count");
2361 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_BLKCNT, adjblkcnt, CTLFLAG_WR,
2362 sysctl_ffs_fsck, "Adjust Inode Used Blocks Count");
2364 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NDIR, adjndir, CTLFLAG_WR,
2365 sysctl_ffs_fsck, "Adjust number of directories");
2367 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NBFREE, adjnbfree, CTLFLAG_WR,
2368 sysctl_ffs_fsck, "Adjust number of free blocks");
2370 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NIFREE, adjnifree, CTLFLAG_WR,
2371 sysctl_ffs_fsck, "Adjust number of free inodes");
2373 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NFFREE, adjnffree, CTLFLAG_WR,
2374 sysctl_ffs_fsck, "Adjust number of free frags");
2376 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NUMCLUSTERS, adjnumclusters, CTLFLAG_WR,
2377 sysctl_ffs_fsck, "Adjust number of free clusters");
2379 static SYSCTL_NODE(_vfs_ffs, FFS_DIR_FREE, freedirs, CTLFLAG_WR,
2380 sysctl_ffs_fsck, "Free Range of Directory Inodes");
2382 static SYSCTL_NODE(_vfs_ffs, FFS_FILE_FREE, freefiles, CTLFLAG_WR,
2383 sysctl_ffs_fsck, "Free Range of File Inodes");
2385 static SYSCTL_NODE(_vfs_ffs, FFS_BLK_FREE, freeblks, CTLFLAG_WR,
2386 sysctl_ffs_fsck, "Free Range of Blocks");
2388 static SYSCTL_NODE(_vfs_ffs, FFS_SET_FLAGS, setflags, CTLFLAG_WR,
2389 sysctl_ffs_fsck, "Change Filesystem Flags");
2392 static int fsckcmds = 0;
2393 SYSCTL_INT(_debug, OID_AUTO, fsckcmds, CTLFLAG_RW, &fsckcmds, 0, "");
2397 sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS)
2399 struct fsck_cmd cmd;
2400 struct ufsmount *ump;
2406 long blkcnt, blksize;
2408 int filetype, error;
2410 if (req->newlen > sizeof cmd)
2412 if ((error = SYSCTL_IN(req, &cmd, sizeof cmd)) != 0)
2414 if (cmd.version != FFS_CMD_VERSION)
2415 return (ERPCMISMATCH);
2416 if ((error = getvnode(curproc->p_fd, cmd.handle, &fp)) != 0)
2418 vn_start_write(fp->f_data, &mp, V_WAIT);
2419 if (mp == 0 || strncmp(mp->mnt_stat.f_fstypename, "ufs", MFSNAMELEN)) {
2420 vn_finished_write(mp);
2421 fdrop(fp, curthread);
2424 if (mp->mnt_flag & MNT_RDONLY) {
2425 vn_finished_write(mp);
2426 fdrop(fp, curthread);
2433 switch (oidp->oid_number) {
2438 printf("%s: %s flags\n", mp->mnt_stat.f_mntonname,
2439 cmd.size > 0 ? "set" : "clear");
2442 fs->fs_flags |= (long)cmd.value;
2444 fs->fs_flags &= ~(long)cmd.value;
2447 case FFS_ADJ_REFCNT:
2450 printf("%s: adjust inode %jd 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 ip->i_nlink += cmd.size;
2459 DIP_SET(ip, i_nlink, ip->i_nlink);
2460 ip->i_effnlink += cmd.size;
2461 ip->i_flag |= IN_CHANGE;
2462 if (DOINGSOFTDEP(vp))
2463 softdep_change_linkcnt(ip);
2467 case FFS_ADJ_BLKCNT:
2470 printf("%s: adjust inode %jd block count by %jd\n",
2471 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2472 (intmax_t)cmd.size);
2475 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2478 if (ip->i_flag & IN_SPACECOUNTED) {
2480 fs->fs_pendingblocks += cmd.size;
2483 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + cmd.size);
2484 ip->i_flag |= IN_CHANGE;
2496 printf("%s: free %s inode %d\n",
2497 mp->mnt_stat.f_mntonname,
2498 filetype == IFDIR ? "directory" : "file",
2501 printf("%s: free %s inodes %d-%d\n",
2502 mp->mnt_stat.f_mntonname,
2503 filetype == IFDIR ? "directory" : "file",
2505 (ino_t)(cmd.value + cmd.size - 1));
2508 while (cmd.size > 0) {
2509 if ((error = ffs_freefile(ump, fs, ump->um_devvp,
2510 cmd.value, filetype)))
2521 printf("%s: free block %jd\n",
2522 mp->mnt_stat.f_mntonname,
2523 (intmax_t)cmd.value);
2525 printf("%s: free blocks %jd-%jd\n",
2526 mp->mnt_stat.f_mntonname,
2527 (intmax_t)cmd.value,
2528 (intmax_t)cmd.value + cmd.size - 1);
2533 blksize = fs->fs_frag - (blkno % fs->fs_frag);
2534 while (blkcnt > 0) {
2535 if (blksize > blkcnt)
2537 ffs_blkfree(ump, fs, ump->um_devvp, blkno,
2538 blksize * fs->fs_fsize, ROOTINO);
2541 blksize = fs->fs_frag;
2546 * Adjust superblock summaries. fsck(8) is expected to
2547 * submit deltas when necessary.
2552 printf("%s: adjust number of directories by %jd\n",
2553 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2556 fs->fs_cstotal.cs_ndir += cmd.value;
2558 case FFS_ADJ_NBFREE:
2561 printf("%s: adjust number of free blocks by %+jd\n",
2562 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2565 fs->fs_cstotal.cs_nbfree += cmd.value;
2567 case FFS_ADJ_NIFREE:
2570 printf("%s: adjust number of free inodes by %+jd\n",
2571 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2574 fs->fs_cstotal.cs_nifree += cmd.value;
2576 case FFS_ADJ_NFFREE:
2579 printf("%s: adjust number of free frags by %+jd\n",
2580 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2583 fs->fs_cstotal.cs_nffree += cmd.value;
2585 case FFS_ADJ_NUMCLUSTERS:
2588 printf("%s: adjust number of free clusters by %+jd\n",
2589 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2592 fs->fs_cstotal.cs_numclusters += cmd.value;
2598 printf("Invalid request %d from fsck\n",
2606 fdrop(fp, curthread);
2607 vn_finished_write(mp);