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, u_int cg, ufs2_daddr_t bpref,
94 static ufs2_daddr_t ffs_alloccg(struct inode *, u_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 *, u_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 *, u_int, ufs2_daddr_t,
106 static void ffs_fserr(struct fs *, ino_t, char *);
107 static ufs2_daddr_t ffs_hashalloc
108 (struct inode *, u_int, ufs2_daddr_t, int, allocfcn_t *);
109 static ufs2_daddr_t ffs_nodealloccg(struct inode *, u_int, ufs2_daddr_t, int);
110 static ufs1_daddr_t ffs_mapsearch(struct fs *, struct cg *, ufs2_daddr_t, int);
111 static int ffs_reallocblks_ufs1(struct vop_reallocblks_args *);
112 static int ffs_reallocblks_ufs2(struct vop_reallocblks_args *);
115 * Allocate a block in the filesystem.
117 * The size of the requested block is given, which must be some
118 * multiple of fs_fsize and <= fs_bsize.
119 * A preference may be optionally specified. If a preference is given
120 * the following hierarchy is used to allocate a block:
121 * 1) allocate the requested block.
122 * 2) allocate a rotationally optimal block in the same cylinder.
123 * 3) allocate a block in the same cylinder group.
124 * 4) quadradically rehash into other cylinder groups, until an
125 * available block is located.
126 * If no block preference is given the following hierarchy is used
127 * to allocate a block:
128 * 1) allocate a block in the cylinder group that contains the
129 * inode for the file.
130 * 2) quadradically rehash into other cylinder groups, until an
131 * available block is located.
134 ffs_alloc(ip, lbn, bpref, size, flags, cred, bnp)
136 ufs2_daddr_t lbn, bpref;
142 struct ufsmount *ump;
145 static struct timeval lastfail;
155 mtx_assert(UFS_MTX(ump), MA_OWNED);
157 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
158 printf("dev = %s, bsize = %ld, size = %d, fs = %s\n",
159 devtoname(ip->i_dev), (long)fs->fs_bsize, size,
161 panic("ffs_alloc: bad size");
164 panic("ffs_alloc: missing credential");
165 #endif /* INVARIANTS */
170 error = chkdq(ip, btodb(size), cred, 0);
175 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
177 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0) &&
178 freespace(fs, fs->fs_minfree) - numfrags(fs, size) < 0)
180 if (bpref >= fs->fs_size)
183 cg = ino_to_cg(fs, ip->i_number);
185 cg = dtog(fs, bpref);
186 bno = ffs_hashalloc(ip, cg, bpref, size, ffs_alloccg);
189 if (ip->i_flag & IN_SPACECOUNTED) {
191 fs->fs_pendingblocks += delta;
194 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
196 ip->i_flag |= IN_CHANGE;
198 ip->i_flag |= IN_CHANGE | IN_UPDATE;
206 * Restore user's disk quota because allocation failed.
208 (void) chkdq(ip, -btodb(size), cred, FORCE);
211 if (fs->fs_pendingblocks > 0 && reclaimed == 0) {
213 softdep_request_cleanup(fs, ITOV(ip));
217 if (ppsratecheck(&lastfail, &curfail, 1)) {
218 ffs_fserr(fs, ip->i_number, "filesystem full");
219 uprintf("\n%s: write failed, filesystem is full\n",
226 * Reallocate a fragment to a bigger size
228 * The number and size of the old block is given, and a preference
229 * and new size is also specified. The allocator attempts to extend
230 * the original block. Failing that, the regular block allocator is
231 * invoked to get an appropriate block.
234 ffs_realloccg(ip, lbprev, bprev, bpref, osize, nsize, flags, cred, bpp)
239 int osize, nsize, flags;
246 struct ufsmount *ump;
247 u_int cg, request, reclaimed;
250 static struct timeval lastfail;
259 mtx_assert(UFS_MTX(ump), MA_OWNED);
261 if (vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
262 panic("ffs_realloccg: allocation on suspended filesystem");
263 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
264 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
266 "dev = %s, bsize = %ld, osize = %d, nsize = %d, fs = %s\n",
267 devtoname(ip->i_dev), (long)fs->fs_bsize, osize,
268 nsize, fs->fs_fsmnt);
269 panic("ffs_realloccg: bad size");
272 panic("ffs_realloccg: missing credential");
273 #endif /* INVARIANTS */
276 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0) &&
277 freespace(fs, fs->fs_minfree) - numfrags(fs, nsize - osize) < 0) {
281 printf("dev = %s, bsize = %ld, bprev = %jd, fs = %s\n",
282 devtoname(ip->i_dev), (long)fs->fs_bsize, (intmax_t)bprev,
284 panic("ffs_realloccg: bad bprev");
288 * Allocate the extra space in the buffer.
290 error = bread(vp, lbprev, osize, NOCRED, &bp);
296 if (bp->b_blkno == bp->b_lblkno) {
297 if (lbprev >= NDADDR)
298 panic("ffs_realloccg: lbprev out of range");
299 bp->b_blkno = fsbtodb(fs, bprev);
303 error = chkdq(ip, btodb(nsize - osize), cred, 0);
310 * Check for extension in the existing location.
312 cg = dtog(fs, bprev);
314 bno = ffs_fragextend(ip, cg, bprev, osize, nsize);
316 if (bp->b_blkno != fsbtodb(fs, bno))
317 panic("ffs_realloccg: bad blockno");
318 delta = btodb(nsize - osize);
319 if (ip->i_flag & IN_SPACECOUNTED) {
321 fs->fs_pendingblocks += delta;
324 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
326 ip->i_flag |= IN_CHANGE;
328 ip->i_flag |= IN_CHANGE | IN_UPDATE;
330 bp->b_flags |= B_DONE;
331 bzero(bp->b_data + osize, nsize - osize);
332 if ((bp->b_flags & (B_MALLOC | B_VMIO)) == B_VMIO)
333 vfs_bio_set_valid(bp, osize, nsize - osize);
338 * Allocate a new disk location.
340 if (bpref >= fs->fs_size)
342 switch ((int)fs->fs_optim) {
345 * Allocate an exact sized fragment. Although this makes
346 * best use of space, we will waste time relocating it if
347 * the file continues to grow. If the fragmentation is
348 * less than half of the minimum free reserve, we choose
349 * to begin optimizing for time.
352 if (fs->fs_minfree <= 5 ||
353 fs->fs_cstotal.cs_nffree >
354 (off_t)fs->fs_dsize * fs->fs_minfree / (2 * 100))
356 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
358 fs->fs_optim = FS_OPTTIME;
362 * At this point we have discovered a file that is trying to
363 * grow a small fragment to a larger fragment. To save time,
364 * we allocate a full sized block, then free the unused portion.
365 * If the file continues to grow, the `ffs_fragextend' call
366 * above will be able to grow it in place without further
367 * copying. If aberrant programs cause disk fragmentation to
368 * grow within 2% of the free reserve, we choose to begin
369 * optimizing for space.
371 request = fs->fs_bsize;
372 if (fs->fs_cstotal.cs_nffree <
373 (off_t)fs->fs_dsize * (fs->fs_minfree - 2) / 100)
375 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
377 fs->fs_optim = FS_OPTSPACE;
380 printf("dev = %s, optim = %ld, fs = %s\n",
381 devtoname(ip->i_dev), (long)fs->fs_optim, fs->fs_fsmnt);
382 panic("ffs_realloccg: bad optim");
385 bno = ffs_hashalloc(ip, cg, bpref, request, ffs_alloccg);
387 bp->b_blkno = fsbtodb(fs, bno);
388 if (!DOINGSOFTDEP(vp))
389 ffs_blkfree(ump, fs, ip->i_devvp, bprev, (long)osize,
392 ffs_blkfree(ump, fs, ip->i_devvp,
393 bno + numfrags(fs, nsize),
394 (long)(request - nsize), ip->i_number);
395 delta = btodb(nsize - osize);
396 if (ip->i_flag & IN_SPACECOUNTED) {
398 fs->fs_pendingblocks += delta;
401 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
403 ip->i_flag |= IN_CHANGE;
405 ip->i_flag |= IN_CHANGE | IN_UPDATE;
407 bp->b_flags |= B_DONE;
408 bzero(bp->b_data + osize, nsize - osize);
409 if ((bp->b_flags & (B_MALLOC | B_VMIO)) == B_VMIO)
410 vfs_bio_set_valid(bp, osize, nsize - osize);
417 * Restore user's disk quota because allocation failed.
419 (void) chkdq(ip, -btodb(nsize - osize), cred, FORCE);
426 if (fs->fs_pendingblocks > 0 && reclaimed == 0) {
428 softdep_request_cleanup(fs, vp);
440 if (ppsratecheck(&lastfail, &curfail, 1)) {
441 ffs_fserr(fs, ip->i_number, "filesystem full");
442 uprintf("\n%s: write failed, filesystem is full\n",
449 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
451 * The vnode and an array of buffer pointers for a range of sequential
452 * logical blocks to be made contiguous is given. The allocator attempts
453 * to find a range of sequential blocks starting as close as possible
454 * from the end of the allocation for the logical block immediately
455 * preceding the current range. If successful, the physical block numbers
456 * in the buffer pointers and in the inode are changed to reflect the new
457 * allocation. If unsuccessful, the allocation is left unchanged. The
458 * success in doing the reallocation is returned. Note that the error
459 * return is not reflected back to the user. Rather the previous block
460 * allocation will be used.
463 SYSCTL_NODE(_vfs, OID_AUTO, ffs, CTLFLAG_RW, 0, "FFS filesystem");
465 static int doasyncfree = 1;
466 SYSCTL_INT(_vfs_ffs, OID_AUTO, doasyncfree, CTLFLAG_RW, &doasyncfree, 0, "");
468 static int doreallocblks = 1;
469 SYSCTL_INT(_vfs_ffs, OID_AUTO, doreallocblks, CTLFLAG_RW, &doreallocblks, 0, "");
472 static volatile int prtrealloc = 0;
477 struct vop_reallocblks_args /* {
479 struct cluster_save *a_buflist;
483 if (doreallocblks == 0)
485 if (VTOI(ap->a_vp)->i_ump->um_fstype == UFS1)
486 return (ffs_reallocblks_ufs1(ap));
487 return (ffs_reallocblks_ufs2(ap));
491 ffs_reallocblks_ufs1(ap)
492 struct vop_reallocblks_args /* {
494 struct cluster_save *a_buflist;
500 struct buf *sbp, *ebp;
501 ufs1_daddr_t *bap, *sbap, *ebap = 0;
502 struct cluster_save *buflist;
503 struct ufsmount *ump;
504 ufs_lbn_t start_lbn, end_lbn;
505 ufs1_daddr_t soff, newblk, blkno;
507 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
508 int i, len, start_lvl, end_lvl, ssize;
514 if (fs->fs_contigsumsize <= 0)
516 buflist = ap->a_buflist;
517 len = buflist->bs_nchildren;
518 start_lbn = buflist->bs_children[0]->b_lblkno;
519 end_lbn = start_lbn + len - 1;
521 for (i = 0; i < len; i++)
522 if (!ffs_checkblk(ip,
523 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
524 panic("ffs_reallocblks: unallocated block 1");
525 for (i = 1; i < len; i++)
526 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
527 panic("ffs_reallocblks: non-logical cluster");
528 blkno = buflist->bs_children[0]->b_blkno;
529 ssize = fsbtodb(fs, fs->fs_frag);
530 for (i = 1; i < len - 1; i++)
531 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
532 panic("ffs_reallocblks: non-physical cluster %d", i);
535 * If the latest allocation is in a new cylinder group, assume that
536 * the filesystem has decided to move and do not force it back to
537 * the previous cylinder group.
539 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
540 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
542 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
543 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
546 * Get the starting offset and block map for the first block.
548 if (start_lvl == 0) {
549 sbap = &ip->i_din1->di_db[0];
552 idp = &start_ap[start_lvl - 1];
553 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
557 sbap = (ufs1_daddr_t *)sbp->b_data;
561 * If the block range spans two block maps, get the second map.
563 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
568 start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
569 panic("ffs_reallocblk: start == end");
571 ssize = len - (idp->in_off + 1);
572 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
574 ebap = (ufs1_daddr_t *)ebp->b_data;
577 * Find the preferred location for the cluster.
580 pref = ffs_blkpref_ufs1(ip, start_lbn, soff, sbap);
582 * Search the block map looking for an allocation of the desired size.
584 if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref,
585 len, ffs_clusteralloc)) == 0) {
590 * We have found a new contiguous block.
592 * First we have to replace the old block pointers with the new
593 * block pointers in the inode and indirect blocks associated
598 printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
599 (intmax_t)start_lbn, (intmax_t)end_lbn);
602 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
608 if (!ffs_checkblk(ip,
609 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
610 panic("ffs_reallocblks: unallocated block 2");
611 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
612 panic("ffs_reallocblks: alloc mismatch");
616 printf(" %d,", *bap);
618 if (DOINGSOFTDEP(vp)) {
619 if (sbap == &ip->i_din1->di_db[0] && i < ssize)
620 softdep_setup_allocdirect(ip, start_lbn + i,
621 blkno, *bap, fs->fs_bsize, fs->fs_bsize,
622 buflist->bs_children[i]);
624 softdep_setup_allocindir_page(ip, start_lbn + i,
625 i < ssize ? sbp : ebp, soff + i, blkno,
626 *bap, buflist->bs_children[i]);
631 * Next we must write out the modified inode and indirect blocks.
632 * For strict correctness, the writes should be synchronous since
633 * the old block values may have been written to disk. In practise
634 * they are almost never written, but if we are concerned about
635 * strict correctness, the `doasyncfree' flag should be set to zero.
637 * The test on `doasyncfree' should be changed to test a flag
638 * that shows whether the associated buffers and inodes have
639 * been written. The flag should be set when the cluster is
640 * started and cleared whenever the buffer or inode is flushed.
641 * We can then check below to see if it is set, and do the
642 * synchronous write only when it has been cleared.
644 if (sbap != &ip->i_din1->di_db[0]) {
650 ip->i_flag |= IN_CHANGE | IN_UPDATE;
661 * Last, free the old blocks and assign the new blocks to the buffers.
667 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
668 if (!DOINGSOFTDEP(vp))
669 ffs_blkfree(ump, fs, ip->i_devvp,
670 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
671 fs->fs_bsize, ip->i_number);
672 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
674 if (!ffs_checkblk(ip,
675 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
676 panic("ffs_reallocblks: unallocated block 3");
680 printf(" %d,", blkno);
694 if (sbap != &ip->i_din1->di_db[0])
700 ffs_reallocblks_ufs2(ap)
701 struct vop_reallocblks_args /* {
703 struct cluster_save *a_buflist;
709 struct buf *sbp, *ebp;
710 ufs2_daddr_t *bap, *sbap, *ebap = 0;
711 struct cluster_save *buflist;
712 struct ufsmount *ump;
713 ufs_lbn_t start_lbn, end_lbn;
714 ufs2_daddr_t soff, newblk, blkno, pref;
715 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
716 int i, len, start_lvl, end_lvl, ssize;
722 if (fs->fs_contigsumsize <= 0)
724 buflist = ap->a_buflist;
725 len = buflist->bs_nchildren;
726 start_lbn = buflist->bs_children[0]->b_lblkno;
727 end_lbn = start_lbn + len - 1;
729 for (i = 0; i < len; i++)
730 if (!ffs_checkblk(ip,
731 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
732 panic("ffs_reallocblks: unallocated block 1");
733 for (i = 1; i < len; i++)
734 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
735 panic("ffs_reallocblks: non-logical cluster");
736 blkno = buflist->bs_children[0]->b_blkno;
737 ssize = fsbtodb(fs, fs->fs_frag);
738 for (i = 1; i < len - 1; i++)
739 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
740 panic("ffs_reallocblks: non-physical cluster %d", i);
743 * If the latest allocation is in a new cylinder group, assume that
744 * the filesystem has decided to move and do not force it back to
745 * the previous cylinder group.
747 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
748 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
750 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
751 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
754 * Get the starting offset and block map for the first block.
756 if (start_lvl == 0) {
757 sbap = &ip->i_din2->di_db[0];
760 idp = &start_ap[start_lvl - 1];
761 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
765 sbap = (ufs2_daddr_t *)sbp->b_data;
769 * If the block range spans two block maps, get the second map.
771 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
776 start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
777 panic("ffs_reallocblk: start == end");
779 ssize = len - (idp->in_off + 1);
780 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
782 ebap = (ufs2_daddr_t *)ebp->b_data;
785 * Find the preferred location for the cluster.
788 pref = ffs_blkpref_ufs2(ip, start_lbn, soff, sbap);
790 * Search the block map looking for an allocation of the desired size.
792 if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref,
793 len, ffs_clusteralloc)) == 0) {
798 * We have found a new contiguous block.
800 * First we have to replace the old block pointers with the new
801 * block pointers in the inode and indirect blocks associated
806 printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
807 (intmax_t)start_lbn, (intmax_t)end_lbn);
810 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
816 if (!ffs_checkblk(ip,
817 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
818 panic("ffs_reallocblks: unallocated block 2");
819 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
820 panic("ffs_reallocblks: alloc mismatch");
824 printf(" %jd,", (intmax_t)*bap);
826 if (DOINGSOFTDEP(vp)) {
827 if (sbap == &ip->i_din2->di_db[0] && i < ssize)
828 softdep_setup_allocdirect(ip, start_lbn + i,
829 blkno, *bap, fs->fs_bsize, fs->fs_bsize,
830 buflist->bs_children[i]);
832 softdep_setup_allocindir_page(ip, start_lbn + i,
833 i < ssize ? sbp : ebp, soff + i, blkno,
834 *bap, buflist->bs_children[i]);
839 * Next we must write out the modified inode and indirect blocks.
840 * For strict correctness, the writes should be synchronous since
841 * the old block values may have been written to disk. In practise
842 * they are almost never written, but if we are concerned about
843 * strict correctness, the `doasyncfree' flag should be set to zero.
845 * The test on `doasyncfree' should be changed to test a flag
846 * that shows whether the associated buffers and inodes have
847 * been written. The flag should be set when the cluster is
848 * started and cleared whenever the buffer or inode is flushed.
849 * We can then check below to see if it is set, and do the
850 * synchronous write only when it has been cleared.
852 if (sbap != &ip->i_din2->di_db[0]) {
858 ip->i_flag |= IN_CHANGE | IN_UPDATE;
869 * Last, free the old blocks and assign the new blocks to the buffers.
875 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
876 if (!DOINGSOFTDEP(vp))
877 ffs_blkfree(ump, fs, ip->i_devvp,
878 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
879 fs->fs_bsize, ip->i_number);
880 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
882 if (!ffs_checkblk(ip,
883 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
884 panic("ffs_reallocblks: unallocated block 3");
888 printf(" %jd,", (intmax_t)blkno);
902 if (sbap != &ip->i_din2->di_db[0])
908 * Allocate an inode in the filesystem.
910 * If allocating a directory, use ffs_dirpref to select the inode.
911 * If allocating in a directory, the following hierarchy is followed:
912 * 1) allocate the preferred inode.
913 * 2) allocate an inode in the same cylinder group.
914 * 3) quadradically rehash into other cylinder groups, until an
915 * available inode is located.
916 * If no inode preference is given the following hierarchy is used
917 * to allocate an inode:
918 * 1) allocate an inode in cylinder group 0.
919 * 2) quadradically rehash into other cylinder groups, until an
920 * available inode is located.
923 ffs_valloc(pvp, mode, cred, vpp)
933 struct ufsmount *ump;
937 static struct timeval lastfail;
946 if (fs->fs_cstotal.cs_nifree == 0)
949 if ((mode & IFMT) == IFDIR)
950 ipref = ffs_dirpref(pip);
952 ipref = pip->i_number;
953 if (ipref >= fs->fs_ncg * fs->fs_ipg)
955 cg = ino_to_cg(fs, ipref);
957 * Track number of dirs created one after another
958 * in a same cg without intervening by files.
960 if ((mode & IFMT) == IFDIR) {
961 if (fs->fs_contigdirs[cg] < 255)
962 fs->fs_contigdirs[cg]++;
964 if (fs->fs_contigdirs[cg] > 0)
965 fs->fs_contigdirs[cg]--;
967 ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode,
968 (allocfcn_t *)ffs_nodealloccg);
971 error = ffs_vget(pvp->v_mount, ino, LK_EXCLUSIVE, vpp);
973 error1 = ffs_vgetf(pvp->v_mount, ino, LK_EXCLUSIVE, vpp,
975 ffs_vfree(pvp, ino, mode);
980 ip->i_flag |= IN_MODIFIED;
988 printf("mode = 0%o, inum = %lu, fs = %s\n",
989 ip->i_mode, (u_long)ip->i_number, fs->fs_fsmnt);
990 panic("ffs_valloc: dup alloc");
992 if (DIP(ip, i_blocks) && (fs->fs_flags & FS_UNCLEAN) == 0) { /* XXX */
993 printf("free inode %s/%lu had %ld blocks\n",
994 fs->fs_fsmnt, (u_long)ino, (long)DIP(ip, i_blocks));
995 DIP_SET(ip, i_blocks, 0);
998 DIP_SET(ip, i_flags, 0);
1000 * Set up a new generation number for this inode.
1002 if (ip->i_gen == 0 || ++ip->i_gen == 0)
1003 ip->i_gen = arc4random() / 2 + 1;
1004 DIP_SET(ip, i_gen, ip->i_gen);
1005 if (fs->fs_magic == FS_UFS2_MAGIC) {
1007 ip->i_din2->di_birthtime = ts.tv_sec;
1008 ip->i_din2->di_birthnsec = ts.tv_nsec;
1011 vnode_destroy_vobject(*vpp);
1012 (*vpp)->v_type = VNON;
1013 if (fs->fs_magic == FS_UFS2_MAGIC)
1014 (*vpp)->v_op = &ffs_vnodeops2;
1016 (*vpp)->v_op = &ffs_vnodeops1;
1020 if (ppsratecheck(&lastfail, &curfail, 1)) {
1021 ffs_fserr(fs, pip->i_number, "out of inodes");
1022 uprintf("\n%s: create/symlink failed, no inodes free\n",
1029 * Find a cylinder group to place a directory.
1031 * The policy implemented by this algorithm is to allocate a
1032 * directory inode in the same cylinder group as its parent
1033 * directory, but also to reserve space for its files inodes
1034 * and data. Restrict the number of directories which may be
1035 * allocated one after another in the same cylinder group
1036 * without intervening allocation of files.
1038 * If we allocate a first level directory then force allocation
1039 * in another cylinder group.
1046 u_int cg, prefcg, dirsize, cgsize;
1047 u_int avgifree, avgbfree, avgndir, curdirsize;
1048 u_int minifree, minbfree, maxndir;
1049 u_int mincg, minndir;
1050 u_int maxcontigdirs;
1052 mtx_assert(UFS_MTX(pip->i_ump), MA_OWNED);
1055 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
1056 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1057 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
1060 * Force allocation in another cg if creating a first level dir.
1062 ASSERT_VOP_LOCKED(ITOV(pip), "ffs_dirpref");
1063 if (ITOV(pip)->v_vflag & VV_ROOT) {
1064 prefcg = arc4random() % fs->fs_ncg;
1066 minndir = fs->fs_ipg;
1067 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1068 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1069 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1070 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1072 minndir = fs->fs_cs(fs, cg).cs_ndir;
1074 for (cg = 0; cg < prefcg; cg++)
1075 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1076 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1077 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1079 minndir = fs->fs_cs(fs, cg).cs_ndir;
1081 return ((ino_t)(fs->fs_ipg * mincg));
1085 * Count various limits which used for
1086 * optimal allocation of a directory inode.
1088 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
1089 minifree = avgifree - avgifree / 4;
1092 minbfree = avgbfree - avgbfree / 4;
1095 cgsize = fs->fs_fsize * fs->fs_fpg;
1096 dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
1097 curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
1098 if (dirsize < curdirsize)
1099 dirsize = curdirsize;
1101 maxcontigdirs = 0; /* dirsize overflowed */
1103 maxcontigdirs = min((avgbfree * fs->fs_bsize) / dirsize, 255);
1104 if (fs->fs_avgfpdir > 0)
1105 maxcontigdirs = min(maxcontigdirs,
1106 fs->fs_ipg / fs->fs_avgfpdir);
1107 if (maxcontigdirs == 0)
1111 * Limit number of dirs in one cg and reserve space for
1112 * regular files, but only if we have no deficit in
1115 prefcg = ino_to_cg(fs, pip->i_number);
1116 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1117 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1118 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1119 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1120 if (fs->fs_contigdirs[cg] < maxcontigdirs)
1121 return ((ino_t)(fs->fs_ipg * cg));
1123 for (cg = 0; cg < prefcg; cg++)
1124 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1125 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1126 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1127 if (fs->fs_contigdirs[cg] < maxcontigdirs)
1128 return ((ino_t)(fs->fs_ipg * cg));
1131 * This is a backstop when we have deficit in space.
1133 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1134 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1135 return ((ino_t)(fs->fs_ipg * cg));
1136 for (cg = 0; cg < prefcg; cg++)
1137 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1139 return ((ino_t)(fs->fs_ipg * cg));
1143 * Select the desired position for the next block in a file. The file is
1144 * logically divided into sections. The first section is composed of the
1145 * direct blocks. Each additional section contains fs_maxbpg blocks.
1147 * If no blocks have been allocated in the first section, the policy is to
1148 * request a block in the same cylinder group as the inode that describes
1149 * the file. If no blocks have been allocated in any other section, the
1150 * policy is to place the section in a cylinder group with a greater than
1151 * average number of free blocks. An appropriate cylinder group is found
1152 * by using a rotor that sweeps the cylinder groups. When a new group of
1153 * blocks is needed, the sweep begins in the cylinder group following the
1154 * cylinder group from which the previous allocation was made. The sweep
1155 * continues until a cylinder group with greater than the average number
1156 * of free blocks is found. If the allocation is for the first block in an
1157 * indirect block, the information on the previous allocation is unavailable;
1158 * here a best guess is made based upon the logical block number being
1161 * If a section is already partially allocated, the policy is to
1162 * contiguously allocate fs_maxcontig blocks. The end of one of these
1163 * contiguous blocks and the beginning of the next is laid out
1164 * contiguously if possible.
1167 ffs_blkpref_ufs1(ip, lbn, indx, bap)
1175 u_int avgbfree, startcg;
1177 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1179 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1180 if (lbn < NDADDR + NINDIR(fs)) {
1181 cg = ino_to_cg(fs, ip->i_number);
1182 return (cgbase(fs, cg) + fs->fs_frag);
1185 * Find a cylinder with greater than average number of
1186 * unused data blocks.
1188 if (indx == 0 || bap[indx - 1] == 0)
1190 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1192 startcg = dtog(fs, bap[indx - 1]) + 1;
1193 startcg %= fs->fs_ncg;
1194 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1195 for (cg = startcg; cg < fs->fs_ncg; cg++)
1196 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1197 fs->fs_cgrotor = cg;
1198 return (cgbase(fs, cg) + fs->fs_frag);
1200 for (cg = 0; cg <= startcg; cg++)
1201 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1202 fs->fs_cgrotor = cg;
1203 return (cgbase(fs, cg) + fs->fs_frag);
1208 * We just always try to lay things out contiguously.
1210 return (bap[indx - 1] + fs->fs_frag);
1214 * Same as above, but for UFS2
1217 ffs_blkpref_ufs2(ip, lbn, indx, bap)
1225 u_int avgbfree, startcg;
1227 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1229 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1230 if (lbn < NDADDR + NINDIR(fs)) {
1231 cg = ino_to_cg(fs, ip->i_number);
1232 return (cgbase(fs, cg) + fs->fs_frag);
1235 * Find a cylinder with greater than average number of
1236 * unused data blocks.
1238 if (indx == 0 || bap[indx - 1] == 0)
1240 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1242 startcg = dtog(fs, bap[indx - 1]) + 1;
1243 startcg %= fs->fs_ncg;
1244 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1245 for (cg = startcg; cg < fs->fs_ncg; cg++)
1246 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1247 fs->fs_cgrotor = cg;
1248 return (cgbase(fs, cg) + fs->fs_frag);
1250 for (cg = 0; cg <= startcg; cg++)
1251 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1252 fs->fs_cgrotor = cg;
1253 return (cgbase(fs, cg) + fs->fs_frag);
1258 * We just always try to lay things out contiguously.
1260 return (bap[indx - 1] + fs->fs_frag);
1264 * Implement the cylinder overflow algorithm.
1266 * The policy implemented by this algorithm is:
1267 * 1) allocate the block in its requested cylinder group.
1268 * 2) quadradically rehash on the cylinder group number.
1269 * 3) brute force search for a free block.
1271 * Must be called with the UFS lock held. Will release the lock on success
1272 * and return with it held on failure.
1276 ffs_hashalloc(ip, cg, pref, size, allocator)
1280 int size; /* size for data blocks, mode for inodes */
1281 allocfcn_t *allocator;
1284 ufs2_daddr_t result;
1287 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1289 if (ITOV(ip)->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
1290 panic("ffs_hashalloc: allocation on suspended filesystem");
1294 * 1: preferred cylinder group
1296 result = (*allocator)(ip, cg, pref, size);
1300 * 2: quadratic rehash
1302 for (i = 1; i < fs->fs_ncg; i *= 2) {
1304 if (cg >= fs->fs_ncg)
1306 result = (*allocator)(ip, cg, 0, size);
1311 * 3: brute force search
1312 * Note that we start at i == 2, since 0 was checked initially,
1313 * and 1 is always checked in the quadratic rehash.
1315 cg = (icg + 2) % fs->fs_ncg;
1316 for (i = 2; i < fs->fs_ncg; i++) {
1317 result = (*allocator)(ip, cg, 0, size);
1321 if (cg == fs->fs_ncg)
1328 * Determine whether a fragment can be extended.
1330 * Check to see if the necessary fragments are available, and
1331 * if they are, allocate them.
1334 ffs_fragextend(ip, cg, bprev, osize, nsize)
1343 struct ufsmount *ump;
1352 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
1354 frags = numfrags(fs, nsize);
1355 bbase = fragnum(fs, bprev);
1356 if (bbase > fragnum(fs, (bprev + frags - 1))) {
1357 /* cannot extend across a block boundary */
1361 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1362 (int)fs->fs_cgsize, NOCRED, &bp);
1365 cgp = (struct cg *)bp->b_data;
1366 if (!cg_chkmagic(cgp))
1368 bp->b_xflags |= BX_BKGRDWRITE;
1369 cgp->cg_old_time = cgp->cg_time = time_second;
1370 bno = dtogd(fs, bprev);
1371 blksfree = cg_blksfree(cgp);
1372 for (i = numfrags(fs, osize); i < frags; i++)
1373 if (isclr(blksfree, bno + i))
1376 * the current fragment can be extended
1377 * deduct the count on fragment being extended into
1378 * increase the count on the remaining fragment (if any)
1379 * allocate the extended piece
1381 for (i = frags; i < fs->fs_frag - bbase; i++)
1382 if (isclr(blksfree, bno + i))
1384 cgp->cg_frsum[i - numfrags(fs, osize)]--;
1386 cgp->cg_frsum[i - frags]++;
1387 for (i = numfrags(fs, osize), nffree = 0; i < frags; i++) {
1388 clrbit(blksfree, bno + i);
1389 cgp->cg_cs.cs_nffree--;
1393 fs->fs_cstotal.cs_nffree -= nffree;
1394 fs->fs_cs(fs, cg).cs_nffree -= nffree;
1396 ACTIVECLEAR(fs, cg);
1398 if (DOINGSOFTDEP(ITOV(ip)))
1399 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), bprev);
1411 * Determine whether a block can be allocated.
1413 * Check to see if a block of the appropriate size is available,
1414 * and if it is, allocate it.
1417 ffs_alloccg(ip, cg, bpref, size)
1426 struct ufsmount *ump;
1429 int i, allocsiz, error, frags;
1434 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1437 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1438 (int)fs->fs_cgsize, NOCRED, &bp);
1441 cgp = (struct cg *)bp->b_data;
1442 if (!cg_chkmagic(cgp) ||
1443 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize))
1445 bp->b_xflags |= BX_BKGRDWRITE;
1446 cgp->cg_old_time = cgp->cg_time = time_second;
1447 if (size == fs->fs_bsize) {
1449 blkno = ffs_alloccgblk(ip, bp, bpref);
1450 ACTIVECLEAR(fs, cg);
1456 * check to see if any fragments are already available
1457 * allocsiz is the size which will be allocated, hacking
1458 * it down to a smaller size if necessary
1460 blksfree = cg_blksfree(cgp);
1461 frags = numfrags(fs, size);
1462 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1463 if (cgp->cg_frsum[allocsiz] != 0)
1465 if (allocsiz == fs->fs_frag) {
1467 * no fragments were available, so a block will be
1468 * allocated, and hacked up
1470 if (cgp->cg_cs.cs_nbfree == 0)
1473 blkno = ffs_alloccgblk(ip, bp, bpref);
1474 bno = dtogd(fs, blkno);
1475 for (i = frags; i < fs->fs_frag; i++)
1476 setbit(blksfree, bno + i);
1477 i = fs->fs_frag - frags;
1478 cgp->cg_cs.cs_nffree += i;
1479 fs->fs_cstotal.cs_nffree += i;
1480 fs->fs_cs(fs, cg).cs_nffree += i;
1483 ACTIVECLEAR(fs, cg);
1488 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1491 for (i = 0; i < frags; i++)
1492 clrbit(blksfree, bno + i);
1493 cgp->cg_cs.cs_nffree -= frags;
1494 cgp->cg_frsum[allocsiz]--;
1495 if (frags != allocsiz)
1496 cgp->cg_frsum[allocsiz - frags]++;
1498 fs->fs_cstotal.cs_nffree -= frags;
1499 fs->fs_cs(fs, cg).cs_nffree -= frags;
1501 blkno = cgbase(fs, cg) + bno;
1502 ACTIVECLEAR(fs, cg);
1504 if (DOINGSOFTDEP(ITOV(ip)))
1505 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno);
1516 * Allocate a block in a cylinder group.
1518 * This algorithm implements the following policy:
1519 * 1) allocate the requested block.
1520 * 2) allocate a rotationally optimal block in the same cylinder.
1521 * 3) allocate the next available block on the block rotor for the
1522 * specified cylinder group.
1523 * Note that this routine only allocates fs_bsize blocks; these
1524 * blocks may be fragmented by the routine that allocates them.
1527 ffs_alloccgblk(ip, bp, bpref)
1534 struct ufsmount *ump;
1541 mtx_assert(UFS_MTX(ump), MA_OWNED);
1542 cgp = (struct cg *)bp->b_data;
1543 blksfree = cg_blksfree(cgp);
1544 if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
1545 bpref = cgp->cg_rotor;
1547 bpref = blknum(fs, bpref);
1548 bno = dtogd(fs, bpref);
1550 * if the requested block is available, use it
1552 if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
1556 * Take the next available block in this cylinder group.
1558 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1561 cgp->cg_rotor = bno;
1563 blkno = fragstoblks(fs, bno);
1564 ffs_clrblock(fs, blksfree, (long)blkno);
1565 ffs_clusteracct(ump, fs, cgp, blkno, -1);
1566 cgp->cg_cs.cs_nbfree--;
1567 fs->fs_cstotal.cs_nbfree--;
1568 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1570 blkno = cgbase(fs, cgp->cg_cgx) + bno;
1573 if (DOINGSOFTDEP(ITOV(ip)))
1574 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno);
1580 * Determine whether a cluster can be allocated.
1582 * We do not currently check for optimal rotational layout if there
1583 * are multiple choices in the same cylinder group. Instead we just
1584 * take the first one that we find following bpref.
1587 ffs_clusteralloc(ip, cg, bpref, len)
1596 struct ufsmount *ump;
1597 int i, run, bit, map, got;
1605 if (fs->fs_maxcluster[cg] < len)
1608 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1611 cgp = (struct cg *)bp->b_data;
1612 if (!cg_chkmagic(cgp))
1614 bp->b_xflags |= BX_BKGRDWRITE;
1616 * Check to see if a cluster of the needed size (or bigger) is
1617 * available in this cylinder group.
1619 lp = &cg_clustersum(cgp)[len];
1620 for (i = len; i <= fs->fs_contigsumsize; i++)
1623 if (i > fs->fs_contigsumsize) {
1625 * This is the first time looking for a cluster in this
1626 * cylinder group. Update the cluster summary information
1627 * to reflect the true maximum sized cluster so that
1628 * future cluster allocation requests can avoid reading
1629 * the cylinder group map only to find no clusters.
1631 lp = &cg_clustersum(cgp)[len - 1];
1632 for (i = len - 1; i > 0; i--)
1636 fs->fs_maxcluster[cg] = i;
1640 * Search the cluster map to find a big enough cluster.
1641 * We take the first one that we find, even if it is larger
1642 * than we need as we prefer to get one close to the previous
1643 * block allocation. We do not search before the current
1644 * preference point as we do not want to allocate a block
1645 * that is allocated before the previous one (as we will
1646 * then have to wait for another pass of the elevator
1647 * algorithm before it will be read). We prefer to fail and
1648 * be recalled to try an allocation in the next cylinder group.
1650 if (dtog(fs, bpref) != cg)
1653 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1654 mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1656 bit = 1 << (bpref % NBBY);
1657 for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
1658 if ((map & bit) == 0) {
1665 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1672 if (got >= cgp->cg_nclusterblks)
1675 * Allocate the cluster that we have found.
1677 blksfree = cg_blksfree(cgp);
1678 for (i = 1; i <= len; i++)
1679 if (!ffs_isblock(fs, blksfree, got - run + i))
1680 panic("ffs_clusteralloc: map mismatch");
1681 bno = cgbase(fs, cg) + blkstofrags(fs, got - run + 1);
1682 if (dtog(fs, bno) != cg)
1683 panic("ffs_clusteralloc: allocated out of group");
1684 len = blkstofrags(fs, len);
1686 for (i = 0; i < len; i += fs->fs_frag)
1687 if (ffs_alloccgblk(ip, bp, bno + i) != bno + i)
1688 panic("ffs_clusteralloc: lost block");
1689 ACTIVECLEAR(fs, cg);
1702 * Determine whether an inode can be allocated.
1704 * Check to see if an inode is available, and if it is,
1705 * allocate it using the following policy:
1706 * 1) allocate the requested inode.
1707 * 2) allocate the next available inode after the requested
1708 * inode in the specified cylinder group.
1711 ffs_nodealloccg(ip, cg, ipref, mode)
1719 struct buf *bp, *ibp;
1720 struct ufsmount *ump;
1722 struct ufs2_dinode *dp2;
1723 int error, start, len, loc, map, i;
1727 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1730 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1731 (int)fs->fs_cgsize, NOCRED, &bp);
1737 cgp = (struct cg *)bp->b_data;
1738 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
1743 bp->b_xflags |= BX_BKGRDWRITE;
1744 cgp->cg_old_time = cgp->cg_time = time_second;
1745 inosused = cg_inosused(cgp);
1747 ipref %= fs->fs_ipg;
1748 if (isclr(inosused, ipref))
1751 start = cgp->cg_irotor / NBBY;
1752 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
1753 loc = skpc(0xff, len, &inosused[start]);
1757 loc = skpc(0xff, len, &inosused[0]);
1759 printf("cg = %d, irotor = %ld, fs = %s\n",
1760 cg, (long)cgp->cg_irotor, fs->fs_fsmnt);
1761 panic("ffs_nodealloccg: map corrupted");
1765 i = start + len - loc;
1768 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1769 if ((map & i) == 0) {
1770 cgp->cg_irotor = ipref;
1774 printf("fs = %s\n", fs->fs_fsmnt);
1775 panic("ffs_nodealloccg: block not in map");
1779 * Check to see if we need to initialize more inodes.
1782 if (fs->fs_magic == FS_UFS2_MAGIC &&
1783 ipref + INOPB(fs) > cgp->cg_initediblk &&
1784 cgp->cg_initediblk < cgp->cg_niblk) {
1785 ibp = getblk(ip->i_devvp, fsbtodb(fs,
1786 ino_to_fsba(fs, cg * fs->fs_ipg + cgp->cg_initediblk)),
1787 (int)fs->fs_bsize, 0, 0, 0);
1788 bzero(ibp->b_data, (int)fs->fs_bsize);
1789 dp2 = (struct ufs2_dinode *)(ibp->b_data);
1790 for (i = 0; i < INOPB(fs); i++) {
1791 dp2->di_gen = arc4random() / 2 + 1;
1794 cgp->cg_initediblk += INOPB(fs);
1797 ACTIVECLEAR(fs, cg);
1798 setbit(inosused, ipref);
1799 cgp->cg_cs.cs_nifree--;
1800 fs->fs_cstotal.cs_nifree--;
1801 fs->fs_cs(fs, cg).cs_nifree--;
1803 if ((mode & IFMT) == IFDIR) {
1804 cgp->cg_cs.cs_ndir++;
1805 fs->fs_cstotal.cs_ndir++;
1806 fs->fs_cs(fs, cg).cs_ndir++;
1809 if (DOINGSOFTDEP(ITOV(ip)))
1810 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1814 return ((ino_t)(cg * fs->fs_ipg + ipref));
1818 * check if a block is free
1821 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
1824 switch ((int)fs->fs_frag) {
1826 return (cp[h] == 0);
1828 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
1830 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
1832 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
1834 panic("ffs_isfreeblock");
1840 * Free a block or fragment.
1842 * The specified block or fragment is placed back in the
1843 * free map. If a fragment is deallocated, a possible
1844 * block reassembly is checked.
1847 ffs_blkfree(ump, fs, devvp, bno, size, inum)
1848 struct ufsmount *ump;
1850 struct vnode *devvp;
1857 ufs1_daddr_t fragno, cgbno;
1858 ufs2_daddr_t cgblkno;
1859 int i, blk, frags, bbase;
1865 if (devvp->v_type == VREG) {
1866 /* devvp is a snapshot */
1867 dev = VTOI(devvp)->i_devvp->v_rdev;
1868 cgblkno = fragstoblks(fs, cgtod(fs, cg));
1870 /* devvp is a normal disk device */
1871 dev = devvp->v_rdev;
1872 cgblkno = fsbtodb(fs, cgtod(fs, cg));
1873 ASSERT_VOP_LOCKED(devvp, "ffs_blkfree");
1874 if ((devvp->v_vflag & VV_COPYONWRITE) &&
1875 ffs_snapblkfree(fs, devvp, bno, size, inum))
1879 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1880 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1881 printf("dev=%s, bno = %jd, bsize = %ld, size = %ld, fs = %s\n",
1882 devtoname(dev), (intmax_t)bno, (long)fs->fs_bsize,
1883 size, fs->fs_fsmnt);
1884 panic("ffs_blkfree: bad size");
1887 if ((u_int)bno >= fs->fs_size) {
1888 printf("bad block %jd, ino %lu\n", (intmax_t)bno,
1890 ffs_fserr(fs, inum, "bad block");
1893 if (bread(devvp, cgblkno, (int)fs->fs_cgsize, NOCRED, &bp)) {
1897 cgp = (struct cg *)bp->b_data;
1898 if (!cg_chkmagic(cgp)) {
1902 bp->b_xflags |= BX_BKGRDWRITE;
1903 cgp->cg_old_time = cgp->cg_time = time_second;
1904 cgbno = dtogd(fs, bno);
1905 blksfree = cg_blksfree(cgp);
1907 if (size == fs->fs_bsize) {
1908 fragno = fragstoblks(fs, cgbno);
1909 if (!ffs_isfreeblock(fs, blksfree, fragno)) {
1910 if (devvp->v_type == VREG) {
1912 /* devvp is a snapshot */
1916 printf("dev = %s, block = %jd, fs = %s\n",
1917 devtoname(dev), (intmax_t)bno, fs->fs_fsmnt);
1918 panic("ffs_blkfree: freeing free block");
1920 ffs_setblock(fs, blksfree, fragno);
1921 ffs_clusteracct(ump, fs, cgp, fragno, 1);
1922 cgp->cg_cs.cs_nbfree++;
1923 fs->fs_cstotal.cs_nbfree++;
1924 fs->fs_cs(fs, cg).cs_nbfree++;
1926 bbase = cgbno - fragnum(fs, cgbno);
1928 * decrement the counts associated with the old frags
1930 blk = blkmap(fs, blksfree, bbase);
1931 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
1933 * deallocate the fragment
1935 frags = numfrags(fs, size);
1936 for (i = 0; i < frags; i++) {
1937 if (isset(blksfree, cgbno + i)) {
1938 printf("dev = %s, block = %jd, fs = %s\n",
1939 devtoname(dev), (intmax_t)(bno + i),
1941 panic("ffs_blkfree: freeing free frag");
1943 setbit(blksfree, cgbno + i);
1945 cgp->cg_cs.cs_nffree += i;
1946 fs->fs_cstotal.cs_nffree += i;
1947 fs->fs_cs(fs, cg).cs_nffree += i;
1949 * add back in counts associated with the new frags
1951 blk = blkmap(fs, blksfree, bbase);
1952 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
1954 * if a complete block has been reassembled, account for it
1956 fragno = fragstoblks(fs, bbase);
1957 if (ffs_isblock(fs, blksfree, fragno)) {
1958 cgp->cg_cs.cs_nffree -= fs->fs_frag;
1959 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1960 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1961 ffs_clusteracct(ump, fs, cgp, fragno, 1);
1962 cgp->cg_cs.cs_nbfree++;
1963 fs->fs_cstotal.cs_nbfree++;
1964 fs->fs_cs(fs, cg).cs_nbfree++;
1968 ACTIVECLEAR(fs, cg);
1975 * Verify allocation of a block or fragment. Returns true if block or
1976 * fragment is allocated, false if it is free.
1979 ffs_checkblk(ip, bno, size)
1988 int i, error, frags, free;
1992 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1993 printf("bsize = %ld, size = %ld, fs = %s\n",
1994 (long)fs->fs_bsize, size, fs->fs_fsmnt);
1995 panic("ffs_checkblk: bad size");
1997 if ((u_int)bno >= fs->fs_size)
1998 panic("ffs_checkblk: bad block %jd", (intmax_t)bno);
1999 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
2000 (int)fs->fs_cgsize, NOCRED, &bp);
2002 panic("ffs_checkblk: cg bread failed");
2003 cgp = (struct cg *)bp->b_data;
2004 if (!cg_chkmagic(cgp))
2005 panic("ffs_checkblk: cg magic mismatch");
2006 bp->b_xflags |= BX_BKGRDWRITE;
2007 blksfree = cg_blksfree(cgp);
2008 cgbno = dtogd(fs, bno);
2009 if (size == fs->fs_bsize) {
2010 free = ffs_isblock(fs, blksfree, fragstoblks(fs, cgbno));
2012 frags = numfrags(fs, size);
2013 for (free = 0, i = 0; i < frags; i++)
2014 if (isset(blksfree, cgbno + i))
2016 if (free != 0 && free != frags)
2017 panic("ffs_checkblk: partially free fragment");
2022 #endif /* INVARIANTS */
2028 ffs_vfree(pvp, ino, mode)
2035 if (DOINGSOFTDEP(pvp)) {
2036 softdep_freefile(pvp, ino, mode);
2040 return (ffs_freefile(ip->i_ump, ip->i_fs, ip->i_devvp, ino, mode));
2044 * Do the actual free operation.
2045 * The specified inode is placed back in the free map.
2048 ffs_freefile(ump, fs, devvp, ino, mode)
2049 struct ufsmount *ump;
2051 struct vnode *devvp;
2063 cg = ino_to_cg(fs, ino);
2064 if (devvp->v_type == VREG) {
2065 /* devvp is a snapshot */
2066 dev = VTOI(devvp)->i_devvp->v_rdev;
2067 cgbno = fragstoblks(fs, cgtod(fs, cg));
2069 /* devvp is a normal disk device */
2070 dev = devvp->v_rdev;
2071 cgbno = fsbtodb(fs, cgtod(fs, cg));
2073 if (ino >= fs->fs_ipg * fs->fs_ncg)
2074 panic("ffs_freefile: range: dev = %s, ino = %lu, fs = %s",
2075 devtoname(dev), (u_long)ino, fs->fs_fsmnt);
2076 if ((error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp))) {
2080 cgp = (struct cg *)bp->b_data;
2081 if (!cg_chkmagic(cgp)) {
2085 bp->b_xflags |= BX_BKGRDWRITE;
2086 cgp->cg_old_time = cgp->cg_time = time_second;
2087 inosused = cg_inosused(cgp);
2089 if (isclr(inosused, ino)) {
2090 printf("dev = %s, ino = %u, fs = %s\n", devtoname(dev),
2091 ino + cg * fs->fs_ipg, fs->fs_fsmnt);
2092 if (fs->fs_ronly == 0)
2093 panic("ffs_freefile: freeing free inode");
2095 clrbit(inosused, ino);
2096 if (ino < cgp->cg_irotor)
2097 cgp->cg_irotor = ino;
2098 cgp->cg_cs.cs_nifree++;
2100 fs->fs_cstotal.cs_nifree++;
2101 fs->fs_cs(fs, cg).cs_nifree++;
2102 if ((mode & IFMT) == IFDIR) {
2103 cgp->cg_cs.cs_ndir--;
2104 fs->fs_cstotal.cs_ndir--;
2105 fs->fs_cs(fs, cg).cs_ndir--;
2108 ACTIVECLEAR(fs, cg);
2115 * Check to see if a file is free.
2118 ffs_checkfreefile(fs, devvp, ino)
2120 struct vnode *devvp;
2130 cg = ino_to_cg(fs, ino);
2131 if (devvp->v_type == VREG) {
2132 /* devvp is a snapshot */
2133 cgbno = fragstoblks(fs, cgtod(fs, cg));
2135 /* devvp is a normal disk device */
2136 cgbno = fsbtodb(fs, cgtod(fs, cg));
2138 if (ino >= fs->fs_ipg * fs->fs_ncg)
2140 if (bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp)) {
2144 cgp = (struct cg *)bp->b_data;
2145 if (!cg_chkmagic(cgp)) {
2149 inosused = cg_inosused(cgp);
2151 ret = isclr(inosused, ino);
2157 * Find a block of the specified size in the specified cylinder group.
2159 * It is a panic if a request is made to find a block if none are
2163 ffs_mapsearch(fs, cgp, bpref, allocsiz)
2170 int start, len, loc, i;
2171 int blk, field, subfield, pos;
2175 * find the fragment by searching through the free block
2176 * map for an appropriate bit pattern
2179 start = dtogd(fs, bpref) / NBBY;
2181 start = cgp->cg_frotor / NBBY;
2182 blksfree = cg_blksfree(cgp);
2183 len = howmany(fs->fs_fpg, NBBY) - start;
2184 loc = scanc((u_int)len, (u_char *)&blksfree[start],
2185 fragtbl[fs->fs_frag],
2186 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2190 loc = scanc((u_int)len, (u_char *)&blksfree[0],
2191 fragtbl[fs->fs_frag],
2192 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2194 printf("start = %d, len = %d, fs = %s\n",
2195 start, len, fs->fs_fsmnt);
2196 panic("ffs_alloccg: map corrupted");
2200 bno = (start + len - loc) * NBBY;
2201 cgp->cg_frotor = bno;
2203 * found the byte in the map
2204 * sift through the bits to find the selected frag
2206 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
2207 blk = blkmap(fs, blksfree, bno);
2209 field = around[allocsiz];
2210 subfield = inside[allocsiz];
2211 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
2212 if ((blk & field) == subfield)
2218 printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt);
2219 panic("ffs_alloccg: block not in map");
2224 * Update the cluster map because of an allocation or free.
2226 * Cnt == 1 means free; cnt == -1 means allocating.
2229 ffs_clusteracct(ump, fs, cgp, blkno, cnt)
2230 struct ufsmount *ump;
2238 u_char *freemapp, *mapp;
2239 int i, start, end, forw, back, map, bit;
2241 mtx_assert(UFS_MTX(ump), MA_OWNED);
2243 if (fs->fs_contigsumsize <= 0)
2245 freemapp = cg_clustersfree(cgp);
2246 sump = cg_clustersum(cgp);
2248 * Allocate or clear the actual block.
2251 setbit(freemapp, blkno);
2253 clrbit(freemapp, blkno);
2255 * Find the size of the cluster going forward.
2258 end = start + fs->fs_contigsumsize;
2259 if (end >= cgp->cg_nclusterblks)
2260 end = cgp->cg_nclusterblks;
2261 mapp = &freemapp[start / NBBY];
2263 bit = 1 << (start % NBBY);
2264 for (i = start; i < end; i++) {
2265 if ((map & bit) == 0)
2267 if ((i & (NBBY - 1)) != (NBBY - 1)) {
2276 * Find the size of the cluster going backward.
2279 end = start - fs->fs_contigsumsize;
2282 mapp = &freemapp[start / NBBY];
2284 bit = 1 << (start % NBBY);
2285 for (i = start; i > end; i--) {
2286 if ((map & bit) == 0)
2288 if ((i & (NBBY - 1)) != 0) {
2292 bit = 1 << (NBBY - 1);
2297 * Account for old cluster and the possibly new forward and
2300 i = back + forw + 1;
2301 if (i > fs->fs_contigsumsize)
2302 i = fs->fs_contigsumsize;
2309 * Update cluster summary information.
2311 lp = &sump[fs->fs_contigsumsize];
2312 for (i = fs->fs_contigsumsize; i > 0; i--)
2315 fs->fs_maxcluster[cgp->cg_cgx] = i;
2319 * Fserr prints the name of a filesystem with an error diagnostic.
2321 * The form of the error message is:
2325 ffs_fserr(fs, inum, cp)
2330 struct thread *td = curthread; /* XXX */
2331 struct proc *p = td->td_proc;
2333 log(LOG_ERR, "pid %d (%s), uid %d inumber %d on %s: %s\n",
2334 p->p_pid, p->p_comm, td->td_ucred->cr_uid, inum, fs->fs_fsmnt, cp);
2338 * This function provides the capability for the fsck program to
2339 * update an active filesystem. Eleven operations are provided:
2341 * adjrefcnt(inode, amt) - adjusts the reference count on the
2342 * specified inode by the specified amount. Under normal
2343 * operation the count should always go down. Decrementing
2344 * the count to zero will cause the inode to be freed.
2345 * adjblkcnt(inode, amt) - adjust the number of blocks used to
2346 * by the specifed amount.
2347 * adjndir, adjbfree, adjifree, adjffree, adjnumclusters(amt) -
2348 * adjust the superblock summary.
2349 * freedirs(inode, count) - directory inodes [inode..inode + count - 1]
2350 * are marked as free. Inodes should never have to be marked
2352 * freefiles(inode, count) - file inodes [inode..inode + count - 1]
2353 * are marked as free. Inodes should never have to be marked
2355 * freeblks(blockno, size) - blocks [blockno..blockno + size - 1]
2356 * are marked as free. Blocks should never have to be marked
2358 * setflags(flags, set/clear) - the fs_flags field has the specified
2359 * flags set (second parameter +1) or cleared (second parameter -1).
2362 static int sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS);
2364 SYSCTL_PROC(_vfs_ffs, FFS_ADJ_REFCNT, adjrefcnt, CTLFLAG_WR|CTLTYPE_STRUCT,
2365 0, 0, sysctl_ffs_fsck, "S,fsck", "Adjust Inode Reference Count");
2367 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_BLKCNT, adjblkcnt, CTLFLAG_WR,
2368 sysctl_ffs_fsck, "Adjust Inode Used Blocks Count");
2370 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NDIR, adjndir, CTLFLAG_WR,
2371 sysctl_ffs_fsck, "Adjust number of directories");
2373 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NBFREE, adjnbfree, CTLFLAG_WR,
2374 sysctl_ffs_fsck, "Adjust number of free blocks");
2376 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NIFREE, adjnifree, CTLFLAG_WR,
2377 sysctl_ffs_fsck, "Adjust number of free inodes");
2379 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NFFREE, adjnffree, CTLFLAG_WR,
2380 sysctl_ffs_fsck, "Adjust number of free frags");
2382 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NUMCLUSTERS, adjnumclusters, CTLFLAG_WR,
2383 sysctl_ffs_fsck, "Adjust number of free clusters");
2385 static SYSCTL_NODE(_vfs_ffs, FFS_DIR_FREE, freedirs, CTLFLAG_WR,
2386 sysctl_ffs_fsck, "Free Range of Directory Inodes");
2388 static SYSCTL_NODE(_vfs_ffs, FFS_FILE_FREE, freefiles, CTLFLAG_WR,
2389 sysctl_ffs_fsck, "Free Range of File Inodes");
2391 static SYSCTL_NODE(_vfs_ffs, FFS_BLK_FREE, freeblks, CTLFLAG_WR,
2392 sysctl_ffs_fsck, "Free Range of Blocks");
2394 static SYSCTL_NODE(_vfs_ffs, FFS_SET_FLAGS, setflags, CTLFLAG_WR,
2395 sysctl_ffs_fsck, "Change Filesystem Flags");
2398 static int fsckcmds = 0;
2399 SYSCTL_INT(_debug, OID_AUTO, fsckcmds, CTLFLAG_RW, &fsckcmds, 0, "");
2403 sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS)
2405 struct fsck_cmd cmd;
2406 struct ufsmount *ump;
2412 long blkcnt, blksize;
2414 int filetype, error;
2416 if (req->newlen > sizeof cmd)
2418 if ((error = SYSCTL_IN(req, &cmd, sizeof cmd)) != 0)
2420 if (cmd.version != FFS_CMD_VERSION)
2421 return (ERPCMISMATCH);
2422 if ((error = getvnode(curproc->p_fd, cmd.handle, &fp)) != 0)
2424 vn_start_write(fp->f_data, &mp, V_WAIT);
2425 if (mp == 0 || strncmp(mp->mnt_stat.f_fstypename, "ufs", MFSNAMELEN)) {
2426 vn_finished_write(mp);
2427 fdrop(fp, curthread);
2430 if (mp->mnt_flag & MNT_RDONLY) {
2431 vn_finished_write(mp);
2432 fdrop(fp, curthread);
2439 switch (oidp->oid_number) {
2444 printf("%s: %s flags\n", mp->mnt_stat.f_mntonname,
2445 cmd.size > 0 ? "set" : "clear");
2448 fs->fs_flags |= (long)cmd.value;
2450 fs->fs_flags &= ~(long)cmd.value;
2453 case FFS_ADJ_REFCNT:
2456 printf("%s: adjust inode %jd count by %jd\n",
2457 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2458 (intmax_t)cmd.size);
2461 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2464 ip->i_nlink += cmd.size;
2465 DIP_SET(ip, i_nlink, ip->i_nlink);
2466 ip->i_effnlink += cmd.size;
2467 ip->i_flag |= IN_CHANGE;
2468 if (DOINGSOFTDEP(vp))
2469 softdep_change_linkcnt(ip);
2473 case FFS_ADJ_BLKCNT:
2476 printf("%s: adjust inode %jd block count by %jd\n",
2477 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2478 (intmax_t)cmd.size);
2481 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2484 if (ip->i_flag & IN_SPACECOUNTED) {
2486 fs->fs_pendingblocks += cmd.size;
2489 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + cmd.size);
2490 ip->i_flag |= IN_CHANGE;
2502 printf("%s: free %s inode %d\n",
2503 mp->mnt_stat.f_mntonname,
2504 filetype == IFDIR ? "directory" : "file",
2507 printf("%s: free %s inodes %d-%d\n",
2508 mp->mnt_stat.f_mntonname,
2509 filetype == IFDIR ? "directory" : "file",
2511 (ino_t)(cmd.value + cmd.size - 1));
2514 while (cmd.size > 0) {
2515 if ((error = ffs_freefile(ump, fs, ump->um_devvp,
2516 cmd.value, filetype)))
2527 printf("%s: free block %jd\n",
2528 mp->mnt_stat.f_mntonname,
2529 (intmax_t)cmd.value);
2531 printf("%s: free blocks %jd-%jd\n",
2532 mp->mnt_stat.f_mntonname,
2533 (intmax_t)cmd.value,
2534 (intmax_t)cmd.value + cmd.size - 1);
2539 blksize = fs->fs_frag - (blkno % fs->fs_frag);
2540 while (blkcnt > 0) {
2541 if (blksize > blkcnt)
2543 ffs_blkfree(ump, fs, ump->um_devvp, blkno,
2544 blksize * fs->fs_fsize, ROOTINO);
2547 blksize = fs->fs_frag;
2552 * Adjust superblock summaries. fsck(8) is expected to
2553 * submit deltas when necessary.
2558 printf("%s: adjust number of directories by %jd\n",
2559 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2562 fs->fs_cstotal.cs_ndir += cmd.value;
2564 case FFS_ADJ_NBFREE:
2567 printf("%s: adjust number of free blocks by %+jd\n",
2568 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2571 fs->fs_cstotal.cs_nbfree += cmd.value;
2573 case FFS_ADJ_NIFREE:
2576 printf("%s: adjust number of free inodes by %+jd\n",
2577 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2580 fs->fs_cstotal.cs_nifree += cmd.value;
2582 case FFS_ADJ_NFFREE:
2585 printf("%s: adjust number of free frags by %+jd\n",
2586 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2589 fs->fs_cstotal.cs_nffree += cmd.value;
2591 case FFS_ADJ_NUMCLUSTERS:
2594 printf("%s: adjust number of free clusters by %+jd\n",
2595 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2598 fs->fs_cstotal.cs_numclusters += cmd.value;
2604 printf("Invalid request %d from fsck\n",
2612 fdrop(fp, curthread);
2613 vn_finished_write(mp);