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 * 3. 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/capsicum.h>
69 #include <sys/systm.h>
73 #include <sys/fcntl.h>
75 #include <sys/filedesc.h>
78 #include <sys/vnode.h>
79 #include <sys/mount.h>
80 #include <sys/kernel.h>
81 #include <sys/syscallsubr.h>
82 #include <sys/sysctl.h>
83 #include <sys/syslog.h>
84 #include <sys/taskqueue.h>
86 #include <security/audit/audit.h>
88 #include <geom/geom.h>
90 #include <ufs/ufs/dir.h>
91 #include <ufs/ufs/extattr.h>
92 #include <ufs/ufs/quota.h>
93 #include <ufs/ufs/inode.h>
94 #include <ufs/ufs/ufs_extern.h>
95 #include <ufs/ufs/ufsmount.h>
97 #include <ufs/ffs/fs.h>
98 #include <ufs/ffs/ffs_extern.h>
99 #include <ufs/ffs/softdep.h>
101 typedef ufs2_daddr_t allocfcn_t(struct inode *ip, u_int cg, ufs2_daddr_t bpref,
102 int size, int rsize);
104 static ufs2_daddr_t ffs_alloccg(struct inode *, u_int, ufs2_daddr_t, int, int);
106 ffs_alloccgblk(struct inode *, struct buf *, ufs2_daddr_t, int);
107 static void ffs_blkfree_cg(struct ufsmount *, struct fs *,
108 struct vnode *, ufs2_daddr_t, long, ino_t,
110 static void ffs_blkfree_trim_completed(struct bio *);
111 static void ffs_blkfree_trim_task(void *ctx, int pending __unused);
113 static int ffs_checkblk(struct inode *, ufs2_daddr_t, long);
115 static ufs2_daddr_t ffs_clusteralloc(struct inode *, u_int, ufs2_daddr_t, int);
116 static ino_t ffs_dirpref(struct inode *);
117 static ufs2_daddr_t ffs_fragextend(struct inode *, u_int, ufs2_daddr_t,
119 static ufs2_daddr_t ffs_hashalloc
120 (struct inode *, u_int, ufs2_daddr_t, int, int, allocfcn_t *);
121 static ufs2_daddr_t ffs_nodealloccg(struct inode *, u_int, ufs2_daddr_t, int,
123 static ufs1_daddr_t ffs_mapsearch(struct fs *, struct cg *, ufs2_daddr_t, int);
124 static int ffs_reallocblks_ufs1(struct vop_reallocblks_args *);
125 static int ffs_reallocblks_ufs2(struct vop_reallocblks_args *);
128 * Allocate a block in the filesystem.
130 * The size of the requested block is given, which must be some
131 * multiple of fs_fsize and <= fs_bsize.
132 * A preference may be optionally specified. If a preference is given
133 * the following hierarchy is used to allocate a block:
134 * 1) allocate the requested block.
135 * 2) allocate a rotationally optimal block in the same cylinder.
136 * 3) allocate a block in the same cylinder group.
137 * 4) quadradically rehash into other cylinder groups, until an
138 * available block is located.
139 * If no block preference is given the following hierarchy is used
140 * to allocate a block:
141 * 1) allocate a block in the cylinder group that contains the
142 * inode for the file.
143 * 2) quadradically rehash into other cylinder groups, until an
144 * available block is located.
147 ffs_alloc(ip, lbn, bpref, size, flags, cred, bnp)
149 ufs2_daddr_t lbn, bpref;
155 struct ufsmount *ump;
158 static struct timeval lastfail;
168 mtx_assert(UFS_MTX(ump), MA_OWNED);
170 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
171 printf("dev = %s, bsize = %ld, size = %d, fs = %s\n",
172 devtoname(ump->um_dev), (long)fs->fs_bsize, size,
174 panic("ffs_alloc: bad size");
177 panic("ffs_alloc: missing credential");
178 #endif /* INVARIANTS */
183 error = chkdq(ip, btodb(size), cred, 0);
188 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
190 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0) &&
191 freespace(fs, fs->fs_minfree) - numfrags(fs, size) < 0)
193 if (bpref >= fs->fs_size)
196 cg = ino_to_cg(fs, ip->i_number);
198 cg = dtog(fs, bpref);
199 bno = ffs_hashalloc(ip, cg, bpref, size, size, ffs_alloccg);
202 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
204 ip->i_flag |= IN_CHANGE;
206 ip->i_flag |= IN_CHANGE | IN_UPDATE;
214 * Restore user's disk quota because allocation failed.
216 (void) chkdq(ip, -btodb(size), cred, FORCE);
219 if (reclaimed == 0 && (flags & IO_BUFLOCKED) == 0) {
221 softdep_request_cleanup(fs, ITOV(ip), cred, FLUSH_BLOCKS_WAIT);
225 if (reclaimed > 0 && ppsratecheck(&lastfail, &curfail, 1)) {
226 ffs_fserr(fs, ip->i_number, "filesystem full");
227 uprintf("\n%s: write failed, filesystem is full\n",
234 * Reallocate a fragment to a bigger size
236 * The number and size of the old block is given, and a preference
237 * and new size is also specified. The allocator attempts to extend
238 * the original block. Failing that, the regular block allocator is
239 * invoked to get an appropriate block.
242 ffs_realloccg(ip, lbprev, bprev, bpref, osize, nsize, flags, cred, bpp)
247 int osize, nsize, flags;
254 struct ufsmount *ump;
255 u_int cg, request, reclaimed;
258 static struct timeval lastfail;
266 gbflags = (flags & BA_UNMAPPED) != 0 ? GB_UNMAPPED : 0;
268 mtx_assert(UFS_MTX(ump), MA_OWNED);
270 if (vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
271 panic("ffs_realloccg: allocation on suspended filesystem");
272 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
273 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
275 "dev = %s, bsize = %ld, osize = %d, nsize = %d, fs = %s\n",
276 devtoname(ump->um_dev), (long)fs->fs_bsize, osize,
277 nsize, fs->fs_fsmnt);
278 panic("ffs_realloccg: bad size");
281 panic("ffs_realloccg: missing credential");
282 #endif /* INVARIANTS */
285 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0) &&
286 freespace(fs, fs->fs_minfree) - numfrags(fs, nsize - osize) < 0) {
290 printf("dev = %s, bsize = %ld, bprev = %jd, fs = %s\n",
291 devtoname(ump->um_dev), (long)fs->fs_bsize, (intmax_t)bprev,
293 panic("ffs_realloccg: bad bprev");
297 * Allocate the extra space in the buffer.
299 error = bread_gb(vp, lbprev, osize, NOCRED, gbflags, &bp);
305 if (bp->b_blkno == bp->b_lblkno) {
306 if (lbprev >= UFS_NDADDR)
307 panic("ffs_realloccg: lbprev out of range");
308 bp->b_blkno = fsbtodb(fs, bprev);
312 error = chkdq(ip, btodb(nsize - osize), cred, 0);
319 * Check for extension in the existing location.
322 cg = dtog(fs, bprev);
324 bno = ffs_fragextend(ip, cg, bprev, osize, nsize);
326 if (bp->b_blkno != fsbtodb(fs, bno))
327 panic("ffs_realloccg: bad blockno");
328 delta = btodb(nsize - osize);
329 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
331 ip->i_flag |= IN_CHANGE;
333 ip->i_flag |= IN_CHANGE | IN_UPDATE;
335 bp->b_flags |= B_DONE;
336 vfs_bio_bzero_buf(bp, osize, nsize - osize);
337 if ((bp->b_flags & (B_MALLOC | B_VMIO)) == B_VMIO)
338 vfs_bio_set_valid(bp, osize, nsize - osize);
343 * Allocate a new disk location.
345 if (bpref >= fs->fs_size)
347 switch ((int)fs->fs_optim) {
350 * Allocate an exact sized fragment. Although this makes
351 * best use of space, we will waste time relocating it if
352 * the file continues to grow. If the fragmentation is
353 * less than half of the minimum free reserve, we choose
354 * to begin optimizing for time.
357 if (fs->fs_minfree <= 5 ||
358 fs->fs_cstotal.cs_nffree >
359 (off_t)fs->fs_dsize * fs->fs_minfree / (2 * 100))
361 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
363 fs->fs_optim = FS_OPTTIME;
367 * At this point we have discovered a file that is trying to
368 * grow a small fragment to a larger fragment. To save time,
369 * we allocate a full sized block, then free the unused portion.
370 * If the file continues to grow, the `ffs_fragextend' call
371 * above will be able to grow it in place without further
372 * copying. If aberrant programs cause disk fragmentation to
373 * grow within 2% of the free reserve, we choose to begin
374 * optimizing for space.
376 request = fs->fs_bsize;
377 if (fs->fs_cstotal.cs_nffree <
378 (off_t)fs->fs_dsize * (fs->fs_minfree - 2) / 100)
380 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
382 fs->fs_optim = FS_OPTSPACE;
385 printf("dev = %s, optim = %ld, fs = %s\n",
386 devtoname(ump->um_dev), (long)fs->fs_optim, fs->fs_fsmnt);
387 panic("ffs_realloccg: bad optim");
390 bno = ffs_hashalloc(ip, cg, bpref, request, nsize, ffs_alloccg);
392 bp->b_blkno = fsbtodb(fs, bno);
393 if (!DOINGSOFTDEP(vp))
394 ffs_blkfree(ump, fs, ump->um_devvp, bprev, (long)osize,
395 ip->i_number, vp->v_type, NULL);
396 delta = btodb(nsize - osize);
397 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + delta);
399 ip->i_flag |= IN_CHANGE;
401 ip->i_flag |= IN_CHANGE | IN_UPDATE;
403 bp->b_flags |= B_DONE;
404 vfs_bio_bzero_buf(bp, osize, nsize - osize);
405 if ((bp->b_flags & (B_MALLOC | B_VMIO)) == B_VMIO)
406 vfs_bio_set_valid(bp, osize, nsize - osize);
413 * Restore user's disk quota because allocation failed.
415 (void) chkdq(ip, -btodb(nsize - osize), cred, FORCE);
422 if (reclaimed == 0 && (flags & IO_BUFLOCKED) == 0) {
430 softdep_request_cleanup(fs, vp, cred, FLUSH_BLOCKS_WAIT);
436 if (reclaimed > 0 && ppsratecheck(&lastfail, &curfail, 1)) {
437 ffs_fserr(fs, ip->i_number, "filesystem full");
438 uprintf("\n%s: write failed, filesystem is full\n",
445 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
447 * The vnode and an array of buffer pointers for a range of sequential
448 * logical blocks to be made contiguous is given. The allocator attempts
449 * to find a range of sequential blocks starting as close as possible
450 * from the end of the allocation for the logical block immediately
451 * preceding the current range. If successful, the physical block numbers
452 * in the buffer pointers and in the inode are changed to reflect the new
453 * allocation. If unsuccessful, the allocation is left unchanged. The
454 * success in doing the reallocation is returned. Note that the error
455 * return is not reflected back to the user. Rather the previous block
456 * allocation will be used.
459 SYSCTL_NODE(_vfs, OID_AUTO, ffs, CTLFLAG_RW, 0, "FFS filesystem");
461 static int doasyncfree = 1;
462 SYSCTL_INT(_vfs_ffs, OID_AUTO, doasyncfree, CTLFLAG_RW, &doasyncfree, 0,
463 "do not force synchronous writes when blocks are reallocated");
465 static int doreallocblks = 1;
466 SYSCTL_INT(_vfs_ffs, OID_AUTO, doreallocblks, CTLFLAG_RW, &doreallocblks, 0,
467 "enable block reallocation");
469 static int maxclustersearch = 10;
470 SYSCTL_INT(_vfs_ffs, OID_AUTO, maxclustersearch, CTLFLAG_RW, &maxclustersearch,
471 0, "max number of cylinder group to search for contigous blocks");
474 static volatile int prtrealloc = 0;
479 struct vop_reallocblks_args /* {
481 struct cluster_save *a_buflist;
484 struct ufsmount *ump;
487 * If the underlying device can do deletes, then skip reallocating
488 * the blocks of this file into contiguous sequences. Devices that
489 * benefit from BIO_DELETE also benefit from not moving the data.
490 * These devices are flash and therefore work less well with this
491 * optimization. Also skip if reallocblks has been disabled globally.
493 ump = ap->a_vp->v_mount->mnt_data;
494 if (ump->um_candelete || doreallocblks == 0)
498 * We can't wait in softdep prealloc as it may fsync and recurse
499 * here. Instead we simply fail to reallocate blocks if this
500 * rare condition arises.
502 if (DOINGSOFTDEP(ap->a_vp))
503 if (softdep_prealloc(ap->a_vp, MNT_NOWAIT) != 0)
505 if (ump->um_fstype == UFS1)
506 return (ffs_reallocblks_ufs1(ap));
507 return (ffs_reallocblks_ufs2(ap));
511 ffs_reallocblks_ufs1(ap)
512 struct vop_reallocblks_args /* {
514 struct cluster_save *a_buflist;
520 struct buf *sbp, *ebp;
521 ufs1_daddr_t *bap, *sbap, *ebap;
522 struct cluster_save *buflist;
523 struct ufsmount *ump;
524 ufs_lbn_t start_lbn, end_lbn;
525 ufs1_daddr_t soff, newblk, blkno;
527 struct indir start_ap[UFS_NIADDR + 1], end_ap[UFS_NIADDR + 1], *idp;
528 int i, cg, len, start_lvl, end_lvl, ssize;
535 * If we are not tracking block clusters or if we have less than 4%
536 * free blocks left, then do not attempt to cluster. Running with
537 * less than 5% free block reserve is not recommended and those that
538 * choose to do so do not expect to have good file layout.
540 if (fs->fs_contigsumsize <= 0 || freespace(fs, 4) < 0)
542 buflist = ap->a_buflist;
543 len = buflist->bs_nchildren;
544 start_lbn = buflist->bs_children[0]->b_lblkno;
545 end_lbn = start_lbn + len - 1;
547 for (i = 0; i < len; i++)
548 if (!ffs_checkblk(ip,
549 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
550 panic("ffs_reallocblks: unallocated block 1");
551 for (i = 1; i < len; i++)
552 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
553 panic("ffs_reallocblks: non-logical cluster");
554 blkno = buflist->bs_children[0]->b_blkno;
555 ssize = fsbtodb(fs, fs->fs_frag);
556 for (i = 1; i < len - 1; i++)
557 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
558 panic("ffs_reallocblks: non-physical cluster %d", i);
561 * If the cluster crosses the boundary for the first indirect
562 * block, leave space for the indirect block. Indirect blocks
563 * are initially laid out in a position after the last direct
564 * block. Block reallocation would usually destroy locality by
565 * moving the indirect block out of the way to make room for
566 * data blocks if we didn't compensate here. We should also do
567 * this for other indirect block boundaries, but it is only
568 * important for the first one.
570 if (start_lbn < UFS_NDADDR && end_lbn >= UFS_NDADDR)
573 * If the latest allocation is in a new cylinder group, assume that
574 * the filesystem has decided to move and do not force it back to
575 * the previous cylinder group.
577 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
578 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
580 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
581 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
584 * Get the starting offset and block map for the first block.
586 if (start_lvl == 0) {
587 sbap = &ip->i_din1->di_db[0];
590 idp = &start_ap[start_lvl - 1];
591 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
595 sbap = (ufs1_daddr_t *)sbp->b_data;
599 * If the block range spans two block maps, get the second map.
602 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
607 start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
608 panic("ffs_reallocblk: start == end");
610 ssize = len - (idp->in_off + 1);
611 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
613 ebap = (ufs1_daddr_t *)ebp->b_data;
616 * Find the preferred location for the cluster. If we have not
617 * previously failed at this endeavor, then follow our standard
618 * preference calculation. If we have failed at it, then pick up
619 * where we last ended our search.
622 if (ip->i_nextclustercg == -1)
623 pref = ffs_blkpref_ufs1(ip, start_lbn, soff, sbap);
625 pref = cgdata(fs, ip->i_nextclustercg);
627 * Search the block map looking for an allocation of the desired size.
628 * To avoid wasting too much time, we limit the number of cylinder
629 * groups that we will search.
632 for (i = min(maxclustersearch, fs->fs_ncg); i > 0; i--) {
633 if ((newblk = ffs_clusteralloc(ip, cg, pref, len)) != 0)
636 if (cg >= fs->fs_ncg)
640 * If we have failed in our search, record where we gave up for
641 * next time. Otherwise, fall back to our usual search citerion.
644 ip->i_nextclustercg = cg;
648 ip->i_nextclustercg = -1;
650 * We have found a new contiguous block.
652 * First we have to replace the old block pointers with the new
653 * block pointers in the inode and indirect blocks associated
658 printf("realloc: ino %ju, lbns %jd-%jd\n\told:",
659 (uintmax_t)ip->i_number,
660 (intmax_t)start_lbn, (intmax_t)end_lbn);
663 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
669 if (!ffs_checkblk(ip,
670 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
671 panic("ffs_reallocblks: unallocated block 2");
672 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
673 panic("ffs_reallocblks: alloc mismatch");
677 printf(" %d,", *bap);
679 if (DOINGSOFTDEP(vp)) {
680 if (sbap == &ip->i_din1->di_db[0] && i < ssize)
681 softdep_setup_allocdirect(ip, start_lbn + i,
682 blkno, *bap, fs->fs_bsize, fs->fs_bsize,
683 buflist->bs_children[i]);
685 softdep_setup_allocindir_page(ip, start_lbn + i,
686 i < ssize ? sbp : ebp, soff + i, blkno,
687 *bap, buflist->bs_children[i]);
692 * Next we must write out the modified inode and indirect blocks.
693 * For strict correctness, the writes should be synchronous since
694 * the old block values may have been written to disk. In practise
695 * they are almost never written, but if we are concerned about
696 * strict correctness, the `doasyncfree' flag should be set to zero.
698 * The test on `doasyncfree' should be changed to test a flag
699 * that shows whether the associated buffers and inodes have
700 * been written. The flag should be set when the cluster is
701 * started and cleared whenever the buffer or inode is flushed.
702 * We can then check below to see if it is set, and do the
703 * synchronous write only when it has been cleared.
705 if (sbap != &ip->i_din1->di_db[0]) {
711 ip->i_flag |= IN_CHANGE | IN_UPDATE;
722 * Last, free the old blocks and assign the new blocks to the buffers.
728 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
729 if (!DOINGSOFTDEP(vp))
730 ffs_blkfree(ump, fs, ump->um_devvp,
731 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
732 fs->fs_bsize, ip->i_number, vp->v_type, NULL);
733 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
735 if (!ffs_checkblk(ip,
736 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
737 panic("ffs_reallocblks: unallocated block 3");
741 printf(" %d,", blkno);
755 if (sbap != &ip->i_din1->di_db[0])
761 ffs_reallocblks_ufs2(ap)
762 struct vop_reallocblks_args /* {
764 struct cluster_save *a_buflist;
770 struct buf *sbp, *ebp;
771 ufs2_daddr_t *bap, *sbap, *ebap;
772 struct cluster_save *buflist;
773 struct ufsmount *ump;
774 ufs_lbn_t start_lbn, end_lbn;
775 ufs2_daddr_t soff, newblk, blkno, pref;
776 struct indir start_ap[UFS_NIADDR + 1], end_ap[UFS_NIADDR + 1], *idp;
777 int i, cg, len, start_lvl, end_lvl, ssize;
784 * If we are not tracking block clusters or if we have less than 4%
785 * free blocks left, then do not attempt to cluster. Running with
786 * less than 5% free block reserve is not recommended and those that
787 * choose to do so do not expect to have good file layout.
789 if (fs->fs_contigsumsize <= 0 || freespace(fs, 4) < 0)
791 buflist = ap->a_buflist;
792 len = buflist->bs_nchildren;
793 start_lbn = buflist->bs_children[0]->b_lblkno;
794 end_lbn = start_lbn + len - 1;
796 for (i = 0; i < len; i++)
797 if (!ffs_checkblk(ip,
798 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
799 panic("ffs_reallocblks: unallocated block 1");
800 for (i = 1; i < len; i++)
801 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
802 panic("ffs_reallocblks: non-logical cluster");
803 blkno = buflist->bs_children[0]->b_blkno;
804 ssize = fsbtodb(fs, fs->fs_frag);
805 for (i = 1; i < len - 1; i++)
806 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
807 panic("ffs_reallocblks: non-physical cluster %d", i);
810 * If the cluster crosses the boundary for the first indirect
811 * block, do not move anything in it. Indirect blocks are
812 * usually initially laid out in a position between the data
813 * blocks. Block reallocation would usually destroy locality by
814 * moving the indirect block out of the way to make room for
815 * data blocks if we didn't compensate here. We should also do
816 * this for other indirect block boundaries, but it is only
817 * important for the first one.
819 if (start_lbn < UFS_NDADDR && end_lbn >= UFS_NDADDR)
822 * If the latest allocation is in a new cylinder group, assume that
823 * the filesystem has decided to move and do not force it back to
824 * the previous cylinder group.
826 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
827 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
829 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
830 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
833 * Get the starting offset and block map for the first block.
835 if (start_lvl == 0) {
836 sbap = &ip->i_din2->di_db[0];
839 idp = &start_ap[start_lvl - 1];
840 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
844 sbap = (ufs2_daddr_t *)sbp->b_data;
848 * If the block range spans two block maps, get the second map.
851 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
856 start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
857 panic("ffs_reallocblk: start == end");
859 ssize = len - (idp->in_off + 1);
860 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
862 ebap = (ufs2_daddr_t *)ebp->b_data;
865 * Find the preferred location for the cluster. If we have not
866 * previously failed at this endeavor, then follow our standard
867 * preference calculation. If we have failed at it, then pick up
868 * where we last ended our search.
871 if (ip->i_nextclustercg == -1)
872 pref = ffs_blkpref_ufs2(ip, start_lbn, soff, sbap);
874 pref = cgdata(fs, ip->i_nextclustercg);
876 * Search the block map looking for an allocation of the desired size.
877 * To avoid wasting too much time, we limit the number of cylinder
878 * groups that we will search.
881 for (i = min(maxclustersearch, fs->fs_ncg); i > 0; i--) {
882 if ((newblk = ffs_clusteralloc(ip, cg, pref, len)) != 0)
885 if (cg >= fs->fs_ncg)
889 * If we have failed in our search, record where we gave up for
890 * next time. Otherwise, fall back to our usual search citerion.
893 ip->i_nextclustercg = cg;
897 ip->i_nextclustercg = -1;
899 * We have found a new contiguous block.
901 * First we have to replace the old block pointers with the new
902 * block pointers in the inode and indirect blocks associated
907 printf("realloc: ino %ju, lbns %jd-%jd\n\told:", (uintmax_t)ip->i_number,
908 (intmax_t)start_lbn, (intmax_t)end_lbn);
911 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
917 if (!ffs_checkblk(ip,
918 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
919 panic("ffs_reallocblks: unallocated block 2");
920 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
921 panic("ffs_reallocblks: alloc mismatch");
925 printf(" %jd,", (intmax_t)*bap);
927 if (DOINGSOFTDEP(vp)) {
928 if (sbap == &ip->i_din2->di_db[0] && i < ssize)
929 softdep_setup_allocdirect(ip, start_lbn + i,
930 blkno, *bap, fs->fs_bsize, fs->fs_bsize,
931 buflist->bs_children[i]);
933 softdep_setup_allocindir_page(ip, start_lbn + i,
934 i < ssize ? sbp : ebp, soff + i, blkno,
935 *bap, buflist->bs_children[i]);
940 * Next we must write out the modified inode and indirect blocks.
941 * For strict correctness, the writes should be synchronous since
942 * the old block values may have been written to disk. In practise
943 * they are almost never written, but if we are concerned about
944 * strict correctness, the `doasyncfree' flag should be set to zero.
946 * The test on `doasyncfree' should be changed to test a flag
947 * that shows whether the associated buffers and inodes have
948 * been written. The flag should be set when the cluster is
949 * started and cleared whenever the buffer or inode is flushed.
950 * We can then check below to see if it is set, and do the
951 * synchronous write only when it has been cleared.
953 if (sbap != &ip->i_din2->di_db[0]) {
959 ip->i_flag |= IN_CHANGE | IN_UPDATE;
970 * Last, free the old blocks and assign the new blocks to the buffers.
976 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
977 if (!DOINGSOFTDEP(vp))
978 ffs_blkfree(ump, fs, ump->um_devvp,
979 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
980 fs->fs_bsize, ip->i_number, vp->v_type, NULL);
981 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
983 if (!ffs_checkblk(ip,
984 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
985 panic("ffs_reallocblks: unallocated block 3");
989 printf(" %jd,", (intmax_t)blkno);
1003 if (sbap != &ip->i_din2->di_db[0])
1009 * Allocate an inode in the filesystem.
1011 * If allocating a directory, use ffs_dirpref to select the inode.
1012 * If allocating in a directory, the following hierarchy is followed:
1013 * 1) allocate the preferred inode.
1014 * 2) allocate an inode in the same cylinder group.
1015 * 3) quadradically rehash into other cylinder groups, until an
1016 * available inode is located.
1017 * If no inode preference is given the following hierarchy is used
1018 * to allocate an inode:
1019 * 1) allocate an inode in cylinder group 0.
1020 * 2) quadradically rehash into other cylinder groups, until an
1021 * available inode is located.
1024 ffs_valloc(pvp, mode, cred, vpp)
1034 struct ufsmount *ump;
1037 int error, error1, reclaimed;
1038 static struct timeval lastfail;
1049 if (fs->fs_cstotal.cs_nifree == 0)
1052 if ((mode & IFMT) == IFDIR)
1053 ipref = ffs_dirpref(pip);
1055 ipref = pip->i_number;
1056 if (ipref >= fs->fs_ncg * fs->fs_ipg)
1058 cg = ino_to_cg(fs, ipref);
1060 * Track number of dirs created one after another
1061 * in a same cg without intervening by files.
1063 if ((mode & IFMT) == IFDIR) {
1064 if (fs->fs_contigdirs[cg] < 255)
1065 fs->fs_contigdirs[cg]++;
1067 if (fs->fs_contigdirs[cg] > 0)
1068 fs->fs_contigdirs[cg]--;
1070 ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode, 0,
1071 (allocfcn_t *)ffs_nodealloccg);
1074 error = ffs_vget(pvp->v_mount, ino, LK_EXCLUSIVE, vpp);
1076 error1 = ffs_vgetf(pvp->v_mount, ino, LK_EXCLUSIVE, vpp,
1078 ffs_vfree(pvp, ino, mode);
1083 ip->i_flag |= IN_MODIFIED;
1091 printf("mode = 0%o, inum = %ju, fs = %s\n",
1092 ip->i_mode, (uintmax_t)ip->i_number, fs->fs_fsmnt);
1093 panic("ffs_valloc: dup alloc");
1095 if (DIP(ip, i_blocks) && (fs->fs_flags & FS_UNCLEAN) == 0) { /* XXX */
1096 printf("free inode %s/%lu had %ld blocks\n",
1097 fs->fs_fsmnt, (u_long)ino, (long)DIP(ip, i_blocks));
1098 DIP_SET(ip, i_blocks, 0);
1101 DIP_SET(ip, i_flags, 0);
1103 * Set up a new generation number for this inode.
1105 while (ip->i_gen == 0 || ++ip->i_gen == 0)
1106 ip->i_gen = arc4random();
1107 DIP_SET(ip, i_gen, ip->i_gen);
1108 if (fs->fs_magic == FS_UFS2_MAGIC) {
1110 ip->i_din2->di_birthtime = ts.tv_sec;
1111 ip->i_din2->di_birthnsec = ts.tv_nsec;
1113 ufs_prepare_reclaim(*vpp);
1115 (*vpp)->v_vflag = 0;
1116 (*vpp)->v_type = VNON;
1117 if (fs->fs_magic == FS_UFS2_MAGIC) {
1118 (*vpp)->v_op = &ffs_vnodeops2;
1119 ip->i_flag |= IN_UFS2;
1121 (*vpp)->v_op = &ffs_vnodeops1;
1125 if (reclaimed == 0) {
1127 softdep_request_cleanup(fs, pvp, cred, FLUSH_INODES_WAIT);
1131 if (ppsratecheck(&lastfail, &curfail, 1)) {
1132 ffs_fserr(fs, pip->i_number, "out of inodes");
1133 uprintf("\n%s: create/symlink failed, no inodes free\n",
1140 * Find a cylinder group to place a directory.
1142 * The policy implemented by this algorithm is to allocate a
1143 * directory inode in the same cylinder group as its parent
1144 * directory, but also to reserve space for its files inodes
1145 * and data. Restrict the number of directories which may be
1146 * allocated one after another in the same cylinder group
1147 * without intervening allocation of files.
1149 * If we allocate a first level directory then force allocation
1150 * in another cylinder group.
1157 int cg, prefcg, dirsize, cgsize;
1158 u_int avgifree, avgbfree, avgndir, curdirsize;
1159 u_int minifree, minbfree, maxndir;
1160 u_int mincg, minndir;
1161 u_int maxcontigdirs;
1163 mtx_assert(UFS_MTX(ITOUMP(pip)), MA_OWNED);
1166 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
1167 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1168 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
1171 * Force allocation in another cg if creating a first level dir.
1173 ASSERT_VOP_LOCKED(ITOV(pip), "ffs_dirpref");
1174 if (ITOV(pip)->v_vflag & VV_ROOT) {
1175 prefcg = arc4random() % fs->fs_ncg;
1177 minndir = fs->fs_ipg;
1178 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1179 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1180 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1181 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1183 minndir = fs->fs_cs(fs, cg).cs_ndir;
1185 for (cg = 0; cg < prefcg; cg++)
1186 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1187 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1188 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1190 minndir = fs->fs_cs(fs, cg).cs_ndir;
1192 return ((ino_t)(fs->fs_ipg * mincg));
1196 * Count various limits which used for
1197 * optimal allocation of a directory inode.
1199 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
1200 minifree = avgifree - avgifree / 4;
1203 minbfree = avgbfree - avgbfree / 4;
1206 cgsize = fs->fs_fsize * fs->fs_fpg;
1207 dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
1208 curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
1209 if (dirsize < curdirsize)
1210 dirsize = curdirsize;
1212 maxcontigdirs = 0; /* dirsize overflowed */
1214 maxcontigdirs = min((avgbfree * fs->fs_bsize) / dirsize, 255);
1215 if (fs->fs_avgfpdir > 0)
1216 maxcontigdirs = min(maxcontigdirs,
1217 fs->fs_ipg / fs->fs_avgfpdir);
1218 if (maxcontigdirs == 0)
1222 * Limit number of dirs in one cg and reserve space for
1223 * regular files, but only if we have no deficit in
1226 * We are trying to find a suitable cylinder group nearby
1227 * our preferred cylinder group to place a new directory.
1228 * We scan from our preferred cylinder group forward looking
1229 * for a cylinder group that meets our criterion. If we get
1230 * to the final cylinder group and do not find anything,
1231 * we start scanning forwards from the beginning of the
1232 * filesystem. While it might seem sensible to start scanning
1233 * backwards or even to alternate looking forward and backward,
1234 * this approach fails badly when the filesystem is nearly full.
1235 * Specifically, we first search all the areas that have no space
1236 * and finally try the one preceding that. We repeat this on
1237 * every request and in the case of the final block end up
1238 * searching the entire filesystem. By jumping to the front
1239 * of the filesystem, our future forward searches always look
1240 * in new cylinder groups so finds every possible block after
1241 * one pass over the filesystem.
1243 prefcg = ino_to_cg(fs, pip->i_number);
1244 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1245 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1246 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1247 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1248 if (fs->fs_contigdirs[cg] < maxcontigdirs)
1249 return ((ino_t)(fs->fs_ipg * cg));
1251 for (cg = 0; cg < prefcg; cg++)
1252 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1253 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1254 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1255 if (fs->fs_contigdirs[cg] < maxcontigdirs)
1256 return ((ino_t)(fs->fs_ipg * cg));
1259 * This is a backstop when we have deficit in space.
1261 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1262 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1263 return ((ino_t)(fs->fs_ipg * cg));
1264 for (cg = 0; cg < prefcg; cg++)
1265 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1267 return ((ino_t)(fs->fs_ipg * cg));
1271 * Select the desired position for the next block in a file. The file is
1272 * logically divided into sections. The first section is composed of the
1273 * direct blocks and the next fs_maxbpg blocks. Each additional section
1274 * contains fs_maxbpg blocks.
1276 * If no blocks have been allocated in the first section, the policy is to
1277 * request a block in the same cylinder group as the inode that describes
1278 * the file. The first indirect is allocated immediately following the last
1279 * direct block and the data blocks for the first indirect immediately
1282 * If no blocks have been allocated in any other section, the indirect
1283 * block(s) are allocated in the same cylinder group as its inode in an
1284 * area reserved immediately following the inode blocks. The policy for
1285 * the data blocks is to place them in a cylinder group with a greater than
1286 * average number of free blocks. An appropriate cylinder group is found
1287 * by using a rotor that sweeps the cylinder groups. When a new group of
1288 * blocks is needed, the sweep begins in the cylinder group following the
1289 * cylinder group from which the previous allocation was made. The sweep
1290 * continues until a cylinder group with greater than the average number
1291 * of free blocks is found. If the allocation is for the first block in an
1292 * indirect block or the previous block is a hole, then the information on
1293 * the previous allocation is unavailable; here a best guess is made based
1294 * on the logical block number being allocated.
1296 * If a section is already partially allocated, the policy is to
1297 * allocate blocks contiguously within the section if possible.
1300 ffs_blkpref_ufs1(ip, lbn, indx, bap)
1308 u_int avgbfree, startcg;
1311 KASSERT(indx <= 0 || bap != NULL, ("need non-NULL bap"));
1312 mtx_assert(UFS_MTX(ITOUMP(ip)), MA_OWNED);
1315 * Allocation of indirect blocks is indicated by passing negative
1316 * values in indx: -1 for single indirect, -2 for double indirect,
1317 * -3 for triple indirect. As noted below, we attempt to allocate
1318 * the first indirect inline with the file data. For all later
1319 * indirect blocks, the data is often allocated in other cylinder
1320 * groups. However to speed random file access and to speed up
1321 * fsck, the filesystem reserves the first fs_metaspace blocks
1322 * (typically half of fs_minfree) of the data area of each cylinder
1323 * group to hold these later indirect blocks.
1325 inocg = ino_to_cg(fs, ip->i_number);
1328 * Our preference for indirect blocks is the zone at the
1329 * beginning of the inode's cylinder group data area that
1330 * we try to reserve for indirect blocks.
1332 pref = cgmeta(fs, inocg);
1334 * If we are allocating the first indirect block, try to
1335 * place it immediately following the last direct block.
1337 if (indx == -1 && lbn < UFS_NDADDR + NINDIR(fs) &&
1338 ip->i_din1->di_db[UFS_NDADDR - 1] != 0)
1339 pref = ip->i_din1->di_db[UFS_NDADDR - 1] + fs->fs_frag;
1343 * If we are allocating the first data block in the first indirect
1344 * block and the indirect has been allocated in the data block area,
1345 * try to place it immediately following the indirect block.
1347 if (lbn == UFS_NDADDR) {
1348 pref = ip->i_din1->di_ib[0];
1349 if (pref != 0 && pref >= cgdata(fs, inocg) &&
1350 pref < cgbase(fs, inocg + 1))
1351 return (pref + fs->fs_frag);
1354 * If we are at the beginning of a file, or we have already allocated
1355 * the maximum number of blocks per cylinder group, or we do not
1356 * have a block allocated immediately preceding us, then we need
1357 * to decide where to start allocating new blocks.
1359 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1361 * If we are allocating a directory data block, we want
1362 * to place it in the metadata area.
1364 if ((ip->i_mode & IFMT) == IFDIR)
1365 return (cgmeta(fs, inocg));
1367 * Until we fill all the direct and all the first indirect's
1368 * blocks, we try to allocate in the data area of the inode's
1371 if (lbn < UFS_NDADDR + NINDIR(fs))
1372 return (cgdata(fs, inocg));
1374 * Find a cylinder with greater than average number of
1375 * unused data blocks.
1377 if (indx == 0 || bap[indx - 1] == 0)
1378 startcg = inocg + lbn / fs->fs_maxbpg;
1380 startcg = dtog(fs, bap[indx - 1]) + 1;
1381 startcg %= fs->fs_ncg;
1382 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1383 for (cg = startcg; cg < fs->fs_ncg; cg++)
1384 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1385 fs->fs_cgrotor = cg;
1386 return (cgdata(fs, cg));
1388 for (cg = 0; cg <= startcg; cg++)
1389 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1390 fs->fs_cgrotor = cg;
1391 return (cgdata(fs, cg));
1396 * Otherwise, we just always try to lay things out contiguously.
1398 return (bap[indx - 1] + fs->fs_frag);
1402 * Same as above, but for UFS2
1405 ffs_blkpref_ufs2(ip, lbn, indx, bap)
1413 u_int avgbfree, startcg;
1416 KASSERT(indx <= 0 || bap != NULL, ("need non-NULL bap"));
1417 mtx_assert(UFS_MTX(ITOUMP(ip)), MA_OWNED);
1420 * Allocation of indirect blocks is indicated by passing negative
1421 * values in indx: -1 for single indirect, -2 for double indirect,
1422 * -3 for triple indirect. As noted below, we attempt to allocate
1423 * the first indirect inline with the file data. For all later
1424 * indirect blocks, the data is often allocated in other cylinder
1425 * groups. However to speed random file access and to speed up
1426 * fsck, the filesystem reserves the first fs_metaspace blocks
1427 * (typically half of fs_minfree) of the data area of each cylinder
1428 * group to hold these later indirect blocks.
1430 inocg = ino_to_cg(fs, ip->i_number);
1433 * Our preference for indirect blocks is the zone at the
1434 * beginning of the inode's cylinder group data area that
1435 * we try to reserve for indirect blocks.
1437 pref = cgmeta(fs, inocg);
1439 * If we are allocating the first indirect block, try to
1440 * place it immediately following the last direct block.
1442 if (indx == -1 && lbn < UFS_NDADDR + NINDIR(fs) &&
1443 ip->i_din2->di_db[UFS_NDADDR - 1] != 0)
1444 pref = ip->i_din2->di_db[UFS_NDADDR - 1] + fs->fs_frag;
1448 * If we are allocating the first data block in the first indirect
1449 * block and the indirect has been allocated in the data block area,
1450 * try to place it immediately following the indirect block.
1452 if (lbn == UFS_NDADDR) {
1453 pref = ip->i_din2->di_ib[0];
1454 if (pref != 0 && pref >= cgdata(fs, inocg) &&
1455 pref < cgbase(fs, inocg + 1))
1456 return (pref + fs->fs_frag);
1459 * If we are at the beginning of a file, or we have already allocated
1460 * the maximum number of blocks per cylinder group, or we do not
1461 * have a block allocated immediately preceding us, then we need
1462 * to decide where to start allocating new blocks.
1464 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1466 * If we are allocating a directory data block, we want
1467 * to place it in the metadata area.
1469 if ((ip->i_mode & IFMT) == IFDIR)
1470 return (cgmeta(fs, inocg));
1472 * Until we fill all the direct and all the first indirect's
1473 * blocks, we try to allocate in the data area of the inode's
1476 if (lbn < UFS_NDADDR + NINDIR(fs))
1477 return (cgdata(fs, inocg));
1479 * Find a cylinder with greater than average number of
1480 * unused data blocks.
1482 if (indx == 0 || bap[indx - 1] == 0)
1483 startcg = inocg + lbn / fs->fs_maxbpg;
1485 startcg = dtog(fs, bap[indx - 1]) + 1;
1486 startcg %= fs->fs_ncg;
1487 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1488 for (cg = startcg; cg < fs->fs_ncg; cg++)
1489 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1490 fs->fs_cgrotor = cg;
1491 return (cgdata(fs, cg));
1493 for (cg = 0; cg <= startcg; cg++)
1494 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1495 fs->fs_cgrotor = cg;
1496 return (cgdata(fs, cg));
1501 * Otherwise, we just always try to lay things out contiguously.
1503 return (bap[indx - 1] + fs->fs_frag);
1507 * Implement the cylinder overflow algorithm.
1509 * The policy implemented by this algorithm is:
1510 * 1) allocate the block in its requested cylinder group.
1511 * 2) quadradically rehash on the cylinder group number.
1512 * 3) brute force search for a free block.
1514 * Must be called with the UFS lock held. Will release the lock on success
1515 * and return with it held on failure.
1519 ffs_hashalloc(ip, cg, pref, size, rsize, allocator)
1523 int size; /* Search size for data blocks, mode for inodes */
1524 int rsize; /* Real allocated size. */
1525 allocfcn_t *allocator;
1528 ufs2_daddr_t result;
1531 mtx_assert(UFS_MTX(ITOUMP(ip)), MA_OWNED);
1533 if (ITOV(ip)->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
1534 panic("ffs_hashalloc: allocation on suspended filesystem");
1538 * 1: preferred cylinder group
1540 result = (*allocator)(ip, cg, pref, size, rsize);
1544 * 2: quadratic rehash
1546 for (i = 1; i < fs->fs_ncg; i *= 2) {
1548 if (cg >= fs->fs_ncg)
1550 result = (*allocator)(ip, cg, 0, size, rsize);
1555 * 3: brute force search
1556 * Note that we start at i == 2, since 0 was checked initially,
1557 * and 1 is always checked in the quadratic rehash.
1559 cg = (icg + 2) % fs->fs_ncg;
1560 for (i = 2; i < fs->fs_ncg; i++) {
1561 result = (*allocator)(ip, cg, 0, size, rsize);
1565 if (cg == fs->fs_ncg)
1572 * Determine whether a fragment can be extended.
1574 * Check to see if the necessary fragments are available, and
1575 * if they are, allocate them.
1578 ffs_fragextend(ip, cg, bprev, osize, nsize)
1587 struct ufsmount *ump;
1596 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
1598 frags = numfrags(fs, nsize);
1599 bbase = fragnum(fs, bprev);
1600 if (bbase > fragnum(fs, (bprev + frags - 1))) {
1601 /* cannot extend across a block boundary */
1605 if ((error = ffs_getcg(fs, ump->um_devvp, cg, &bp, &cgp)) != 0)
1607 bno = dtogd(fs, bprev);
1608 blksfree = cg_blksfree(cgp);
1609 for (i = numfrags(fs, osize); i < frags; i++)
1610 if (isclr(blksfree, bno + i))
1613 * the current fragment can be extended
1614 * deduct the count on fragment being extended into
1615 * increase the count on the remaining fragment (if any)
1616 * allocate the extended piece
1618 for (i = frags; i < fs->fs_frag - bbase; i++)
1619 if (isclr(blksfree, bno + i))
1621 cgp->cg_frsum[i - numfrags(fs, osize)]--;
1623 cgp->cg_frsum[i - frags]++;
1624 for (i = numfrags(fs, osize), nffree = 0; i < frags; i++) {
1625 clrbit(blksfree, bno + i);
1626 cgp->cg_cs.cs_nffree--;
1630 fs->fs_cstotal.cs_nffree -= nffree;
1631 fs->fs_cs(fs, cg).cs_nffree -= nffree;
1633 ACTIVECLEAR(fs, cg);
1635 if (DOINGSOFTDEP(ITOV(ip)))
1636 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), bprev,
1637 frags, numfrags(fs, osize));
1649 * Determine whether a block can be allocated.
1651 * Check to see if a block of the appropriate size is available,
1652 * and if it is, allocate it.
1655 ffs_alloccg(ip, cg, bpref, size, rsize)
1665 struct ufsmount *ump;
1668 int i, allocsiz, error, frags;
1673 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1676 if ((error = ffs_getcg(fs, ump->um_devvp, cg, &bp, &cgp)) != 0 ||
1677 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize))
1679 if (size == fs->fs_bsize) {
1681 blkno = ffs_alloccgblk(ip, bp, bpref, rsize);
1682 ACTIVECLEAR(fs, cg);
1688 * check to see if any fragments are already available
1689 * allocsiz is the size which will be allocated, hacking
1690 * it down to a smaller size if necessary
1692 blksfree = cg_blksfree(cgp);
1693 frags = numfrags(fs, size);
1694 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1695 if (cgp->cg_frsum[allocsiz] != 0)
1697 if (allocsiz == fs->fs_frag) {
1699 * no fragments were available, so a block will be
1700 * allocated, and hacked up
1702 if (cgp->cg_cs.cs_nbfree == 0)
1705 blkno = ffs_alloccgblk(ip, bp, bpref, rsize);
1706 ACTIVECLEAR(fs, cg);
1711 KASSERT(size == rsize,
1712 ("ffs_alloccg: size(%d) != rsize(%d)", size, rsize));
1713 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1716 for (i = 0; i < frags; i++)
1717 clrbit(blksfree, bno + i);
1718 cgp->cg_cs.cs_nffree -= frags;
1719 cgp->cg_frsum[allocsiz]--;
1720 if (frags != allocsiz)
1721 cgp->cg_frsum[allocsiz - frags]++;
1723 fs->fs_cstotal.cs_nffree -= frags;
1724 fs->fs_cs(fs, cg).cs_nffree -= frags;
1726 blkno = cgbase(fs, cg) + bno;
1727 ACTIVECLEAR(fs, cg);
1729 if (DOINGSOFTDEP(ITOV(ip)))
1730 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno, frags, 0);
1741 * Allocate a block in a cylinder group.
1743 * This algorithm implements the following policy:
1744 * 1) allocate the requested block.
1745 * 2) allocate a rotationally optimal block in the same cylinder.
1746 * 3) allocate the next available block on the block rotor for the
1747 * specified cylinder group.
1748 * Note that this routine only allocates fs_bsize blocks; these
1749 * blocks may be fragmented by the routine that allocates them.
1752 ffs_alloccgblk(ip, bp, bpref, size)
1760 struct ufsmount *ump;
1768 mtx_assert(UFS_MTX(ump), MA_OWNED);
1769 cgp = (struct cg *)bp->b_data;
1770 blksfree = cg_blksfree(cgp);
1772 bpref = cgbase(fs, cgp->cg_cgx) + cgp->cg_rotor + fs->fs_frag;
1773 } else if ((cgbpref = dtog(fs, bpref)) != cgp->cg_cgx) {
1774 /* map bpref to correct zone in this cg */
1775 if (bpref < cgdata(fs, cgbpref))
1776 bpref = cgmeta(fs, cgp->cg_cgx);
1778 bpref = cgdata(fs, cgp->cg_cgx);
1781 * if the requested block is available, use it
1783 bno = dtogd(fs, blknum(fs, bpref));
1784 if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
1787 * Take the next available block in this cylinder group.
1789 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1792 /* Update cg_rotor only if allocated from the data zone */
1793 if (bno >= dtogd(fs, cgdata(fs, cgp->cg_cgx)))
1794 cgp->cg_rotor = bno;
1796 blkno = fragstoblks(fs, bno);
1797 ffs_clrblock(fs, blksfree, (long)blkno);
1798 ffs_clusteracct(fs, cgp, blkno, -1);
1799 cgp->cg_cs.cs_nbfree--;
1800 fs->fs_cstotal.cs_nbfree--;
1801 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1803 blkno = cgbase(fs, cgp->cg_cgx) + bno;
1805 * If the caller didn't want the whole block free the frags here.
1807 size = numfrags(fs, size);
1808 if (size != fs->fs_frag) {
1809 bno = dtogd(fs, blkno);
1810 for (i = size; i < fs->fs_frag; i++)
1811 setbit(blksfree, bno + i);
1812 i = fs->fs_frag - size;
1813 cgp->cg_cs.cs_nffree += i;
1814 fs->fs_cstotal.cs_nffree += i;
1815 fs->fs_cs(fs, cgp->cg_cgx).cs_nffree += i;
1821 if (DOINGSOFTDEP(ITOV(ip)))
1822 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno,
1829 * Determine whether a cluster can be allocated.
1831 * We do not currently check for optimal rotational layout if there
1832 * are multiple choices in the same cylinder group. Instead we just
1833 * take the first one that we find following bpref.
1836 ffs_clusteralloc(ip, cg, bpref, len)
1845 struct ufsmount *ump;
1846 int i, run, bit, map, got, error;
1854 if (fs->fs_maxcluster[cg] < len)
1857 if ((error = ffs_getcg(fs, ump->um_devvp, cg, &bp, &cgp)) != 0) {
1862 * Check to see if a cluster of the needed size (or bigger) is
1863 * available in this cylinder group.
1865 lp = &cg_clustersum(cgp)[len];
1866 for (i = len; i <= fs->fs_contigsumsize; i++)
1869 if (i > fs->fs_contigsumsize) {
1871 * This is the first time looking for a cluster in this
1872 * cylinder group. Update the cluster summary information
1873 * to reflect the true maximum sized cluster so that
1874 * future cluster allocation requests can avoid reading
1875 * the cylinder group map only to find no clusters.
1877 lp = &cg_clustersum(cgp)[len - 1];
1878 for (i = len - 1; i > 0; i--)
1882 fs->fs_maxcluster[cg] = i;
1887 * Search the cluster map to find a big enough cluster.
1888 * We take the first one that we find, even if it is larger
1889 * than we need as we prefer to get one close to the previous
1890 * block allocation. We do not search before the current
1891 * preference point as we do not want to allocate a block
1892 * that is allocated before the previous one (as we will
1893 * then have to wait for another pass of the elevator
1894 * algorithm before it will be read). We prefer to fail and
1895 * be recalled to try an allocation in the next cylinder group.
1897 if (dtog(fs, bpref) != cg)
1898 bpref = cgdata(fs, cg);
1900 bpref = blknum(fs, bpref);
1901 bpref = fragstoblks(fs, dtogd(fs, bpref));
1902 mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1904 bit = 1 << (bpref % NBBY);
1905 for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
1906 if ((map & bit) == 0) {
1913 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1920 if (got >= cgp->cg_nclusterblks) {
1926 * Allocate the cluster that we have found.
1928 blksfree = cg_blksfree(cgp);
1929 for (i = 1; i <= len; i++)
1930 if (!ffs_isblock(fs, blksfree, got - run + i))
1931 panic("ffs_clusteralloc: map mismatch");
1932 bno = cgbase(fs, cg) + blkstofrags(fs, got - run + 1);
1933 if (dtog(fs, bno) != cg)
1934 panic("ffs_clusteralloc: allocated out of group");
1935 len = blkstofrags(fs, len);
1937 for (i = 0; i < len; i += fs->fs_frag)
1938 if (ffs_alloccgblk(ip, bp, bno + i, fs->fs_bsize) != bno + i)
1939 panic("ffs_clusteralloc: lost block");
1940 ACTIVECLEAR(fs, cg);
1946 static inline struct buf *
1947 getinobuf(struct inode *ip, u_int cg, u_int32_t cginoblk, int gbflags)
1952 return (getblk(ITODEVVP(ip), fsbtodb(fs, ino_to_fsba(fs,
1953 cg * fs->fs_ipg + cginoblk)), (int)fs->fs_bsize, 0, 0,
1958 * Determine whether an inode can be allocated.
1960 * Check to see if an inode is available, and if it is,
1961 * allocate it using the following policy:
1962 * 1) allocate the requested inode.
1963 * 2) allocate the next available inode after the requested
1964 * inode in the specified cylinder group.
1967 ffs_nodealloccg(ip, cg, ipref, mode, unused)
1976 struct buf *bp, *ibp;
1977 struct ufsmount *ump;
1978 u_int8_t *inosused, *loc;
1979 struct ufs2_dinode *dp2;
1980 int error, start, len, i;
1981 u_int32_t old_initediblk;
1986 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1989 if ((error = ffs_getcg(fs, ump->um_devvp, cg, &bp, &cgp)) != 0) {
1994 if (cgp->cg_cs.cs_nifree == 0) {
1999 inosused = cg_inosused(cgp);
2001 ipref %= fs->fs_ipg;
2002 if (isclr(inosused, ipref))
2005 start = cgp->cg_irotor / NBBY;
2006 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
2007 loc = memcchr(&inosused[start], 0xff, len);
2011 loc = memcchr(&inosused[start], 0xff, len);
2013 printf("cg = %d, irotor = %ld, fs = %s\n",
2014 cg, (long)cgp->cg_irotor, fs->fs_fsmnt);
2015 panic("ffs_nodealloccg: map corrupted");
2019 ipref = (loc - inosused) * NBBY + ffs(~*loc) - 1;
2022 * Check to see if we need to initialize more inodes.
2024 if (fs->fs_magic == FS_UFS2_MAGIC &&
2025 ipref + INOPB(fs) > cgp->cg_initediblk &&
2026 cgp->cg_initediblk < cgp->cg_niblk) {
2027 old_initediblk = cgp->cg_initediblk;
2030 * Free the cylinder group lock before writing the
2031 * initialized inode block. Entering the
2032 * babarrierwrite() with the cylinder group lock
2033 * causes lock order violation between the lock and
2036 * Another thread can decide to initialize the same
2037 * inode block, but whichever thread first gets the
2038 * cylinder group lock after writing the newly
2039 * allocated inode block will update it and the other
2040 * will realize that it has lost and leave the
2041 * cylinder group unchanged.
2043 ibp = getinobuf(ip, cg, old_initediblk, GB_LOCK_NOWAIT);
2047 * The inode block buffer is already owned by
2048 * another thread, which must initialize it.
2049 * Wait on the buffer to allow another thread
2050 * to finish the updates, with dropped cg
2051 * buffer lock, then retry.
2053 ibp = getinobuf(ip, cg, old_initediblk, 0);
2058 bzero(ibp->b_data, (int)fs->fs_bsize);
2059 dp2 = (struct ufs2_dinode *)(ibp->b_data);
2060 for (i = 0; i < INOPB(fs); i++) {
2061 while (dp2->di_gen == 0)
2062 dp2->di_gen = arc4random();
2066 * Rather than adding a soft updates dependency to ensure
2067 * that the new inode block is written before it is claimed
2068 * by the cylinder group map, we just do a barrier write
2069 * here. The barrier write will ensure that the inode block
2070 * gets written before the updated cylinder group map can be
2071 * written. The barrier write should only slow down bulk
2072 * loading of newly created filesystems.
2074 babarrierwrite(ibp);
2077 * After the inode block is written, try to update the
2078 * cg initediblk pointer. If another thread beat us
2079 * to it, then leave it unchanged as the other thread
2080 * has already set it correctly.
2082 error = ffs_getcg(fs, ump->um_devvp, cg, &bp, &cgp);
2084 ACTIVECLEAR(fs, cg);
2088 if (cgp->cg_initediblk == old_initediblk)
2089 cgp->cg_initediblk += INOPB(fs);
2092 cgp->cg_irotor = ipref;
2094 ACTIVECLEAR(fs, cg);
2095 setbit(inosused, ipref);
2096 cgp->cg_cs.cs_nifree--;
2097 fs->fs_cstotal.cs_nifree--;
2098 fs->fs_cs(fs, cg).cs_nifree--;
2100 if ((mode & IFMT) == IFDIR) {
2101 cgp->cg_cs.cs_ndir++;
2102 fs->fs_cstotal.cs_ndir++;
2103 fs->fs_cs(fs, cg).cs_ndir++;
2106 if (DOINGSOFTDEP(ITOV(ip)))
2107 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref, mode);
2109 return ((ino_t)(cg * fs->fs_ipg + ipref));
2113 * Free a block or fragment.
2115 * The specified block or fragment is placed back in the
2116 * free map. If a fragment is deallocated, a possible
2117 * block reassembly is checked.
2120 ffs_blkfree_cg(ump, fs, devvp, bno, size, inum, dephd)
2121 struct ufsmount *ump;
2123 struct vnode *devvp;
2127 struct workhead *dephd;
2132 ufs1_daddr_t fragno, cgbno;
2133 ufs2_daddr_t cgblkno;
2134 int i, blk, frags, bbase, error;
2140 if (devvp->v_type == VREG) {
2141 /* devvp is a snapshot */
2142 MPASS(devvp->v_mount->mnt_data == ump);
2143 dev = ump->um_devvp->v_rdev;
2144 cgblkno = fragstoblks(fs, cgtod(fs, cg));
2145 } else if (devvp->v_type == VCHR) {
2146 /* devvp is a normal disk device */
2147 dev = devvp->v_rdev;
2148 cgblkno = fsbtodb(fs, cgtod(fs, cg));
2149 ASSERT_VOP_LOCKED(devvp, "ffs_blkfree_cg");
2153 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
2154 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
2155 printf("dev=%s, bno = %jd, bsize = %ld, size = %ld, fs = %s\n",
2156 devtoname(dev), (intmax_t)bno, (long)fs->fs_bsize,
2157 size, fs->fs_fsmnt);
2158 panic("ffs_blkfree_cg: bad size");
2161 if ((u_int)bno >= fs->fs_size) {
2162 printf("bad block %jd, ino %lu\n", (intmax_t)bno,
2164 ffs_fserr(fs, inum, "bad block");
2167 if ((error = ffs_getcg(fs, devvp, cg, &bp, &cgp)) != 0)
2169 cgbno = dtogd(fs, bno);
2170 blksfree = cg_blksfree(cgp);
2172 if (size == fs->fs_bsize) {
2173 fragno = fragstoblks(fs, cgbno);
2174 if (!ffs_isfreeblock(fs, blksfree, fragno)) {
2175 if (devvp->v_type == VREG) {
2177 /* devvp is a snapshot */
2181 printf("dev = %s, block = %jd, fs = %s\n",
2182 devtoname(dev), (intmax_t)bno, fs->fs_fsmnt);
2183 panic("ffs_blkfree_cg: freeing free block");
2185 ffs_setblock(fs, blksfree, fragno);
2186 ffs_clusteracct(fs, cgp, fragno, 1);
2187 cgp->cg_cs.cs_nbfree++;
2188 fs->fs_cstotal.cs_nbfree++;
2189 fs->fs_cs(fs, cg).cs_nbfree++;
2191 bbase = cgbno - fragnum(fs, cgbno);
2193 * decrement the counts associated with the old frags
2195 blk = blkmap(fs, blksfree, bbase);
2196 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
2198 * deallocate the fragment
2200 frags = numfrags(fs, size);
2201 for (i = 0; i < frags; i++) {
2202 if (isset(blksfree, cgbno + i)) {
2203 printf("dev = %s, block = %jd, fs = %s\n",
2204 devtoname(dev), (intmax_t)(bno + i),
2206 panic("ffs_blkfree_cg: freeing free frag");
2208 setbit(blksfree, cgbno + i);
2210 cgp->cg_cs.cs_nffree += i;
2211 fs->fs_cstotal.cs_nffree += i;
2212 fs->fs_cs(fs, cg).cs_nffree += i;
2214 * add back in counts associated with the new frags
2216 blk = blkmap(fs, blksfree, bbase);
2217 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
2219 * if a complete block has been reassembled, account for it
2221 fragno = fragstoblks(fs, bbase);
2222 if (ffs_isblock(fs, blksfree, fragno)) {
2223 cgp->cg_cs.cs_nffree -= fs->fs_frag;
2224 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
2225 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
2226 ffs_clusteracct(fs, cgp, fragno, 1);
2227 cgp->cg_cs.cs_nbfree++;
2228 fs->fs_cstotal.cs_nbfree++;
2229 fs->fs_cs(fs, cg).cs_nbfree++;
2233 ACTIVECLEAR(fs, cg);
2236 if (MOUNTEDSOFTDEP(mp) && devvp->v_type == VCHR)
2237 softdep_setup_blkfree(UFSTOVFS(ump), bp, bno,
2238 numfrags(fs, size), dephd);
2242 struct ffs_blkfree_trim_params {
2244 struct ufsmount *ump;
2245 struct vnode *devvp;
2249 struct workhead *pdephd;
2250 struct workhead dephd;
2254 ffs_blkfree_trim_task(ctx, pending)
2258 struct ffs_blkfree_trim_params *tp;
2261 ffs_blkfree_cg(tp->ump, tp->ump->um_fs, tp->devvp, tp->bno, tp->size,
2262 tp->inum, tp->pdephd);
2263 vn_finished_secondary_write(UFSTOVFS(tp->ump));
2264 atomic_add_int(&tp->ump->um_trim_inflight, -1);
2269 ffs_blkfree_trim_completed(bip)
2272 struct ffs_blkfree_trim_params *tp;
2274 tp = bip->bio_caller2;
2276 TASK_INIT(&tp->task, 0, ffs_blkfree_trim_task, tp);
2277 taskqueue_enqueue(tp->ump->um_trim_tq, &tp->task);
2281 ffs_blkfree(ump, fs, devvp, bno, size, inum, vtype, dephd)
2282 struct ufsmount *ump;
2284 struct vnode *devvp;
2289 struct workhead *dephd;
2293 struct ffs_blkfree_trim_params *tp;
2296 * Check to see if a snapshot wants to claim the block.
2297 * Check that devvp is a normal disk device, not a snapshot,
2298 * it has a snapshot(s) associated with it, and one of the
2299 * snapshots wants to claim the block.
2301 if (devvp->v_type == VCHR &&
2302 (devvp->v_vflag & VV_COPYONWRITE) &&
2303 ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, dephd)) {
2307 * Nothing to delay if TRIM is disabled, or the operation is
2308 * performed on the snapshot.
2310 if (!ump->um_candelete || devvp->v_type == VREG) {
2311 ffs_blkfree_cg(ump, fs, devvp, bno, size, inum, dephd);
2316 * Postpone the set of the free bit in the cg bitmap until the
2317 * BIO_DELETE is completed. Otherwise, due to disk queue
2318 * reordering, TRIM might be issued after we reuse the block
2319 * and write some new data into it.
2321 atomic_add_int(&ump->um_trim_inflight, 1);
2322 tp = malloc(sizeof(struct ffs_blkfree_trim_params), M_TEMP, M_WAITOK);
2328 if (dephd != NULL) {
2329 LIST_INIT(&tp->dephd);
2330 LIST_SWAP(dephd, &tp->dephd, worklist, wk_list);
2331 tp->pdephd = &tp->dephd;
2335 bip = g_alloc_bio();
2336 bip->bio_cmd = BIO_DELETE;
2337 bip->bio_offset = dbtob(fsbtodb(fs, bno));
2338 bip->bio_done = ffs_blkfree_trim_completed;
2339 bip->bio_length = size;
2340 bip->bio_caller2 = tp;
2343 vn_start_secondary_write(NULL, &mp, 0);
2344 g_io_request(bip, (struct g_consumer *)devvp->v_bufobj.bo_private);
2349 * Verify allocation of a block or fragment. Returns true if block or
2350 * fragment is allocated, false if it is free.
2353 ffs_checkblk(ip, bno, size)
2362 int i, error, frags, free;
2366 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
2367 printf("bsize = %ld, size = %ld, fs = %s\n",
2368 (long)fs->fs_bsize, size, fs->fs_fsmnt);
2369 panic("ffs_checkblk: bad size");
2371 if ((u_int)bno >= fs->fs_size)
2372 panic("ffs_checkblk: bad block %jd", (intmax_t)bno);
2373 error = ffs_getcg(fs, ITODEVVP(ip), dtog(fs, bno), &bp, &cgp);
2375 panic("ffs_checkblk: cylinder group read failed");
2376 blksfree = cg_blksfree(cgp);
2377 cgbno = dtogd(fs, bno);
2378 if (size == fs->fs_bsize) {
2379 free = ffs_isblock(fs, blksfree, fragstoblks(fs, cgbno));
2381 frags = numfrags(fs, size);
2382 for (free = 0, i = 0; i < frags; i++)
2383 if (isset(blksfree, cgbno + i))
2385 if (free != 0 && free != frags)
2386 panic("ffs_checkblk: partially free fragment");
2391 #endif /* INVARIANTS */
2397 ffs_vfree(pvp, ino, mode)
2402 struct ufsmount *ump;
2405 if (DOINGSOFTDEP(pvp)) {
2406 softdep_freefile(pvp, ino, mode);
2410 ump = VFSTOUFS(pvp->v_mount);
2411 return (ffs_freefile(ump, ump->um_fs, ump->um_devvp, ino, mode, NULL));
2415 * Do the actual free operation.
2416 * The specified inode is placed back in the free map.
2419 ffs_freefile(ump, fs, devvp, ino, mode, wkhd)
2420 struct ufsmount *ump;
2422 struct vnode *devvp;
2425 struct workhead *wkhd;
2435 cg = ino_to_cg(fs, ino);
2436 if (devvp->v_type == VREG) {
2437 /* devvp is a snapshot */
2438 MPASS(devvp->v_mount->mnt_data == ump);
2439 dev = ump->um_devvp->v_rdev;
2440 cgbno = fragstoblks(fs, cgtod(fs, cg));
2441 } else if (devvp->v_type == VCHR) {
2442 /* devvp is a normal disk device */
2443 dev = devvp->v_rdev;
2444 cgbno = fsbtodb(fs, cgtod(fs, cg));
2449 if (ino >= fs->fs_ipg * fs->fs_ncg)
2450 panic("ffs_freefile: range: dev = %s, ino = %ju, fs = %s",
2451 devtoname(dev), (uintmax_t)ino, fs->fs_fsmnt);
2452 if ((error = ffs_getcg(fs, devvp, cg, &bp, &cgp)) != 0)
2454 inosused = cg_inosused(cgp);
2456 if (isclr(inosused, ino)) {
2457 printf("dev = %s, ino = %ju, fs = %s\n", devtoname(dev),
2458 (uintmax_t)(ino + cg * fs->fs_ipg), fs->fs_fsmnt);
2459 if (fs->fs_ronly == 0)
2460 panic("ffs_freefile: freeing free inode");
2462 clrbit(inosused, ino);
2463 if (ino < cgp->cg_irotor)
2464 cgp->cg_irotor = ino;
2465 cgp->cg_cs.cs_nifree++;
2467 fs->fs_cstotal.cs_nifree++;
2468 fs->fs_cs(fs, cg).cs_nifree++;
2469 if ((mode & IFMT) == IFDIR) {
2470 cgp->cg_cs.cs_ndir--;
2471 fs->fs_cstotal.cs_ndir--;
2472 fs->fs_cs(fs, cg).cs_ndir--;
2475 ACTIVECLEAR(fs, cg);
2477 if (MOUNTEDSOFTDEP(UFSTOVFS(ump)) && devvp->v_type == VCHR)
2478 softdep_setup_inofree(UFSTOVFS(ump), bp,
2479 ino + cg * fs->fs_ipg, wkhd);
2485 * Check to see if a file is free.
2486 * Used to check for allocated files in snapshots.
2489 ffs_checkfreefile(fs, devvp, ino)
2491 struct vnode *devvp;
2501 cg = ino_to_cg(fs, ino);
2502 if (devvp->v_type == VREG) {
2503 /* devvp is a snapshot */
2504 cgbno = fragstoblks(fs, cgtod(fs, cg));
2505 } else if (devvp->v_type == VCHR) {
2506 /* devvp is a normal disk device */
2507 cgbno = fsbtodb(fs, cgtod(fs, cg));
2511 if (ino >= fs->fs_ipg * fs->fs_ncg)
2513 if ((error = ffs_getcg(fs, devvp, cg, &bp, &cgp)) != 0)
2515 inosused = cg_inosused(cgp);
2517 ret = isclr(inosused, ino);
2523 * Find a block of the specified size in the specified cylinder group.
2525 * It is a panic if a request is made to find a block if none are
2529 ffs_mapsearch(fs, cgp, bpref, allocsiz)
2536 int start, len, loc, i;
2537 int blk, field, subfield, pos;
2541 * find the fragment by searching through the free block
2542 * map for an appropriate bit pattern
2545 start = dtogd(fs, bpref) / NBBY;
2547 start = cgp->cg_frotor / NBBY;
2548 blksfree = cg_blksfree(cgp);
2549 len = howmany(fs->fs_fpg, NBBY) - start;
2550 loc = scanc((u_int)len, (u_char *)&blksfree[start],
2551 fragtbl[fs->fs_frag],
2552 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2556 loc = scanc((u_int)len, (u_char *)&blksfree[0],
2557 fragtbl[fs->fs_frag],
2558 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2560 printf("start = %d, len = %d, fs = %s\n",
2561 start, len, fs->fs_fsmnt);
2562 panic("ffs_alloccg: map corrupted");
2566 bno = (start + len - loc) * NBBY;
2567 cgp->cg_frotor = bno;
2569 * found the byte in the map
2570 * sift through the bits to find the selected frag
2572 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
2573 blk = blkmap(fs, blksfree, bno);
2575 field = around[allocsiz];
2576 subfield = inside[allocsiz];
2577 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
2578 if ((blk & field) == subfield)
2584 printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt);
2585 panic("ffs_alloccg: block not in map");
2590 * Fetch and verify a cylinder group.
2593 ffs_getcg(fs, devvp, cg, bpp, cgpp)
2595 struct vnode *devvp;
2606 error = bread(devvp, fsbtodb(fs, cgtod(fs, cg)),
2607 (int)fs->fs_cgsize, NOCRED, &bp);
2610 cgp = (struct cg *)bp->b_data;
2611 if (!cg_chkmagic(cgp) || cgp->cg_cgx != cg) {
2615 bp->b_xflags |= BX_BKGRDWRITE;
2616 cgp->cg_old_time = cgp->cg_time = time_second;
2623 * Fserr prints the name of a filesystem with an error diagnostic.
2625 * The form of the error message is:
2629 ffs_fserr(fs, inum, cp)
2634 struct thread *td = curthread; /* XXX */
2635 struct proc *p = td->td_proc;
2637 log(LOG_ERR, "pid %d (%s), uid %d inumber %ju on %s: %s\n",
2638 p->p_pid, p->p_comm, td->td_ucred->cr_uid, (uintmax_t)inum,
2643 * This function provides the capability for the fsck program to
2644 * update an active filesystem. Fourteen operations are provided:
2646 * adjrefcnt(inode, amt) - adjusts the reference count on the
2647 * specified inode by the specified amount. Under normal
2648 * operation the count should always go down. Decrementing
2649 * the count to zero will cause the inode to be freed.
2650 * adjblkcnt(inode, amt) - adjust the number of blocks used by the
2651 * inode by the specified amount.
2652 * adjndir, adjbfree, adjifree, adjffree, adjnumclusters(amt) -
2653 * adjust the superblock summary.
2654 * freedirs(inode, count) - directory inodes [inode..inode + count - 1]
2655 * are marked as free. Inodes should never have to be marked
2657 * freefiles(inode, count) - file inodes [inode..inode + count - 1]
2658 * are marked as free. Inodes should never have to be marked
2660 * freeblks(blockno, size) - blocks [blockno..blockno + size - 1]
2661 * are marked as free. Blocks should never have to be marked
2663 * setflags(flags, set/clear) - the fs_flags field has the specified
2664 * flags set (second parameter +1) or cleared (second parameter -1).
2665 * setcwd(dirinode) - set the current directory to dirinode in the
2666 * filesystem associated with the snapshot.
2667 * setdotdot(oldvalue, newvalue) - Verify that the inode number for ".."
2668 * in the current directory is oldvalue then change it to newvalue.
2669 * unlink(nameptr, oldvalue) - Verify that the inode number associated
2670 * with nameptr in the current directory is oldvalue then unlink it.
2672 * The following functions may only be used on a quiescent filesystem
2673 * by the soft updates journal. They are not safe to be run on an active
2676 * setinode(inode, dip) - the specified disk inode is replaced with the
2677 * contents pointed to by dip.
2678 * setbufoutput(fd, flags) - output associated with the specified file
2679 * descriptor (which must reference the character device supporting
2680 * the filesystem) switches from using physio to running through the
2681 * buffer cache when flags is set to 1. The descriptor reverts to
2682 * physio for output when flags is set to zero.
2685 static int sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS);
2687 SYSCTL_PROC(_vfs_ffs, FFS_ADJ_REFCNT, adjrefcnt, CTLFLAG_WR|CTLTYPE_STRUCT,
2688 0, 0, sysctl_ffs_fsck, "S,fsck", "Adjust Inode Reference Count");
2690 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_BLKCNT, adjblkcnt, CTLFLAG_WR,
2691 sysctl_ffs_fsck, "Adjust Inode Used Blocks Count");
2693 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NDIR, adjndir, CTLFLAG_WR,
2694 sysctl_ffs_fsck, "Adjust number of directories");
2696 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NBFREE, adjnbfree, CTLFLAG_WR,
2697 sysctl_ffs_fsck, "Adjust number of free blocks");
2699 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NIFREE, adjnifree, CTLFLAG_WR,
2700 sysctl_ffs_fsck, "Adjust number of free inodes");
2702 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NFFREE, adjnffree, CTLFLAG_WR,
2703 sysctl_ffs_fsck, "Adjust number of free frags");
2705 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NUMCLUSTERS, adjnumclusters, CTLFLAG_WR,
2706 sysctl_ffs_fsck, "Adjust number of free clusters");
2708 static SYSCTL_NODE(_vfs_ffs, FFS_DIR_FREE, freedirs, CTLFLAG_WR,
2709 sysctl_ffs_fsck, "Free Range of Directory Inodes");
2711 static SYSCTL_NODE(_vfs_ffs, FFS_FILE_FREE, freefiles, CTLFLAG_WR,
2712 sysctl_ffs_fsck, "Free Range of File Inodes");
2714 static SYSCTL_NODE(_vfs_ffs, FFS_BLK_FREE, freeblks, CTLFLAG_WR,
2715 sysctl_ffs_fsck, "Free Range of Blocks");
2717 static SYSCTL_NODE(_vfs_ffs, FFS_SET_FLAGS, setflags, CTLFLAG_WR,
2718 sysctl_ffs_fsck, "Change Filesystem Flags");
2720 static SYSCTL_NODE(_vfs_ffs, FFS_SET_CWD, setcwd, CTLFLAG_WR,
2721 sysctl_ffs_fsck, "Set Current Working Directory");
2723 static SYSCTL_NODE(_vfs_ffs, FFS_SET_DOTDOT, setdotdot, CTLFLAG_WR,
2724 sysctl_ffs_fsck, "Change Value of .. Entry");
2726 static SYSCTL_NODE(_vfs_ffs, FFS_UNLINK, unlink, CTLFLAG_WR,
2727 sysctl_ffs_fsck, "Unlink a Duplicate Name");
2729 static SYSCTL_NODE(_vfs_ffs, FFS_SET_INODE, setinode, CTLFLAG_WR,
2730 sysctl_ffs_fsck, "Update an On-Disk Inode");
2732 static SYSCTL_NODE(_vfs_ffs, FFS_SET_BUFOUTPUT, setbufoutput, CTLFLAG_WR,
2733 sysctl_ffs_fsck, "Set Buffered Writing for Descriptor");
2737 static int fsckcmds = 0;
2738 SYSCTL_INT(_debug, OID_AUTO, fsckcmds, CTLFLAG_RW, &fsckcmds, 0, "");
2741 static int buffered_write(struct file *, struct uio *, struct ucred *,
2742 int, struct thread *);
2745 sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS)
2747 struct thread *td = curthread;
2748 struct fsck_cmd cmd;
2749 struct ufsmount *ump;
2750 struct vnode *vp, *dvp, *fdvp;
2751 struct inode *ip, *dp;
2755 long blkcnt, blksize;
2756 struct file *fp, *vfp;
2757 cap_rights_t rights;
2758 int filetype, error;
2759 static struct fileops *origops, bufferedops;
2761 if (req->newlen > sizeof cmd)
2763 if ((error = SYSCTL_IN(req, &cmd, sizeof cmd)) != 0)
2765 if (cmd.version != FFS_CMD_VERSION)
2766 return (ERPCMISMATCH);
2767 if ((error = getvnode(td, cmd.handle,
2768 cap_rights_init(&rights, CAP_FSCK), &fp)) != 0)
2771 if (vp->v_type != VREG && vp->v_type != VDIR) {
2775 vn_start_write(vp, &mp, V_WAIT);
2777 strncmp(mp->mnt_stat.f_fstypename, "ufs", MFSNAMELEN)) {
2778 vn_finished_write(mp);
2783 if ((mp->mnt_flag & MNT_RDONLY) &&
2784 ump->um_fsckpid != td->td_proc->p_pid) {
2785 vn_finished_write(mp);
2792 switch (oidp->oid_number) {
2797 printf("%s: %s flags\n", mp->mnt_stat.f_mntonname,
2798 cmd.size > 0 ? "set" : "clear");
2801 fs->fs_flags |= (long)cmd.value;
2803 fs->fs_flags &= ~(long)cmd.value;
2806 case FFS_ADJ_REFCNT:
2809 printf("%s: adjust inode %jd link count by %jd\n",
2810 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2811 (intmax_t)cmd.size);
2814 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2817 ip->i_nlink += cmd.size;
2818 DIP_SET(ip, i_nlink, ip->i_nlink);
2819 ip->i_effnlink += cmd.size;
2820 ip->i_flag |= IN_CHANGE | IN_MODIFIED;
2821 error = ffs_update(vp, 1);
2822 if (DOINGSOFTDEP(vp))
2823 softdep_change_linkcnt(ip);
2827 case FFS_ADJ_BLKCNT:
2830 printf("%s: adjust inode %jd block count by %jd\n",
2831 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2832 (intmax_t)cmd.size);
2835 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2838 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + cmd.size);
2839 ip->i_flag |= IN_CHANGE | IN_MODIFIED;
2840 error = ffs_update(vp, 1);
2852 printf("%s: free %s inode %ju\n",
2853 mp->mnt_stat.f_mntonname,
2854 filetype == IFDIR ? "directory" : "file",
2855 (uintmax_t)cmd.value);
2857 printf("%s: free %s inodes %ju-%ju\n",
2858 mp->mnt_stat.f_mntonname,
2859 filetype == IFDIR ? "directory" : "file",
2860 (uintmax_t)cmd.value,
2861 (uintmax_t)(cmd.value + cmd.size - 1));
2864 while (cmd.size > 0) {
2865 if ((error = ffs_freefile(ump, fs, ump->um_devvp,
2866 cmd.value, filetype, NULL)))
2877 printf("%s: free block %jd\n",
2878 mp->mnt_stat.f_mntonname,
2879 (intmax_t)cmd.value);
2881 printf("%s: free blocks %jd-%jd\n",
2882 mp->mnt_stat.f_mntonname,
2883 (intmax_t)cmd.value,
2884 (intmax_t)cmd.value + cmd.size - 1);
2889 blksize = fs->fs_frag - (blkno % fs->fs_frag);
2890 while (blkcnt > 0) {
2891 if (blksize > blkcnt)
2893 ffs_blkfree(ump, fs, ump->um_devvp, blkno,
2894 blksize * fs->fs_fsize, UFS_ROOTINO, VDIR, NULL);
2897 blksize = fs->fs_frag;
2902 * Adjust superblock summaries. fsck(8) is expected to
2903 * submit deltas when necessary.
2908 printf("%s: adjust number of directories by %jd\n",
2909 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2912 fs->fs_cstotal.cs_ndir += cmd.value;
2915 case FFS_ADJ_NBFREE:
2918 printf("%s: adjust number of free blocks by %+jd\n",
2919 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2922 fs->fs_cstotal.cs_nbfree += cmd.value;
2925 case FFS_ADJ_NIFREE:
2928 printf("%s: adjust number of free inodes by %+jd\n",
2929 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2932 fs->fs_cstotal.cs_nifree += cmd.value;
2935 case FFS_ADJ_NFFREE:
2938 printf("%s: adjust number of free frags by %+jd\n",
2939 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2942 fs->fs_cstotal.cs_nffree += cmd.value;
2945 case FFS_ADJ_NUMCLUSTERS:
2948 printf("%s: adjust number of free clusters by %+jd\n",
2949 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2952 fs->fs_cstotal.cs_numclusters += cmd.value;
2958 printf("%s: set current directory to inode %jd\n",
2959 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2962 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_SHARED, &vp)))
2964 AUDIT_ARG_VNODE1(vp);
2965 if ((error = change_dir(vp, td)) != 0) {
2973 case FFS_SET_DOTDOT:
2976 printf("%s: change .. in cwd from %jd to %jd\n",
2977 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2978 (intmax_t)cmd.size);
2982 * First we have to get and lock the parent directory
2983 * to which ".." points.
2985 error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &fdvp);
2989 * Now we get and lock the child directory containing "..".
2991 FILEDESC_SLOCK(td->td_proc->p_fd);
2992 dvp = td->td_proc->p_fd->fd_cdir;
2993 FILEDESC_SUNLOCK(td->td_proc->p_fd);
2994 if ((error = vget(dvp, LK_EXCLUSIVE, td)) != 0) {
2999 dp->i_offset = 12; /* XXX mastertemplate.dot_reclen */
3000 error = ufs_dirrewrite(dp, VTOI(fdvp), (ino_t)cmd.size,
3013 if (copyinstr((char *)(intptr_t)cmd.value, buf,32,NULL))
3014 strncpy(buf, "Name_too_long", 32);
3015 printf("%s: unlink %s (inode %jd)\n",
3016 mp->mnt_stat.f_mntonname, buf, (intmax_t)cmd.size);
3020 * kern_unlinkat will do its own start/finish writes and
3021 * they do not nest, so drop ours here. Setting mp == NULL
3022 * indicates that vn_finished_write is not needed down below.
3024 vn_finished_write(mp);
3026 error = kern_unlinkat(td, AT_FDCWD, (char *)(intptr_t)cmd.value,
3027 UIO_USERSPACE, (ino_t)cmd.size);
3031 if (ump->um_fsckpid != td->td_proc->p_pid) {
3037 printf("%s: update inode %jd\n",
3038 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
3041 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
3043 AUDIT_ARG_VNODE1(vp);
3046 error = copyin((void *)(intptr_t)cmd.size, ip->i_din1,
3047 sizeof(struct ufs1_dinode));
3049 error = copyin((void *)(intptr_t)cmd.size, ip->i_din2,
3050 sizeof(struct ufs2_dinode));
3055 ip->i_flag |= IN_CHANGE | IN_MODIFIED;
3056 error = ffs_update(vp, 1);
3060 case FFS_SET_BUFOUTPUT:
3061 if (ump->um_fsckpid != td->td_proc->p_pid) {
3065 if (ITOUMP(VTOI(vp)) != ump) {
3071 printf("%s: %s buffered output for descriptor %jd\n",
3072 mp->mnt_stat.f_mntonname,
3073 cmd.size == 1 ? "enable" : "disable",
3074 (intmax_t)cmd.value);
3077 if ((error = getvnode(td, cmd.value,
3078 cap_rights_init(&rights, CAP_FSCK), &vfp)) != 0)
3080 if (vfp->f_vnode->v_type != VCHR) {
3085 if (origops == NULL) {
3086 origops = vfp->f_ops;
3087 bcopy((void *)origops, (void *)&bufferedops,
3088 sizeof(bufferedops));
3089 bufferedops.fo_write = buffered_write;
3092 atomic_store_rel_ptr((volatile uintptr_t *)&vfp->f_ops,
3093 (uintptr_t)&bufferedops);
3095 atomic_store_rel_ptr((volatile uintptr_t *)&vfp->f_ops,
3096 (uintptr_t)origops);
3103 printf("Invalid request %d from fsck\n",
3112 vn_finished_write(mp);
3117 * Function to switch a descriptor to use the buffer cache to stage
3118 * its I/O. This is needed so that writes to the filesystem device
3119 * will give snapshots a chance to copy modified blocks for which it
3120 * needs to retain copies.
3123 buffered_write(fp, uio, active_cred, flags, td)
3126 struct ucred *active_cred;
3130 struct vnode *devvp, *vp;
3134 struct filedesc *fdp;
3139 * The devvp is associated with the /dev filesystem. To discover
3140 * the filesystem with which the device is associated, we depend
3141 * on the application setting the current directory to a location
3142 * within the filesystem being written. Yes, this is an ugly hack.
3144 devvp = fp->f_vnode;
3145 if (!vn_isdisk(devvp, NULL))
3147 fdp = td->td_proc->p_fd;
3148 FILEDESC_SLOCK(fdp);
3151 FILEDESC_SUNLOCK(fdp);
3152 vn_lock(vp, LK_SHARED | LK_RETRY);
3154 * Check that the current directory vnode indeed belongs to
3155 * UFS before trying to dereference UFS-specific v_data fields.
3157 if (vp->v_op != &ffs_vnodeops1 && vp->v_op != &ffs_vnodeops2) {
3162 if (ITODEVVP(ip) != devvp) {
3168 foffset_lock_uio(fp, uio, flags);
3169 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
3172 printf("%s: buffered write for block %jd\n",
3173 fs->fs_fsmnt, (intmax_t)btodb(uio->uio_offset));
3177 * All I/O must be contained within a filesystem block, start on
3178 * a fragment boundary, and be a multiple of fragments in length.
3180 if (uio->uio_resid > fs->fs_bsize - (uio->uio_offset % fs->fs_bsize) ||
3181 fragoff(fs, uio->uio_offset) != 0 ||
3182 fragoff(fs, uio->uio_resid) != 0) {
3186 lbn = numfrags(fs, uio->uio_offset);
3187 bp = getblk(devvp, lbn, uio->uio_resid, 0, 0, 0);
3188 bp->b_flags |= B_RELBUF;
3189 if ((error = uiomove((char *)bp->b_data, uio->uio_resid, uio)) != 0) {
3195 VOP_UNLOCK(devvp, 0);
3196 foffset_unlock_uio(fp, uio, flags | FOF_NEXTOFF);