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/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(ip->i_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;
267 gbflags = (flags & BA_UNMAPPED) != 0 ? GB_UNMAPPED : 0;
269 mtx_assert(UFS_MTX(ump), MA_OWNED);
271 if (vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
272 panic("ffs_realloccg: allocation on suspended filesystem");
273 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
274 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
276 "dev = %s, bsize = %ld, osize = %d, nsize = %d, fs = %s\n",
277 devtoname(ip->i_dev), (long)fs->fs_bsize, osize,
278 nsize, fs->fs_fsmnt);
279 panic("ffs_realloccg: bad size");
282 panic("ffs_realloccg: missing credential");
283 #endif /* INVARIANTS */
286 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0) &&
287 freespace(fs, fs->fs_minfree) - numfrags(fs, nsize - osize) < 0) {
291 printf("dev = %s, bsize = %ld, bprev = %jd, fs = %s\n",
292 devtoname(ip->i_dev), (long)fs->fs_bsize, (intmax_t)bprev,
294 panic("ffs_realloccg: bad bprev");
298 * Allocate the extra space in the buffer.
300 error = bread_gb(vp, lbprev, osize, NOCRED, gbflags, &bp);
306 if (bp->b_blkno == bp->b_lblkno) {
307 if (lbprev >= NDADDR)
308 panic("ffs_realloccg: lbprev out of range");
309 bp->b_blkno = fsbtodb(fs, bprev);
313 error = chkdq(ip, btodb(nsize - osize), cred, 0);
320 * 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(ip->i_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, ip->i_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 = VTOI(ap->a_vp)->i_ump;
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 = 0;
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[NIADDR + 1], end_ap[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 < NDADDR && end_lbn >= 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.
601 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
606 start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
607 panic("ffs_reallocblk: start == end");
609 ssize = len - (idp->in_off + 1);
610 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
612 ebap = (ufs1_daddr_t *)ebp->b_data;
615 * Find the preferred location for the cluster. If we have not
616 * previously failed at this endeavor, then follow our standard
617 * preference calculation. If we have failed at it, then pick up
618 * where we last ended our search.
621 if (ip->i_nextclustercg == -1)
622 pref = ffs_blkpref_ufs1(ip, start_lbn, soff, sbap);
624 pref = cgdata(fs, ip->i_nextclustercg);
626 * Search the block map looking for an allocation of the desired size.
627 * To avoid wasting too much time, we limit the number of cylinder
628 * groups that we will search.
631 for (i = min(maxclustersearch, fs->fs_ncg); i > 0; i--) {
632 if ((newblk = ffs_clusteralloc(ip, cg, pref, len)) != 0)
635 if (cg >= fs->fs_ncg)
639 * If we have failed in our search, record where we gave up for
640 * next time. Otherwise, fall back to our usual search citerion.
643 ip->i_nextclustercg = cg;
647 ip->i_nextclustercg = -1;
649 * We have found a new contiguous block.
651 * First we have to replace the old block pointers with the new
652 * block pointers in the inode and indirect blocks associated
657 printf("realloc: ino %ju, lbns %jd-%jd\n\told:",
658 (uintmax_t)ip->i_number,
659 (intmax_t)start_lbn, (intmax_t)end_lbn);
662 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
668 if (!ffs_checkblk(ip,
669 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
670 panic("ffs_reallocblks: unallocated block 2");
671 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
672 panic("ffs_reallocblks: alloc mismatch");
676 printf(" %d,", *bap);
678 if (DOINGSOFTDEP(vp)) {
679 if (sbap == &ip->i_din1->di_db[0] && i < ssize)
680 softdep_setup_allocdirect(ip, start_lbn + i,
681 blkno, *bap, fs->fs_bsize, fs->fs_bsize,
682 buflist->bs_children[i]);
684 softdep_setup_allocindir_page(ip, start_lbn + i,
685 i < ssize ? sbp : ebp, soff + i, blkno,
686 *bap, buflist->bs_children[i]);
691 * Next we must write out the modified inode and indirect blocks.
692 * For strict correctness, the writes should be synchronous since
693 * the old block values may have been written to disk. In practise
694 * they are almost never written, but if we are concerned about
695 * strict correctness, the `doasyncfree' flag should be set to zero.
697 * The test on `doasyncfree' should be changed to test a flag
698 * that shows whether the associated buffers and inodes have
699 * been written. The flag should be set when the cluster is
700 * started and cleared whenever the buffer or inode is flushed.
701 * We can then check below to see if it is set, and do the
702 * synchronous write only when it has been cleared.
704 if (sbap != &ip->i_din1->di_db[0]) {
710 ip->i_flag |= IN_CHANGE | IN_UPDATE;
721 * Last, free the old blocks and assign the new blocks to the buffers.
727 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
728 if (!DOINGSOFTDEP(vp))
729 ffs_blkfree(ump, fs, ip->i_devvp,
730 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
731 fs->fs_bsize, ip->i_number, vp->v_type, NULL);
732 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
734 if (!ffs_checkblk(ip,
735 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
736 panic("ffs_reallocblks: unallocated block 3");
740 printf(" %d,", blkno);
754 if (sbap != &ip->i_din1->di_db[0])
760 ffs_reallocblks_ufs2(ap)
761 struct vop_reallocblks_args /* {
763 struct cluster_save *a_buflist;
769 struct buf *sbp, *ebp;
770 ufs2_daddr_t *bap, *sbap, *ebap = 0;
771 struct cluster_save *buflist;
772 struct ufsmount *ump;
773 ufs_lbn_t start_lbn, end_lbn;
774 ufs2_daddr_t soff, newblk, blkno, pref;
775 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
776 int i, cg, len, start_lvl, end_lvl, ssize;
783 * If we are not tracking block clusters or if we have less than 4%
784 * free blocks left, then do not attempt to cluster. Running with
785 * less than 5% free block reserve is not recommended and those that
786 * choose to do so do not expect to have good file layout.
788 if (fs->fs_contigsumsize <= 0 || freespace(fs, 4) < 0)
790 buflist = ap->a_buflist;
791 len = buflist->bs_nchildren;
792 start_lbn = buflist->bs_children[0]->b_lblkno;
793 end_lbn = start_lbn + len - 1;
795 for (i = 0; i < len; i++)
796 if (!ffs_checkblk(ip,
797 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
798 panic("ffs_reallocblks: unallocated block 1");
799 for (i = 1; i < len; i++)
800 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
801 panic("ffs_reallocblks: non-logical cluster");
802 blkno = buflist->bs_children[0]->b_blkno;
803 ssize = fsbtodb(fs, fs->fs_frag);
804 for (i = 1; i < len - 1; i++)
805 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
806 panic("ffs_reallocblks: non-physical cluster %d", i);
809 * If the cluster crosses the boundary for the first indirect
810 * block, do not move anything in it. Indirect blocks are
811 * usually initially laid out in a position between the data
812 * blocks. Block reallocation would usually destroy locality by
813 * moving the indirect block out of the way to make room for
814 * data blocks if we didn't compensate here. We should also do
815 * this for other indirect block boundaries, but it is only
816 * important for the first one.
818 if (start_lbn < NDADDR && end_lbn >= NDADDR)
821 * If the latest allocation is in a new cylinder group, assume that
822 * the filesystem has decided to move and do not force it back to
823 * the previous cylinder group.
825 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
826 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
828 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
829 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
832 * Get the starting offset and block map for the first block.
834 if (start_lvl == 0) {
835 sbap = &ip->i_din2->di_db[0];
838 idp = &start_ap[start_lvl - 1];
839 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
843 sbap = (ufs2_daddr_t *)sbp->b_data;
847 * If the block range spans two block maps, get the second map.
849 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
854 start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
855 panic("ffs_reallocblk: start == end");
857 ssize = len - (idp->in_off + 1);
858 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
860 ebap = (ufs2_daddr_t *)ebp->b_data;
863 * Find the preferred location for the cluster. If we have not
864 * previously failed at this endeavor, then follow our standard
865 * preference calculation. If we have failed at it, then pick up
866 * where we last ended our search.
869 if (ip->i_nextclustercg == -1)
870 pref = ffs_blkpref_ufs2(ip, start_lbn, soff, sbap);
872 pref = cgdata(fs, ip->i_nextclustercg);
874 * Search the block map looking for an allocation of the desired size.
875 * To avoid wasting too much time, we limit the number of cylinder
876 * groups that we will search.
879 for (i = min(maxclustersearch, fs->fs_ncg); i > 0; i--) {
880 if ((newblk = ffs_clusteralloc(ip, cg, pref, len)) != 0)
883 if (cg >= fs->fs_ncg)
887 * If we have failed in our search, record where we gave up for
888 * next time. Otherwise, fall back to our usual search citerion.
891 ip->i_nextclustercg = cg;
895 ip->i_nextclustercg = -1;
897 * We have found a new contiguous block.
899 * First we have to replace the old block pointers with the new
900 * block pointers in the inode and indirect blocks associated
905 printf("realloc: ino %ju, lbns %jd-%jd\n\told:", (uintmax_t)ip->i_number,
906 (intmax_t)start_lbn, (intmax_t)end_lbn);
909 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
915 if (!ffs_checkblk(ip,
916 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
917 panic("ffs_reallocblks: unallocated block 2");
918 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
919 panic("ffs_reallocblks: alloc mismatch");
923 printf(" %jd,", (intmax_t)*bap);
925 if (DOINGSOFTDEP(vp)) {
926 if (sbap == &ip->i_din2->di_db[0] && i < ssize)
927 softdep_setup_allocdirect(ip, start_lbn + i,
928 blkno, *bap, fs->fs_bsize, fs->fs_bsize,
929 buflist->bs_children[i]);
931 softdep_setup_allocindir_page(ip, start_lbn + i,
932 i < ssize ? sbp : ebp, soff + i, blkno,
933 *bap, buflist->bs_children[i]);
938 * Next we must write out the modified inode and indirect blocks.
939 * For strict correctness, the writes should be synchronous since
940 * the old block values may have been written to disk. In practise
941 * they are almost never written, but if we are concerned about
942 * strict correctness, the `doasyncfree' flag should be set to zero.
944 * The test on `doasyncfree' should be changed to test a flag
945 * that shows whether the associated buffers and inodes have
946 * been written. The flag should be set when the cluster is
947 * started and cleared whenever the buffer or inode is flushed.
948 * We can then check below to see if it is set, and do the
949 * synchronous write only when it has been cleared.
951 if (sbap != &ip->i_din2->di_db[0]) {
957 ip->i_flag |= IN_CHANGE | IN_UPDATE;
968 * Last, free the old blocks and assign the new blocks to the buffers.
974 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
975 if (!DOINGSOFTDEP(vp))
976 ffs_blkfree(ump, fs, ip->i_devvp,
977 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
978 fs->fs_bsize, ip->i_number, vp->v_type, NULL);
979 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
981 if (!ffs_checkblk(ip,
982 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
983 panic("ffs_reallocblks: unallocated block 3");
987 printf(" %jd,", (intmax_t)blkno);
1001 if (sbap != &ip->i_din2->di_db[0])
1007 * Allocate an inode in the filesystem.
1009 * If allocating a directory, use ffs_dirpref to select the inode.
1010 * If allocating in a directory, the following hierarchy is followed:
1011 * 1) allocate the preferred inode.
1012 * 2) allocate an inode in the same cylinder group.
1013 * 3) quadradically rehash into other cylinder groups, until an
1014 * available inode is located.
1015 * If no inode preference is given the following hierarchy is used
1016 * to allocate an inode:
1017 * 1) allocate an inode in cylinder group 0.
1018 * 2) quadradically rehash into other cylinder groups, until an
1019 * available inode is located.
1022 ffs_valloc(pvp, mode, cred, vpp)
1032 struct ufsmount *ump;
1035 int error, error1, reclaimed;
1036 static struct timeval lastfail;
1047 if (fs->fs_cstotal.cs_nifree == 0)
1050 if ((mode & IFMT) == IFDIR)
1051 ipref = ffs_dirpref(pip);
1053 ipref = pip->i_number;
1054 if (ipref >= fs->fs_ncg * fs->fs_ipg)
1056 cg = ino_to_cg(fs, ipref);
1058 * Track number of dirs created one after another
1059 * in a same cg without intervening by files.
1061 if ((mode & IFMT) == IFDIR) {
1062 if (fs->fs_contigdirs[cg] < 255)
1063 fs->fs_contigdirs[cg]++;
1065 if (fs->fs_contigdirs[cg] > 0)
1066 fs->fs_contigdirs[cg]--;
1068 ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode, 0,
1069 (allocfcn_t *)ffs_nodealloccg);
1072 error = ffs_vget(pvp->v_mount, ino, LK_EXCLUSIVE, vpp);
1074 error1 = ffs_vgetf(pvp->v_mount, ino, LK_EXCLUSIVE, vpp,
1076 ffs_vfree(pvp, ino, mode);
1081 ip->i_flag |= IN_MODIFIED;
1089 printf("mode = 0%o, inum = %ju, fs = %s\n",
1090 ip->i_mode, (uintmax_t)ip->i_number, fs->fs_fsmnt);
1091 panic("ffs_valloc: dup alloc");
1093 if (DIP(ip, i_blocks) && (fs->fs_flags & FS_UNCLEAN) == 0) { /* XXX */
1094 printf("free inode %s/%lu had %ld blocks\n",
1095 fs->fs_fsmnt, (u_long)ino, (long)DIP(ip, i_blocks));
1096 DIP_SET(ip, i_blocks, 0);
1099 DIP_SET(ip, i_flags, 0);
1101 * Set up a new generation number for this inode.
1103 if (ip->i_gen == 0 || ++ip->i_gen == 0)
1104 ip->i_gen = arc4random() / 2 + 1;
1105 DIP_SET(ip, i_gen, ip->i_gen);
1106 if (fs->fs_magic == FS_UFS2_MAGIC) {
1108 ip->i_din2->di_birthtime = ts.tv_sec;
1109 ip->i_din2->di_birthnsec = ts.tv_nsec;
1111 ufs_prepare_reclaim(*vpp);
1113 (*vpp)->v_vflag = 0;
1114 (*vpp)->v_type = VNON;
1115 if (fs->fs_magic == FS_UFS2_MAGIC)
1116 (*vpp)->v_op = &ffs_vnodeops2;
1118 (*vpp)->v_op = &ffs_vnodeops1;
1121 if (reclaimed == 0) {
1123 softdep_request_cleanup(fs, pvp, cred, FLUSH_INODES_WAIT);
1127 if (ppsratecheck(&lastfail, &curfail, 1)) {
1128 ffs_fserr(fs, pip->i_number, "out of inodes");
1129 uprintf("\n%s: create/symlink failed, no inodes free\n",
1136 * Find a cylinder group to place a directory.
1138 * The policy implemented by this algorithm is to allocate a
1139 * directory inode in the same cylinder group as its parent
1140 * directory, but also to reserve space for its files inodes
1141 * and data. Restrict the number of directories which may be
1142 * allocated one after another in the same cylinder group
1143 * without intervening allocation of files.
1145 * If we allocate a first level directory then force allocation
1146 * in another cylinder group.
1153 int cg, prefcg, dirsize, cgsize;
1154 u_int avgifree, avgbfree, avgndir, curdirsize;
1155 u_int minifree, minbfree, maxndir;
1156 u_int mincg, minndir;
1157 u_int maxcontigdirs;
1159 mtx_assert(UFS_MTX(pip->i_ump), MA_OWNED);
1162 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
1163 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1164 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
1167 * Force allocation in another cg if creating a first level dir.
1169 ASSERT_VOP_LOCKED(ITOV(pip), "ffs_dirpref");
1170 if (ITOV(pip)->v_vflag & VV_ROOT) {
1171 prefcg = arc4random() % fs->fs_ncg;
1173 minndir = fs->fs_ipg;
1174 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1175 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1176 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1177 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1179 minndir = fs->fs_cs(fs, cg).cs_ndir;
1181 for (cg = 0; cg < prefcg; cg++)
1182 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
1183 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
1184 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1186 minndir = fs->fs_cs(fs, cg).cs_ndir;
1188 return ((ino_t)(fs->fs_ipg * mincg));
1192 * Count various limits which used for
1193 * optimal allocation of a directory inode.
1195 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
1196 minifree = avgifree - avgifree / 4;
1199 minbfree = avgbfree - avgbfree / 4;
1202 cgsize = fs->fs_fsize * fs->fs_fpg;
1203 dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
1204 curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
1205 if (dirsize < curdirsize)
1206 dirsize = curdirsize;
1208 maxcontigdirs = 0; /* dirsize overflowed */
1210 maxcontigdirs = min((avgbfree * fs->fs_bsize) / dirsize, 255);
1211 if (fs->fs_avgfpdir > 0)
1212 maxcontigdirs = min(maxcontigdirs,
1213 fs->fs_ipg / fs->fs_avgfpdir);
1214 if (maxcontigdirs == 0)
1218 * Limit number of dirs in one cg and reserve space for
1219 * regular files, but only if we have no deficit in
1222 * We are trying to find a suitable cylinder group nearby
1223 * our preferred cylinder group to place a new directory.
1224 * We scan from our preferred cylinder group forward looking
1225 * for a cylinder group that meets our criterion. If we get
1226 * to the final cylinder group and do not find anything,
1227 * we start scanning forwards from the beginning of the
1228 * filesystem. While it might seem sensible to start scanning
1229 * backwards or even to alternate looking forward and backward,
1230 * this approach fails badly when the filesystem is nearly full.
1231 * Specifically, we first search all the areas that have no space
1232 * and finally try the one preceeding that. We repeat this on
1233 * every request and in the case of the final block end up
1234 * searching the entire filesystem. By jumping to the front
1235 * of the filesystem, our future forward searches always look
1236 * in new cylinder groups so finds every possible block after
1237 * one pass over the filesystem.
1239 prefcg = ino_to_cg(fs, pip->i_number);
1240 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1241 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1242 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1243 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1244 if (fs->fs_contigdirs[cg] < maxcontigdirs)
1245 return ((ino_t)(fs->fs_ipg * cg));
1247 for (cg = 0; cg < prefcg; cg++)
1248 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
1249 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
1250 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
1251 if (fs->fs_contigdirs[cg] < maxcontigdirs)
1252 return ((ino_t)(fs->fs_ipg * cg));
1255 * This is a backstop when we have deficit in space.
1257 for (cg = prefcg; cg < fs->fs_ncg; cg++)
1258 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1259 return ((ino_t)(fs->fs_ipg * cg));
1260 for (cg = 0; cg < prefcg; cg++)
1261 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
1263 return ((ino_t)(fs->fs_ipg * cg));
1267 * Select the desired position for the next block in a file. The file is
1268 * logically divided into sections. The first section is composed of the
1269 * direct blocks and the next fs_maxbpg blocks. Each additional section
1270 * contains fs_maxbpg blocks.
1272 * If no blocks have been allocated in the first section, the policy is to
1273 * request a block in the same cylinder group as the inode that describes
1274 * the file. The first indirect is allocated immediately following the last
1275 * direct block and the data blocks for the first indirect immediately
1278 * If no blocks have been allocated in any other section, the indirect
1279 * block(s) are allocated in the same cylinder group as its inode in an
1280 * area reserved immediately following the inode blocks. The policy for
1281 * the data blocks is to place them in a cylinder group with a greater than
1282 * average number of free blocks. An appropriate cylinder group is found
1283 * by using a rotor that sweeps the cylinder groups. When a new group of
1284 * blocks is needed, the sweep begins in the cylinder group following the
1285 * cylinder group from which the previous allocation was made. The sweep
1286 * continues until a cylinder group with greater than the average number
1287 * of free blocks is found. If the allocation is for the first block in an
1288 * indirect block or the previous block is a hole, then the information on
1289 * the previous allocation is unavailable; here a best guess is made based
1290 * on the logical block number being allocated.
1292 * If a section is already partially allocated, the policy is to
1293 * allocate blocks contiguously within the section if possible.
1296 ffs_blkpref_ufs1(ip, lbn, indx, bap)
1304 u_int avgbfree, startcg;
1307 KASSERT(indx <= 0 || bap != NULL, ("need non-NULL bap"));
1308 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1311 * Allocation of indirect blocks is indicated by passing negative
1312 * values in indx: -1 for single indirect, -2 for double indirect,
1313 * -3 for triple indirect. As noted below, we attempt to allocate
1314 * the first indirect inline with the file data. For all later
1315 * indirect blocks, the data is often allocated in other cylinder
1316 * groups. However to speed random file access and to speed up
1317 * fsck, the filesystem reserves the first fs_metaspace blocks
1318 * (typically half of fs_minfree) of the data area of each cylinder
1319 * group to hold these later indirect blocks.
1321 inocg = ino_to_cg(fs, ip->i_number);
1324 * Our preference for indirect blocks is the zone at the
1325 * beginning of the inode's cylinder group data area that
1326 * we try to reserve for indirect blocks.
1328 pref = cgmeta(fs, inocg);
1330 * If we are allocating the first indirect block, try to
1331 * place it immediately following the last direct block.
1333 if (indx == -1 && lbn < NDADDR + NINDIR(fs) &&
1334 ip->i_din1->di_db[NDADDR - 1] != 0)
1335 pref = ip->i_din1->di_db[NDADDR - 1] + fs->fs_frag;
1339 * If we are allocating the first data block in the first indirect
1340 * block and the indirect has been allocated in the data block area,
1341 * try to place it immediately following the indirect block.
1343 if (lbn == NDADDR) {
1344 pref = ip->i_din1->di_ib[0];
1345 if (pref != 0 && pref >= cgdata(fs, inocg) &&
1346 pref < cgbase(fs, inocg + 1))
1347 return (pref + fs->fs_frag);
1350 * If we are at the beginning of a file, or we have already allocated
1351 * the maximum number of blocks per cylinder group, or we do not
1352 * have a block allocated immediately preceeding us, then we need
1353 * to decide where to start allocating new blocks.
1355 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1357 * If we are allocating a directory data block, we want
1358 * to place it in the metadata area.
1360 if ((ip->i_mode & IFMT) == IFDIR)
1361 return (cgmeta(fs, inocg));
1363 * Until we fill all the direct and all the first indirect's
1364 * blocks, we try to allocate in the data area of the inode's
1367 if (lbn < NDADDR + NINDIR(fs))
1368 return (cgdata(fs, inocg));
1370 * Find a cylinder with greater than average number of
1371 * unused data blocks.
1373 if (indx == 0 || bap[indx - 1] == 0)
1374 startcg = inocg + lbn / fs->fs_maxbpg;
1376 startcg = dtog(fs, bap[indx - 1]) + 1;
1377 startcg %= fs->fs_ncg;
1378 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1379 for (cg = startcg; cg < fs->fs_ncg; cg++)
1380 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1381 fs->fs_cgrotor = cg;
1382 return (cgdata(fs, cg));
1384 for (cg = 0; cg <= startcg; cg++)
1385 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1386 fs->fs_cgrotor = cg;
1387 return (cgdata(fs, cg));
1392 * Otherwise, we just always try to lay things out contiguously.
1394 return (bap[indx - 1] + fs->fs_frag);
1398 * Same as above, but for UFS2
1401 ffs_blkpref_ufs2(ip, lbn, indx, bap)
1409 u_int avgbfree, startcg;
1412 KASSERT(indx <= 0 || bap != NULL, ("need non-NULL bap"));
1413 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1416 * Allocation of indirect blocks is indicated by passing negative
1417 * values in indx: -1 for single indirect, -2 for double indirect,
1418 * -3 for triple indirect. As noted below, we attempt to allocate
1419 * the first indirect inline with the file data. For all later
1420 * indirect blocks, the data is often allocated in other cylinder
1421 * groups. However to speed random file access and to speed up
1422 * fsck, the filesystem reserves the first fs_metaspace blocks
1423 * (typically half of fs_minfree) of the data area of each cylinder
1424 * group to hold these later indirect blocks.
1426 inocg = ino_to_cg(fs, ip->i_number);
1429 * Our preference for indirect blocks is the zone at the
1430 * beginning of the inode's cylinder group data area that
1431 * we try to reserve for indirect blocks.
1433 pref = cgmeta(fs, inocg);
1435 * If we are allocating the first indirect block, try to
1436 * place it immediately following the last direct block.
1438 if (indx == -1 && lbn < NDADDR + NINDIR(fs) &&
1439 ip->i_din2->di_db[NDADDR - 1] != 0)
1440 pref = ip->i_din2->di_db[NDADDR - 1] + fs->fs_frag;
1444 * If we are allocating the first data block in the first indirect
1445 * block and the indirect has been allocated in the data block area,
1446 * try to place it immediately following the indirect block.
1448 if (lbn == NDADDR) {
1449 pref = ip->i_din2->di_ib[0];
1450 if (pref != 0 && pref >= cgdata(fs, inocg) &&
1451 pref < cgbase(fs, inocg + 1))
1452 return (pref + fs->fs_frag);
1455 * If we are at the beginning of a file, or we have already allocated
1456 * the maximum number of blocks per cylinder group, or we do not
1457 * have a block allocated immediately preceeding us, then we need
1458 * to decide where to start allocating new blocks.
1460 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1462 * If we are allocating a directory data block, we want
1463 * to place it in the metadata area.
1465 if ((ip->i_mode & IFMT) == IFDIR)
1466 return (cgmeta(fs, inocg));
1468 * Until we fill all the direct and all the first indirect's
1469 * blocks, we try to allocate in the data area of the inode's
1472 if (lbn < NDADDR + NINDIR(fs))
1473 return (cgdata(fs, inocg));
1475 * Find a cylinder with greater than average number of
1476 * unused data blocks.
1478 if (indx == 0 || bap[indx - 1] == 0)
1479 startcg = inocg + lbn / fs->fs_maxbpg;
1481 startcg = dtog(fs, bap[indx - 1]) + 1;
1482 startcg %= fs->fs_ncg;
1483 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1484 for (cg = startcg; cg < fs->fs_ncg; cg++)
1485 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1486 fs->fs_cgrotor = cg;
1487 return (cgdata(fs, cg));
1489 for (cg = 0; cg <= startcg; cg++)
1490 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1491 fs->fs_cgrotor = cg;
1492 return (cgdata(fs, cg));
1497 * Otherwise, we just always try to lay things out contiguously.
1499 return (bap[indx - 1] + fs->fs_frag);
1503 * Implement the cylinder overflow algorithm.
1505 * The policy implemented by this algorithm is:
1506 * 1) allocate the block in its requested cylinder group.
1507 * 2) quadradically rehash on the cylinder group number.
1508 * 3) brute force search for a free block.
1510 * Must be called with the UFS lock held. Will release the lock on success
1511 * and return with it held on failure.
1515 ffs_hashalloc(ip, cg, pref, size, rsize, allocator)
1519 int size; /* Search size for data blocks, mode for inodes */
1520 int rsize; /* Real allocated size. */
1521 allocfcn_t *allocator;
1524 ufs2_daddr_t result;
1527 mtx_assert(UFS_MTX(ip->i_ump), MA_OWNED);
1529 if (ITOV(ip)->v_mount->mnt_kern_flag & MNTK_SUSPENDED)
1530 panic("ffs_hashalloc: allocation on suspended filesystem");
1534 * 1: preferred cylinder group
1536 result = (*allocator)(ip, cg, pref, size, rsize);
1540 * 2: quadratic rehash
1542 for (i = 1; i < fs->fs_ncg; i *= 2) {
1544 if (cg >= fs->fs_ncg)
1546 result = (*allocator)(ip, cg, 0, size, rsize);
1551 * 3: brute force search
1552 * Note that we start at i == 2, since 0 was checked initially,
1553 * and 1 is always checked in the quadratic rehash.
1555 cg = (icg + 2) % fs->fs_ncg;
1556 for (i = 2; i < fs->fs_ncg; i++) {
1557 result = (*allocator)(ip, cg, 0, size, rsize);
1561 if (cg == fs->fs_ncg)
1568 * Determine whether a fragment can be extended.
1570 * Check to see if the necessary fragments are available, and
1571 * if they are, allocate them.
1574 ffs_fragextend(ip, cg, bprev, osize, nsize)
1583 struct ufsmount *ump;
1592 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
1594 frags = numfrags(fs, nsize);
1595 bbase = fragnum(fs, bprev);
1596 if (bbase > fragnum(fs, (bprev + frags - 1))) {
1597 /* cannot extend across a block boundary */
1601 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1602 (int)fs->fs_cgsize, NOCRED, &bp);
1605 cgp = (struct cg *)bp->b_data;
1606 if (!cg_chkmagic(cgp))
1608 bp->b_xflags |= BX_BKGRDWRITE;
1609 cgp->cg_old_time = cgp->cg_time = time_second;
1610 bno = dtogd(fs, bprev);
1611 blksfree = cg_blksfree(cgp);
1612 for (i = numfrags(fs, osize); i < frags; i++)
1613 if (isclr(blksfree, bno + i))
1616 * the current fragment can be extended
1617 * deduct the count on fragment being extended into
1618 * increase the count on the remaining fragment (if any)
1619 * allocate the extended piece
1621 for (i = frags; i < fs->fs_frag - bbase; i++)
1622 if (isclr(blksfree, bno + i))
1624 cgp->cg_frsum[i - numfrags(fs, osize)]--;
1626 cgp->cg_frsum[i - frags]++;
1627 for (i = numfrags(fs, osize), nffree = 0; i < frags; i++) {
1628 clrbit(blksfree, bno + i);
1629 cgp->cg_cs.cs_nffree--;
1633 fs->fs_cstotal.cs_nffree -= nffree;
1634 fs->fs_cs(fs, cg).cs_nffree -= nffree;
1636 ACTIVECLEAR(fs, cg);
1638 if (DOINGSOFTDEP(ITOV(ip)))
1639 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), bprev,
1640 frags, numfrags(fs, osize));
1652 * Determine whether a block can be allocated.
1654 * Check to see if a block of the appropriate size is available,
1655 * and if it is, allocate it.
1658 ffs_alloccg(ip, cg, bpref, size, rsize)
1668 struct ufsmount *ump;
1671 int i, allocsiz, error, frags;
1676 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1679 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1680 (int)fs->fs_cgsize, NOCRED, &bp);
1683 cgp = (struct cg *)bp->b_data;
1684 if (!cg_chkmagic(cgp) ||
1685 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize))
1687 bp->b_xflags |= BX_BKGRDWRITE;
1688 cgp->cg_old_time = cgp->cg_time = time_second;
1689 if (size == fs->fs_bsize) {
1691 blkno = ffs_alloccgblk(ip, bp, bpref, rsize);
1692 ACTIVECLEAR(fs, cg);
1698 * check to see if any fragments are already available
1699 * allocsiz is the size which will be allocated, hacking
1700 * it down to a smaller size if necessary
1702 blksfree = cg_blksfree(cgp);
1703 frags = numfrags(fs, size);
1704 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1705 if (cgp->cg_frsum[allocsiz] != 0)
1707 if (allocsiz == fs->fs_frag) {
1709 * no fragments were available, so a block will be
1710 * allocated, and hacked up
1712 if (cgp->cg_cs.cs_nbfree == 0)
1715 blkno = ffs_alloccgblk(ip, bp, bpref, rsize);
1716 ACTIVECLEAR(fs, cg);
1721 KASSERT(size == rsize,
1722 ("ffs_alloccg: size(%d) != rsize(%d)", size, rsize));
1723 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1726 for (i = 0; i < frags; i++)
1727 clrbit(blksfree, bno + i);
1728 cgp->cg_cs.cs_nffree -= frags;
1729 cgp->cg_frsum[allocsiz]--;
1730 if (frags != allocsiz)
1731 cgp->cg_frsum[allocsiz - frags]++;
1733 fs->fs_cstotal.cs_nffree -= frags;
1734 fs->fs_cs(fs, cg).cs_nffree -= frags;
1736 blkno = cgbase(fs, cg) + bno;
1737 ACTIVECLEAR(fs, cg);
1739 if (DOINGSOFTDEP(ITOV(ip)))
1740 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno, frags, 0);
1751 * Allocate a block in a cylinder group.
1753 * This algorithm implements the following policy:
1754 * 1) allocate the requested block.
1755 * 2) allocate a rotationally optimal block in the same cylinder.
1756 * 3) allocate the next available block on the block rotor for the
1757 * specified cylinder group.
1758 * Note that this routine only allocates fs_bsize blocks; these
1759 * blocks may be fragmented by the routine that allocates them.
1762 ffs_alloccgblk(ip, bp, bpref, size)
1770 struct ufsmount *ump;
1778 mtx_assert(UFS_MTX(ump), MA_OWNED);
1779 cgp = (struct cg *)bp->b_data;
1780 blksfree = cg_blksfree(cgp);
1782 bpref = cgbase(fs, cgp->cg_cgx) + cgp->cg_rotor + fs->fs_frag;
1783 } else if ((cgbpref = dtog(fs, bpref)) != cgp->cg_cgx) {
1784 /* map bpref to correct zone in this cg */
1785 if (bpref < cgdata(fs, cgbpref))
1786 bpref = cgmeta(fs, cgp->cg_cgx);
1788 bpref = cgdata(fs, cgp->cg_cgx);
1791 * if the requested block is available, use it
1793 bno = dtogd(fs, blknum(fs, bpref));
1794 if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
1797 * Take the next available block in this cylinder group.
1799 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1802 /* Update cg_rotor only if allocated from the data zone */
1803 if (bno >= dtogd(fs, cgdata(fs, cgp->cg_cgx)))
1804 cgp->cg_rotor = bno;
1806 blkno = fragstoblks(fs, bno);
1807 ffs_clrblock(fs, blksfree, (long)blkno);
1808 ffs_clusteracct(fs, cgp, blkno, -1);
1809 cgp->cg_cs.cs_nbfree--;
1810 fs->fs_cstotal.cs_nbfree--;
1811 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1813 blkno = cgbase(fs, cgp->cg_cgx) + bno;
1815 * If the caller didn't want the whole block free the frags here.
1817 size = numfrags(fs, size);
1818 if (size != fs->fs_frag) {
1819 bno = dtogd(fs, blkno);
1820 for (i = size; i < fs->fs_frag; i++)
1821 setbit(blksfree, bno + i);
1822 i = fs->fs_frag - size;
1823 cgp->cg_cs.cs_nffree += i;
1824 fs->fs_cstotal.cs_nffree += i;
1825 fs->fs_cs(fs, cgp->cg_cgx).cs_nffree += i;
1831 if (DOINGSOFTDEP(ITOV(ip)))
1832 softdep_setup_blkmapdep(bp, UFSTOVFS(ump), blkno,
1839 * Determine whether a cluster can be allocated.
1841 * We do not currently check for optimal rotational layout if there
1842 * are multiple choices in the same cylinder group. Instead we just
1843 * take the first one that we find following bpref.
1846 ffs_clusteralloc(ip, cg, bpref, len)
1855 struct ufsmount *ump;
1856 int i, run, bit, map, got;
1864 if (fs->fs_maxcluster[cg] < len)
1867 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1870 cgp = (struct cg *)bp->b_data;
1871 if (!cg_chkmagic(cgp))
1873 bp->b_xflags |= BX_BKGRDWRITE;
1875 * Check to see if a cluster of the needed size (or bigger) is
1876 * available in this cylinder group.
1878 lp = &cg_clustersum(cgp)[len];
1879 for (i = len; i <= fs->fs_contigsumsize; i++)
1882 if (i > fs->fs_contigsumsize) {
1884 * This is the first time looking for a cluster in this
1885 * cylinder group. Update the cluster summary information
1886 * to reflect the true maximum sized cluster so that
1887 * future cluster allocation requests can avoid reading
1888 * the cylinder group map only to find no clusters.
1890 lp = &cg_clustersum(cgp)[len - 1];
1891 for (i = len - 1; i > 0; i--)
1895 fs->fs_maxcluster[cg] = i;
1899 * Search the cluster map to find a big enough cluster.
1900 * We take the first one that we find, even if it is larger
1901 * than we need as we prefer to get one close to the previous
1902 * block allocation. We do not search before the current
1903 * preference point as we do not want to allocate a block
1904 * that is allocated before the previous one (as we will
1905 * then have to wait for another pass of the elevator
1906 * algorithm before it will be read). We prefer to fail and
1907 * be recalled to try an allocation in the next cylinder group.
1909 if (dtog(fs, bpref) != cg)
1910 bpref = cgdata(fs, cg);
1912 bpref = blknum(fs, bpref);
1913 bpref = fragstoblks(fs, dtogd(fs, bpref));
1914 mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1916 bit = 1 << (bpref % NBBY);
1917 for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
1918 if ((map & bit) == 0) {
1925 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1932 if (got >= cgp->cg_nclusterblks)
1935 * Allocate the cluster that we have found.
1937 blksfree = cg_blksfree(cgp);
1938 for (i = 1; i <= len; i++)
1939 if (!ffs_isblock(fs, blksfree, got - run + i))
1940 panic("ffs_clusteralloc: map mismatch");
1941 bno = cgbase(fs, cg) + blkstofrags(fs, got - run + 1);
1942 if (dtog(fs, bno) != cg)
1943 panic("ffs_clusteralloc: allocated out of group");
1944 len = blkstofrags(fs, len);
1946 for (i = 0; i < len; i += fs->fs_frag)
1947 if (ffs_alloccgblk(ip, bp, bno + i, fs->fs_bsize) != bno + i)
1948 panic("ffs_clusteralloc: lost block");
1949 ACTIVECLEAR(fs, cg);
1961 static inline struct buf *
1962 getinobuf(struct inode *ip, u_int cg, u_int32_t cginoblk, int gbflags)
1967 return (getblk(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs,
1968 cg * fs->fs_ipg + cginoblk)), (int)fs->fs_bsize, 0, 0,
1973 * Determine whether an inode can be allocated.
1975 * Check to see if an inode is available, and if it is,
1976 * allocate it using the following policy:
1977 * 1) allocate the requested inode.
1978 * 2) allocate the next available inode after the requested
1979 * inode in the specified cylinder group.
1982 ffs_nodealloccg(ip, cg, ipref, mode, unused)
1991 struct buf *bp, *ibp;
1992 struct ufsmount *ump;
1993 u_int8_t *inosused, *loc;
1994 struct ufs2_dinode *dp2;
1995 int error, start, len, i;
1996 u_int32_t old_initediblk;
2001 if (fs->fs_cs(fs, cg).cs_nifree == 0)
2004 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
2005 (int)fs->fs_cgsize, NOCRED, &bp);
2011 cgp = (struct cg *)bp->b_data;
2013 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
2018 bp->b_xflags |= BX_BKGRDWRITE;
2019 inosused = cg_inosused(cgp);
2021 ipref %= fs->fs_ipg;
2022 if (isclr(inosused, ipref))
2025 start = cgp->cg_irotor / NBBY;
2026 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
2027 loc = memcchr(&inosused[start], 0xff, len);
2031 loc = memcchr(&inosused[start], 0xff, len);
2033 printf("cg = %d, irotor = %ld, fs = %s\n",
2034 cg, (long)cgp->cg_irotor, fs->fs_fsmnt);
2035 panic("ffs_nodealloccg: map corrupted");
2039 ipref = (loc - inosused) * NBBY + ffs(~*loc) - 1;
2042 * Check to see if we need to initialize more inodes.
2044 if (fs->fs_magic == FS_UFS2_MAGIC &&
2045 ipref + INOPB(fs) > cgp->cg_initediblk &&
2046 cgp->cg_initediblk < cgp->cg_niblk) {
2047 old_initediblk = cgp->cg_initediblk;
2050 * Free the cylinder group lock before writing the
2051 * initialized inode block. Entering the
2052 * babarrierwrite() with the cylinder group lock
2053 * causes lock order violation between the lock and
2056 * Another thread can decide to initialize the same
2057 * inode block, but whichever thread first gets the
2058 * cylinder group lock after writing the newly
2059 * allocated inode block will update it and the other
2060 * will realize that it has lost and leave the
2061 * cylinder group unchanged.
2063 ibp = getinobuf(ip, cg, old_initediblk, GB_LOCK_NOWAIT);
2067 * The inode block buffer is already owned by
2068 * another thread, which must initialize it.
2069 * Wait on the buffer to allow another thread
2070 * to finish the updates, with dropped cg
2071 * buffer lock, then retry.
2073 ibp = getinobuf(ip, cg, old_initediblk, 0);
2078 bzero(ibp->b_data, (int)fs->fs_bsize);
2079 dp2 = (struct ufs2_dinode *)(ibp->b_data);
2080 for (i = 0; i < INOPB(fs); i++) {
2081 dp2->di_gen = arc4random() / 2 + 1;
2085 * Rather than adding a soft updates dependency to ensure
2086 * that the new inode block is written before it is claimed
2087 * by the cylinder group map, we just do a barrier write
2088 * here. The barrier write will ensure that the inode block
2089 * gets written before the updated cylinder group map can be
2090 * written. The barrier write should only slow down bulk
2091 * loading of newly created filesystems.
2093 babarrierwrite(ibp);
2096 * After the inode block is written, try to update the
2097 * cg initediblk pointer. If another thread beat us
2098 * to it, then leave it unchanged as the other thread
2099 * has already set it correctly.
2101 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
2102 (int)fs->fs_cgsize, NOCRED, &bp);
2104 ACTIVECLEAR(fs, cg);
2110 cgp = (struct cg *)bp->b_data;
2111 if (cgp->cg_initediblk == old_initediblk)
2112 cgp->cg_initediblk += INOPB(fs);
2115 cgp->cg_old_time = cgp->cg_time = time_second;
2116 cgp->cg_irotor = ipref;
2118 ACTIVECLEAR(fs, cg);
2119 setbit(inosused, ipref);
2120 cgp->cg_cs.cs_nifree--;
2121 fs->fs_cstotal.cs_nifree--;
2122 fs->fs_cs(fs, cg).cs_nifree--;
2124 if ((mode & IFMT) == IFDIR) {
2125 cgp->cg_cs.cs_ndir++;
2126 fs->fs_cstotal.cs_ndir++;
2127 fs->fs_cs(fs, cg).cs_ndir++;
2130 if (DOINGSOFTDEP(ITOV(ip)))
2131 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref, mode);
2133 return ((ino_t)(cg * fs->fs_ipg + ipref));
2137 * Free a block or fragment.
2139 * The specified block or fragment is placed back in the
2140 * free map. If a fragment is deallocated, a possible
2141 * block reassembly is checked.
2144 ffs_blkfree_cg(ump, fs, devvp, bno, size, inum, dephd)
2145 struct ufsmount *ump;
2147 struct vnode *devvp;
2151 struct workhead *dephd;
2156 ufs1_daddr_t fragno, cgbno;
2157 ufs2_daddr_t cgblkno;
2158 int i, blk, frags, bbase;
2164 if (devvp->v_type == VREG) {
2165 /* devvp is a snapshot */
2166 dev = VTOI(devvp)->i_devvp->v_rdev;
2167 cgblkno = fragstoblks(fs, cgtod(fs, cg));
2169 /* devvp is a normal disk device */
2170 dev = devvp->v_rdev;
2171 cgblkno = fsbtodb(fs, cgtod(fs, cg));
2172 ASSERT_VOP_LOCKED(devvp, "ffs_blkfree_cg");
2175 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
2176 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
2177 printf("dev=%s, bno = %jd, bsize = %ld, size = %ld, fs = %s\n",
2178 devtoname(dev), (intmax_t)bno, (long)fs->fs_bsize,
2179 size, fs->fs_fsmnt);
2180 panic("ffs_blkfree_cg: bad size");
2183 if ((u_int)bno >= fs->fs_size) {
2184 printf("bad block %jd, ino %lu\n", (intmax_t)bno,
2186 ffs_fserr(fs, inum, "bad block");
2189 if (bread(devvp, cgblkno, (int)fs->fs_cgsize, NOCRED, &bp)) {
2193 cgp = (struct cg *)bp->b_data;
2194 if (!cg_chkmagic(cgp)) {
2198 bp->b_xflags |= BX_BKGRDWRITE;
2199 cgp->cg_old_time = cgp->cg_time = time_second;
2200 cgbno = dtogd(fs, bno);
2201 blksfree = cg_blksfree(cgp);
2203 if (size == fs->fs_bsize) {
2204 fragno = fragstoblks(fs, cgbno);
2205 if (!ffs_isfreeblock(fs, blksfree, fragno)) {
2206 if (devvp->v_type == VREG) {
2208 /* devvp is a snapshot */
2212 printf("dev = %s, block = %jd, fs = %s\n",
2213 devtoname(dev), (intmax_t)bno, fs->fs_fsmnt);
2214 panic("ffs_blkfree_cg: freeing free block");
2216 ffs_setblock(fs, blksfree, fragno);
2217 ffs_clusteracct(fs, cgp, fragno, 1);
2218 cgp->cg_cs.cs_nbfree++;
2219 fs->fs_cstotal.cs_nbfree++;
2220 fs->fs_cs(fs, cg).cs_nbfree++;
2222 bbase = cgbno - fragnum(fs, cgbno);
2224 * decrement the counts associated with the old frags
2226 blk = blkmap(fs, blksfree, bbase);
2227 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
2229 * deallocate the fragment
2231 frags = numfrags(fs, size);
2232 for (i = 0; i < frags; i++) {
2233 if (isset(blksfree, cgbno + i)) {
2234 printf("dev = %s, block = %jd, fs = %s\n",
2235 devtoname(dev), (intmax_t)(bno + i),
2237 panic("ffs_blkfree_cg: freeing free frag");
2239 setbit(blksfree, cgbno + i);
2241 cgp->cg_cs.cs_nffree += i;
2242 fs->fs_cstotal.cs_nffree += i;
2243 fs->fs_cs(fs, cg).cs_nffree += i;
2245 * add back in counts associated with the new frags
2247 blk = blkmap(fs, blksfree, bbase);
2248 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
2250 * if a complete block has been reassembled, account for it
2252 fragno = fragstoblks(fs, bbase);
2253 if (ffs_isblock(fs, blksfree, fragno)) {
2254 cgp->cg_cs.cs_nffree -= fs->fs_frag;
2255 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
2256 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
2257 ffs_clusteracct(fs, cgp, fragno, 1);
2258 cgp->cg_cs.cs_nbfree++;
2259 fs->fs_cstotal.cs_nbfree++;
2260 fs->fs_cs(fs, cg).cs_nbfree++;
2264 ACTIVECLEAR(fs, cg);
2267 if (MOUNTEDSOFTDEP(mp) && devvp->v_type != VREG)
2268 softdep_setup_blkfree(UFSTOVFS(ump), bp, bno,
2269 numfrags(fs, size), dephd);
2273 TASKQUEUE_DEFINE_THREAD(ffs_trim);
2275 struct ffs_blkfree_trim_params {
2277 struct ufsmount *ump;
2278 struct vnode *devvp;
2282 struct workhead *pdephd;
2283 struct workhead dephd;
2287 ffs_blkfree_trim_task(ctx, pending)
2291 struct ffs_blkfree_trim_params *tp;
2294 ffs_blkfree_cg(tp->ump, tp->ump->um_fs, tp->devvp, tp->bno, tp->size,
2295 tp->inum, tp->pdephd);
2296 vn_finished_secondary_write(UFSTOVFS(tp->ump));
2301 ffs_blkfree_trim_completed(bip)
2304 struct ffs_blkfree_trim_params *tp;
2306 tp = bip->bio_caller2;
2308 TASK_INIT(&tp->task, 0, ffs_blkfree_trim_task, tp);
2309 taskqueue_enqueue(taskqueue_ffs_trim, &tp->task);
2313 ffs_blkfree(ump, fs, devvp, bno, size, inum, vtype, dephd)
2314 struct ufsmount *ump;
2316 struct vnode *devvp;
2321 struct workhead *dephd;
2325 struct ffs_blkfree_trim_params *tp;
2328 * Check to see if a snapshot wants to claim the block.
2329 * Check that devvp is a normal disk device, not a snapshot,
2330 * it has a snapshot(s) associated with it, and one of the
2331 * snapshots wants to claim the block.
2333 if (devvp->v_type != VREG &&
2334 (devvp->v_vflag & VV_COPYONWRITE) &&
2335 ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, dephd)) {
2339 * Nothing to delay if TRIM is disabled, or the operation is
2340 * performed on the snapshot.
2342 if (!ump->um_candelete || devvp->v_type == VREG) {
2343 ffs_blkfree_cg(ump, fs, devvp, bno, size, inum, dephd);
2348 * Postpone the set of the free bit in the cg bitmap until the
2349 * BIO_DELETE is completed. Otherwise, due to disk queue
2350 * reordering, TRIM might be issued after we reuse the block
2351 * and write some new data into it.
2353 tp = malloc(sizeof(struct ffs_blkfree_trim_params), M_TEMP, M_WAITOK);
2359 if (dephd != NULL) {
2360 LIST_INIT(&tp->dephd);
2361 LIST_SWAP(dephd, &tp->dephd, worklist, wk_list);
2362 tp->pdephd = &tp->dephd;
2366 bip = g_alloc_bio();
2367 bip->bio_cmd = BIO_DELETE;
2368 bip->bio_offset = dbtob(fsbtodb(fs, bno));
2369 bip->bio_done = ffs_blkfree_trim_completed;
2370 bip->bio_length = size;
2371 bip->bio_caller2 = tp;
2374 vn_start_secondary_write(NULL, &mp, 0);
2375 g_io_request(bip, (struct g_consumer *)devvp->v_bufobj.bo_private);
2380 * Verify allocation of a block or fragment. Returns true if block or
2381 * fragment is allocated, false if it is free.
2384 ffs_checkblk(ip, bno, size)
2393 int i, error, frags, free;
2397 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
2398 printf("bsize = %ld, size = %ld, fs = %s\n",
2399 (long)fs->fs_bsize, size, fs->fs_fsmnt);
2400 panic("ffs_checkblk: bad size");
2402 if ((u_int)bno >= fs->fs_size)
2403 panic("ffs_checkblk: bad block %jd", (intmax_t)bno);
2404 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
2405 (int)fs->fs_cgsize, NOCRED, &bp);
2407 panic("ffs_checkblk: cg bread failed");
2408 cgp = (struct cg *)bp->b_data;
2409 if (!cg_chkmagic(cgp))
2410 panic("ffs_checkblk: cg magic mismatch");
2411 bp->b_xflags |= BX_BKGRDWRITE;
2412 blksfree = cg_blksfree(cgp);
2413 cgbno = dtogd(fs, bno);
2414 if (size == fs->fs_bsize) {
2415 free = ffs_isblock(fs, blksfree, fragstoblks(fs, cgbno));
2417 frags = numfrags(fs, size);
2418 for (free = 0, i = 0; i < frags; i++)
2419 if (isset(blksfree, cgbno + i))
2421 if (free != 0 && free != frags)
2422 panic("ffs_checkblk: partially free fragment");
2427 #endif /* INVARIANTS */
2433 ffs_vfree(pvp, ino, mode)
2440 if (DOINGSOFTDEP(pvp)) {
2441 softdep_freefile(pvp, ino, mode);
2445 return (ffs_freefile(ip->i_ump, ip->i_fs, ip->i_devvp, ino, mode,
2450 * Do the actual free operation.
2451 * The specified inode is placed back in the free map.
2454 ffs_freefile(ump, fs, devvp, ino, mode, wkhd)
2455 struct ufsmount *ump;
2457 struct vnode *devvp;
2460 struct workhead *wkhd;
2470 cg = ino_to_cg(fs, ino);
2471 if (devvp->v_type == VREG) {
2472 /* devvp is a snapshot */
2473 dev = VTOI(devvp)->i_devvp->v_rdev;
2474 cgbno = fragstoblks(fs, cgtod(fs, cg));
2476 /* devvp is a normal disk device */
2477 dev = devvp->v_rdev;
2478 cgbno = fsbtodb(fs, cgtod(fs, cg));
2480 if (ino >= fs->fs_ipg * fs->fs_ncg)
2481 panic("ffs_freefile: range: dev = %s, ino = %ju, fs = %s",
2482 devtoname(dev), (uintmax_t)ino, fs->fs_fsmnt);
2483 if ((error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp))) {
2487 cgp = (struct cg *)bp->b_data;
2488 if (!cg_chkmagic(cgp)) {
2492 bp->b_xflags |= BX_BKGRDWRITE;
2493 cgp->cg_old_time = cgp->cg_time = time_second;
2494 inosused = cg_inosused(cgp);
2496 if (isclr(inosused, ino)) {
2497 printf("dev = %s, ino = %ju, fs = %s\n", devtoname(dev),
2498 (uintmax_t)(ino + cg * fs->fs_ipg), fs->fs_fsmnt);
2499 if (fs->fs_ronly == 0)
2500 panic("ffs_freefile: freeing free inode");
2502 clrbit(inosused, ino);
2503 if (ino < cgp->cg_irotor)
2504 cgp->cg_irotor = ino;
2505 cgp->cg_cs.cs_nifree++;
2507 fs->fs_cstotal.cs_nifree++;
2508 fs->fs_cs(fs, cg).cs_nifree++;
2509 if ((mode & IFMT) == IFDIR) {
2510 cgp->cg_cs.cs_ndir--;
2511 fs->fs_cstotal.cs_ndir--;
2512 fs->fs_cs(fs, cg).cs_ndir--;
2515 ACTIVECLEAR(fs, cg);
2517 if (MOUNTEDSOFTDEP(UFSTOVFS(ump)) && devvp->v_type != VREG)
2518 softdep_setup_inofree(UFSTOVFS(ump), bp,
2519 ino + cg * fs->fs_ipg, wkhd);
2525 * Check to see if a file is free.
2528 ffs_checkfreefile(fs, devvp, ino)
2530 struct vnode *devvp;
2540 cg = ino_to_cg(fs, ino);
2541 if (devvp->v_type == VREG) {
2542 /* devvp is a snapshot */
2543 cgbno = fragstoblks(fs, cgtod(fs, cg));
2545 /* devvp is a normal disk device */
2546 cgbno = fsbtodb(fs, cgtod(fs, cg));
2548 if (ino >= fs->fs_ipg * fs->fs_ncg)
2550 if (bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp)) {
2554 cgp = (struct cg *)bp->b_data;
2555 if (!cg_chkmagic(cgp)) {
2559 inosused = cg_inosused(cgp);
2561 ret = isclr(inosused, ino);
2567 * Find a block of the specified size in the specified cylinder group.
2569 * It is a panic if a request is made to find a block if none are
2573 ffs_mapsearch(fs, cgp, bpref, allocsiz)
2580 int start, len, loc, i;
2581 int blk, field, subfield, pos;
2585 * find the fragment by searching through the free block
2586 * map for an appropriate bit pattern
2589 start = dtogd(fs, bpref) / NBBY;
2591 start = cgp->cg_frotor / NBBY;
2592 blksfree = cg_blksfree(cgp);
2593 len = howmany(fs->fs_fpg, NBBY) - start;
2594 loc = scanc((u_int)len, (u_char *)&blksfree[start],
2595 fragtbl[fs->fs_frag],
2596 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2600 loc = scanc((u_int)len, (u_char *)&blksfree[0],
2601 fragtbl[fs->fs_frag],
2602 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
2604 printf("start = %d, len = %d, fs = %s\n",
2605 start, len, fs->fs_fsmnt);
2606 panic("ffs_alloccg: map corrupted");
2610 bno = (start + len - loc) * NBBY;
2611 cgp->cg_frotor = bno;
2613 * found the byte in the map
2614 * sift through the bits to find the selected frag
2616 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
2617 blk = blkmap(fs, blksfree, bno);
2619 field = around[allocsiz];
2620 subfield = inside[allocsiz];
2621 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
2622 if ((blk & field) == subfield)
2628 printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt);
2629 panic("ffs_alloccg: block not in map");
2634 * Fserr prints the name of a filesystem with an error diagnostic.
2636 * The form of the error message is:
2640 ffs_fserr(fs, inum, cp)
2645 struct thread *td = curthread; /* XXX */
2646 struct proc *p = td->td_proc;
2648 log(LOG_ERR, "pid %d (%s), uid %d inumber %ju on %s: %s\n",
2649 p->p_pid, p->p_comm, td->td_ucred->cr_uid, (uintmax_t)inum,
2654 * This function provides the capability for the fsck program to
2655 * update an active filesystem. Fourteen operations are provided:
2657 * adjrefcnt(inode, amt) - adjusts the reference count on the
2658 * specified inode by the specified amount. Under normal
2659 * operation the count should always go down. Decrementing
2660 * the count to zero will cause the inode to be freed.
2661 * adjblkcnt(inode, amt) - adjust the number of blocks used by the
2662 * inode by the specified amount.
2663 * adjndir, adjbfree, adjifree, adjffree, adjnumclusters(amt) -
2664 * adjust the superblock summary.
2665 * freedirs(inode, count) - directory inodes [inode..inode + count - 1]
2666 * are marked as free. Inodes should never have to be marked
2668 * freefiles(inode, count) - file inodes [inode..inode + count - 1]
2669 * are marked as free. Inodes should never have to be marked
2671 * freeblks(blockno, size) - blocks [blockno..blockno + size - 1]
2672 * are marked as free. Blocks should never have to be marked
2674 * setflags(flags, set/clear) - the fs_flags field has the specified
2675 * flags set (second parameter +1) or cleared (second parameter -1).
2676 * setcwd(dirinode) - set the current directory to dirinode in the
2677 * filesystem associated with the snapshot.
2678 * setdotdot(oldvalue, newvalue) - Verify that the inode number for ".."
2679 * in the current directory is oldvalue then change it to newvalue.
2680 * unlink(nameptr, oldvalue) - Verify that the inode number associated
2681 * with nameptr in the current directory is oldvalue then unlink it.
2683 * The following functions may only be used on a quiescent filesystem
2684 * by the soft updates journal. They are not safe to be run on an active
2687 * setinode(inode, dip) - the specified disk inode is replaced with the
2688 * contents pointed to by dip.
2689 * setbufoutput(fd, flags) - output associated with the specified file
2690 * descriptor (which must reference the character device supporting
2691 * the filesystem) switches from using physio to running through the
2692 * buffer cache when flags is set to 1. The descriptor reverts to
2693 * physio for output when flags is set to zero.
2696 static int sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS);
2698 SYSCTL_PROC(_vfs_ffs, FFS_ADJ_REFCNT, adjrefcnt, CTLFLAG_WR|CTLTYPE_STRUCT,
2699 0, 0, sysctl_ffs_fsck, "S,fsck", "Adjust Inode Reference Count");
2701 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_BLKCNT, adjblkcnt, CTLFLAG_WR,
2702 sysctl_ffs_fsck, "Adjust Inode Used Blocks Count");
2704 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NDIR, adjndir, CTLFLAG_WR,
2705 sysctl_ffs_fsck, "Adjust number of directories");
2707 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NBFREE, adjnbfree, CTLFLAG_WR,
2708 sysctl_ffs_fsck, "Adjust number of free blocks");
2710 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NIFREE, adjnifree, CTLFLAG_WR,
2711 sysctl_ffs_fsck, "Adjust number of free inodes");
2713 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NFFREE, adjnffree, CTLFLAG_WR,
2714 sysctl_ffs_fsck, "Adjust number of free frags");
2716 static SYSCTL_NODE(_vfs_ffs, FFS_ADJ_NUMCLUSTERS, adjnumclusters, CTLFLAG_WR,
2717 sysctl_ffs_fsck, "Adjust number of free clusters");
2719 static SYSCTL_NODE(_vfs_ffs, FFS_DIR_FREE, freedirs, CTLFLAG_WR,
2720 sysctl_ffs_fsck, "Free Range of Directory Inodes");
2722 static SYSCTL_NODE(_vfs_ffs, FFS_FILE_FREE, freefiles, CTLFLAG_WR,
2723 sysctl_ffs_fsck, "Free Range of File Inodes");
2725 static SYSCTL_NODE(_vfs_ffs, FFS_BLK_FREE, freeblks, CTLFLAG_WR,
2726 sysctl_ffs_fsck, "Free Range of Blocks");
2728 static SYSCTL_NODE(_vfs_ffs, FFS_SET_FLAGS, setflags, CTLFLAG_WR,
2729 sysctl_ffs_fsck, "Change Filesystem Flags");
2731 static SYSCTL_NODE(_vfs_ffs, FFS_SET_CWD, setcwd, CTLFLAG_WR,
2732 sysctl_ffs_fsck, "Set Current Working Directory");
2734 static SYSCTL_NODE(_vfs_ffs, FFS_SET_DOTDOT, setdotdot, CTLFLAG_WR,
2735 sysctl_ffs_fsck, "Change Value of .. Entry");
2737 static SYSCTL_NODE(_vfs_ffs, FFS_UNLINK, unlink, CTLFLAG_WR,
2738 sysctl_ffs_fsck, "Unlink a Duplicate Name");
2740 static SYSCTL_NODE(_vfs_ffs, FFS_SET_INODE, setinode, CTLFLAG_WR,
2741 sysctl_ffs_fsck, "Update an On-Disk Inode");
2743 static SYSCTL_NODE(_vfs_ffs, FFS_SET_BUFOUTPUT, setbufoutput, CTLFLAG_WR,
2744 sysctl_ffs_fsck, "Set Buffered Writing for Descriptor");
2748 static int fsckcmds = 0;
2749 SYSCTL_INT(_debug, OID_AUTO, fsckcmds, CTLFLAG_RW, &fsckcmds, 0, "");
2752 static int buffered_write(struct file *, struct uio *, struct ucred *,
2753 int, struct thread *);
2756 sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS)
2758 struct thread *td = curthread;
2759 struct fsck_cmd cmd;
2760 struct ufsmount *ump;
2761 struct vnode *vp, *dvp, *fdvp;
2762 struct inode *ip, *dp;
2766 long blkcnt, blksize;
2767 struct file *fp, *vfp;
2768 cap_rights_t rights;
2769 int filetype, error;
2770 static struct fileops *origops, bufferedops;
2772 if (req->newlen > sizeof cmd)
2774 if ((error = SYSCTL_IN(req, &cmd, sizeof cmd)) != 0)
2776 if (cmd.version != FFS_CMD_VERSION)
2777 return (ERPCMISMATCH);
2778 if ((error = getvnode(td, cmd.handle,
2779 cap_rights_init(&rights, CAP_FSCK), &fp)) != 0)
2782 if (vp->v_type != VREG && vp->v_type != VDIR) {
2786 vn_start_write(vp, &mp, V_WAIT);
2787 if (mp == 0 || strncmp(mp->mnt_stat.f_fstypename, "ufs", MFSNAMELEN)) {
2788 vn_finished_write(mp);
2793 if ((mp->mnt_flag & MNT_RDONLY) &&
2794 ump->um_fsckpid != td->td_proc->p_pid) {
2795 vn_finished_write(mp);
2802 switch (oidp->oid_number) {
2807 printf("%s: %s flags\n", mp->mnt_stat.f_mntonname,
2808 cmd.size > 0 ? "set" : "clear");
2811 fs->fs_flags |= (long)cmd.value;
2813 fs->fs_flags &= ~(long)cmd.value;
2816 case FFS_ADJ_REFCNT:
2819 printf("%s: adjust inode %jd link count by %jd\n",
2820 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2821 (intmax_t)cmd.size);
2824 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2827 ip->i_nlink += cmd.size;
2828 DIP_SET(ip, i_nlink, ip->i_nlink);
2829 ip->i_effnlink += cmd.size;
2830 ip->i_flag |= IN_CHANGE | IN_MODIFIED;
2831 error = ffs_update(vp, 1);
2832 if (DOINGSOFTDEP(vp))
2833 softdep_change_linkcnt(ip);
2837 case FFS_ADJ_BLKCNT:
2840 printf("%s: adjust inode %jd block count by %jd\n",
2841 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2842 (intmax_t)cmd.size);
2845 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
2848 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + cmd.size);
2849 ip->i_flag |= IN_CHANGE | IN_MODIFIED;
2850 error = ffs_update(vp, 1);
2862 printf("%s: free %s inode %ju\n",
2863 mp->mnt_stat.f_mntonname,
2864 filetype == IFDIR ? "directory" : "file",
2865 (uintmax_t)cmd.value);
2867 printf("%s: free %s inodes %ju-%ju\n",
2868 mp->mnt_stat.f_mntonname,
2869 filetype == IFDIR ? "directory" : "file",
2870 (uintmax_t)cmd.value,
2871 (uintmax_t)(cmd.value + cmd.size - 1));
2874 while (cmd.size > 0) {
2875 if ((error = ffs_freefile(ump, fs, ump->um_devvp,
2876 cmd.value, filetype, NULL)))
2887 printf("%s: free block %jd\n",
2888 mp->mnt_stat.f_mntonname,
2889 (intmax_t)cmd.value);
2891 printf("%s: free blocks %jd-%jd\n",
2892 mp->mnt_stat.f_mntonname,
2893 (intmax_t)cmd.value,
2894 (intmax_t)cmd.value + cmd.size - 1);
2899 blksize = fs->fs_frag - (blkno % fs->fs_frag);
2900 while (blkcnt > 0) {
2901 if (blksize > blkcnt)
2903 ffs_blkfree(ump, fs, ump->um_devvp, blkno,
2904 blksize * fs->fs_fsize, ROOTINO, VDIR, NULL);
2907 blksize = fs->fs_frag;
2912 * Adjust superblock summaries. fsck(8) is expected to
2913 * submit deltas when necessary.
2918 printf("%s: adjust number of directories by %jd\n",
2919 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2922 fs->fs_cstotal.cs_ndir += cmd.value;
2925 case FFS_ADJ_NBFREE:
2928 printf("%s: adjust number of free blocks by %+jd\n",
2929 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2932 fs->fs_cstotal.cs_nbfree += cmd.value;
2935 case FFS_ADJ_NIFREE:
2938 printf("%s: adjust number of free inodes by %+jd\n",
2939 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2942 fs->fs_cstotal.cs_nifree += cmd.value;
2945 case FFS_ADJ_NFFREE:
2948 printf("%s: adjust number of free frags by %+jd\n",
2949 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2952 fs->fs_cstotal.cs_nffree += cmd.value;
2955 case FFS_ADJ_NUMCLUSTERS:
2958 printf("%s: adjust number of free clusters by %+jd\n",
2959 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2962 fs->fs_cstotal.cs_numclusters += cmd.value;
2968 printf("%s: set current directory to inode %jd\n",
2969 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
2972 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_SHARED, &vp)))
2974 AUDIT_ARG_VNODE1(vp);
2975 if ((error = change_dir(vp, td)) != 0) {
2983 case FFS_SET_DOTDOT:
2986 printf("%s: change .. in cwd from %jd to %jd\n",
2987 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value,
2988 (intmax_t)cmd.size);
2992 * First we have to get and lock the parent directory
2993 * to which ".." points.
2995 error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &fdvp);
2999 * Now we get and lock the child directory containing "..".
3001 FILEDESC_SLOCK(td->td_proc->p_fd);
3002 dvp = td->td_proc->p_fd->fd_cdir;
3003 FILEDESC_SUNLOCK(td->td_proc->p_fd);
3004 if ((error = vget(dvp, LK_EXCLUSIVE, td)) != 0) {
3009 dp->i_offset = 12; /* XXX mastertemplate.dot_reclen */
3010 error = ufs_dirrewrite(dp, VTOI(fdvp), (ino_t)cmd.size,
3023 if (copyinstr((char *)(intptr_t)cmd.value, buf,32,NULL))
3024 strncpy(buf, "Name_too_long", 32);
3025 printf("%s: unlink %s (inode %jd)\n",
3026 mp->mnt_stat.f_mntonname, buf, (intmax_t)cmd.size);
3030 * kern_unlinkat will do its own start/finish writes and
3031 * they do not nest, so drop ours here. Setting mp == NULL
3032 * indicates that vn_finished_write is not needed down below.
3034 vn_finished_write(mp);
3036 error = kern_unlinkat(td, AT_FDCWD, (char *)(intptr_t)cmd.value,
3037 UIO_USERSPACE, (ino_t)cmd.size);
3041 if (ump->um_fsckpid != td->td_proc->p_pid) {
3047 printf("%s: update inode %jd\n",
3048 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value);
3051 if ((error = ffs_vget(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp)))
3053 AUDIT_ARG_VNODE1(vp);
3055 if (ip->i_ump->um_fstype == UFS1)
3056 error = copyin((void *)(intptr_t)cmd.size, ip->i_din1,
3057 sizeof(struct ufs1_dinode));
3059 error = copyin((void *)(intptr_t)cmd.size, ip->i_din2,
3060 sizeof(struct ufs2_dinode));
3065 ip->i_flag |= IN_CHANGE | IN_MODIFIED;
3066 error = ffs_update(vp, 1);
3070 case FFS_SET_BUFOUTPUT:
3071 if (ump->um_fsckpid != td->td_proc->p_pid) {
3075 if (VTOI(vp)->i_ump != ump) {
3081 printf("%s: %s buffered output for descriptor %jd\n",
3082 mp->mnt_stat.f_mntonname,
3083 cmd.size == 1 ? "enable" : "disable",
3084 (intmax_t)cmd.value);
3087 if ((error = getvnode(td, cmd.value,
3088 cap_rights_init(&rights, CAP_FSCK), &vfp)) != 0)
3090 if (vfp->f_vnode->v_type != VCHR) {
3095 if (origops == NULL) {
3096 origops = vfp->f_ops;
3097 bcopy((void *)origops, (void *)&bufferedops,
3098 sizeof(bufferedops));
3099 bufferedops.fo_write = buffered_write;
3102 atomic_store_rel_ptr((volatile uintptr_t *)&vfp->f_ops,
3103 (uintptr_t)&bufferedops);
3105 atomic_store_rel_ptr((volatile uintptr_t *)&vfp->f_ops,
3106 (uintptr_t)origops);
3113 printf("Invalid request %d from fsck\n",
3122 vn_finished_write(mp);
3127 * Function to switch a descriptor to use the buffer cache to stage
3128 * its I/O. This is needed so that writes to the filesystem device
3129 * will give snapshots a chance to copy modified blocks for which it
3130 * needs to retain copies.
3133 buffered_write(fp, uio, active_cred, flags, td)
3136 struct ucred *active_cred;
3140 struct vnode *devvp, *vp;
3144 struct filedesc *fdp;
3149 * The devvp is associated with the /dev filesystem. To discover
3150 * the filesystem with which the device is associated, we depend
3151 * on the application setting the current directory to a location
3152 * within the filesystem being written. Yes, this is an ugly hack.
3154 devvp = fp->f_vnode;
3155 if (!vn_isdisk(devvp, NULL))
3157 fdp = td->td_proc->p_fd;
3158 FILEDESC_SLOCK(fdp);
3161 FILEDESC_SUNLOCK(fdp);
3162 vn_lock(vp, LK_SHARED | LK_RETRY);
3164 * Check that the current directory vnode indeed belongs to
3165 * UFS before trying to dereference UFS-specific v_data fields.
3167 if (vp->v_op != &ffs_vnodeops1 && vp->v_op != &ffs_vnodeops2) {
3172 if (ip->i_devvp != devvp) {
3178 foffset_lock_uio(fp, uio, flags);
3179 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
3182 printf("%s: buffered write for block %jd\n",
3183 fs->fs_fsmnt, (intmax_t)btodb(uio->uio_offset));
3187 * All I/O must be contained within a filesystem block, start on
3188 * a fragment boundary, and be a multiple of fragments in length.
3190 if (uio->uio_resid > fs->fs_bsize - (uio->uio_offset % fs->fs_bsize) ||
3191 fragoff(fs, uio->uio_offset) != 0 ||
3192 fragoff(fs, uio->uio_resid) != 0) {
3196 lbn = numfrags(fs, uio->uio_offset);
3197 bp = getblk(devvp, lbn, uio->uio_resid, 0, 0, 0);
3198 bp->b_flags |= B_RELBUF;
3199 if ((error = uiomove((char *)bp->b_data, uio->uio_resid, uio)) != 0) {
3205 VOP_UNLOCK(devvp, 0);
3206 foffset_unlock_uio(fp, uio, flags | FOF_NEXTOFF);