1 /* $NetBSD: ffs_alloc.c,v 1.151 2015/08/12 14:52:35 riastradh Exp $ */
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Wasabi Systems, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 2002 Networks Associates Technology, Inc.
34 * All rights reserved.
36 * This software was developed for the FreeBSD Project by Marshall
37 * Kirk McKusick and Network Associates Laboratories, the Security
38 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
39 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
42 * Copyright (c) 1982, 1986, 1989, 1993
43 * The Regents of the University of California. All rights reserved.
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: ffs_alloc.c,v 1.151 2015/08/12 14:52:35 riastradh Exp $");
75 #if defined(_KERNEL_OPT)
77 #include "opt_quota.h"
78 #include "opt_uvm_page_trkown.h"
81 #include <sys/param.h>
82 #include <sys/systm.h>
84 #include <sys/cprng.h>
85 #include <sys/fstrans.h>
86 #include <sys/kauth.h>
87 #include <sys/kernel.h>
88 #include <sys/mount.h>
90 #include <sys/syslog.h>
91 #include <sys/vnode.h>
92 #include <sys/wapbl.h>
93 #include <sys/cprng.h>
95 #include <miscfs/specfs/specdev.h>
96 #include <ufs/ufs/quota.h>
97 #include <ufs/ufs/ufsmount.h>
98 #include <ufs/ufs/inode.h>
99 #include <ufs/ufs/ufs_extern.h>
100 #include <ufs/ufs/ufs_bswap.h>
101 #include <ufs/ufs/ufs_wapbl.h>
103 #include <ufs/ffs/fs.h>
104 #include <ufs/ffs/ffs_extern.h>
106 #ifdef UVM_PAGE_TRKOWN
110 static daddr_t
ffs_alloccg(struct inode
*, int, daddr_t
, int, int);
111 static daddr_t
ffs_alloccgblk(struct inode
*, struct buf
*, daddr_t
, int);
112 static ino_t
ffs_dirpref(struct inode
*);
113 static daddr_t
ffs_fragextend(struct inode
*, int, daddr_t
, int, int);
114 static void ffs_fserr(struct fs
*, kauth_cred_t
, const char *);
115 static daddr_t
ffs_hashalloc(struct inode
*, int, daddr_t
, int, int,
116 daddr_t (*)(struct inode
*, int, daddr_t
, int, int));
117 static daddr_t
ffs_nodealloccg(struct inode
*, int, daddr_t
, int, int);
118 static int32_t ffs_mapsearch(struct fs
*, struct cg
*,
120 static void ffs_blkfree_common(struct ufsmount
*, struct fs
*, dev_t
, struct buf
*,
121 daddr_t
, long, bool);
122 static void ffs_freefile_common(struct ufsmount
*, struct fs
*, dev_t
, struct buf
*, ino_t
,
125 /* if 1, changes in optimalization strategy are logged */
126 int ffs_log_changeopt
= 0;
128 /* in ffs_tables.c */
129 extern const int inside
[], around
[];
130 extern const u_char
* const fragtbl
[];
132 /* Basic consistency check for block allocations */
134 ffs_check_bad_allocation(const char *func
, struct fs
*fs
, daddr_t bno
,
135 long size
, dev_t dev
, ino_t inum
)
137 if ((u_int
)size
> fs
->fs_bsize
|| ffs_fragoff(fs
, size
) != 0 ||
138 ffs_fragnum(fs
, bno
) + ffs_numfrags(fs
, size
) > fs
->fs_frag
) {
139 printf("dev = 0x%llx, bno = %" PRId64
" bsize = %d, "
140 "size = %ld, fs = %s\n",
141 (long long)dev
, bno
, fs
->fs_bsize
, size
, fs
->fs_fsmnt
);
142 panic("%s: bad size", func
);
145 if (bno
>= fs
->fs_size
) {
146 printf("bad block %" PRId64
", ino %llu\n", bno
,
147 (unsigned long long)inum
);
148 ffs_fserr(fs
, NOCRED
, "bad block");
155 * Allocate a block in the file system.
157 * The size of the requested block is given, which must be some
158 * multiple of fs_fsize and <= fs_bsize.
159 * A preference may be optionally specified. If a preference is given
160 * the following hierarchy is used to allocate a block:
161 * 1) allocate the requested block.
162 * 2) allocate a rotationally optimal block in the same cylinder.
163 * 3) allocate a block in the same cylinder group.
164 * 4) quadradically rehash into other cylinder groups, until an
165 * available block is located.
166 * If no block preference is given the following hierarchy is used
167 * to allocate a block:
168 * 1) allocate a block in the cylinder group that contains the
169 * inode for the file.
170 * 2) quadradically rehash into other cylinder groups, until an
171 * available block is located.
173 * => called with um_lock held
174 * => releases um_lock before returning
177 ffs_alloc(struct inode
*ip
, daddr_t lbn
, daddr_t bpref
, int size
, int flags
,
178 kauth_cred_t cred
, daddr_t
*bnp
)
180 struct ufsmount
*ump
;
184 #if defined(QUOTA) || defined(QUOTA2)
191 KASSERT(mutex_owned(&ump
->um_lock
));
193 #ifdef UVM_PAGE_TRKOWN
196 * Sanity-check that allocations within the file size
197 * do not allow other threads to read the stale contents
198 * of newly allocated blocks.
199 * Usually pages will exist to cover the new allocation.
200 * There is an optimization in ffs_write() where we skip
201 * creating pages if several conditions are met:
202 * - the file must not be mapped (in any user address space).
203 * - the write must cover whole pages and whole blocks.
204 * If those conditions are not met then pages must exist and
205 * be locked by the current thread.
208 if (ITOV(ip
)->v_type
== VREG
&&
209 ffs_lblktosize(fs
, (voff_t
)lbn
) < round_page(ITOV(ip
)->v_size
)) {
211 struct vnode
*vp
= ITOV(ip
);
212 struct uvm_object
*uobj
= &vp
->v_uobj
;
213 voff_t off
= trunc_page(ffs_lblktosize(fs
, lbn
));
214 voff_t endoff
= round_page(ffs_lblktosize(fs
, lbn
) + size
);
216 mutex_enter(uobj
->vmobjlock
);
217 while (off
< endoff
) {
218 pg
= uvm_pagelookup(uobj
, off
);
219 KASSERT((pg
== NULL
&& (vp
->v_vflag
& VV_MAPPED
) == 0 &&
220 (size
& PAGE_MASK
) == 0 &&
221 ffs_blkoff(fs
, size
) == 0) ||
222 (pg
!= NULL
&& pg
->owner
== curproc
->p_pid
&&
223 pg
->lowner
== curlwp
->l_lid
));
226 mutex_exit(uobj
->vmobjlock
);
232 if ((u_int
)size
> fs
->fs_bsize
|| ffs_fragoff(fs
, size
) != 0) {
233 printf("dev = 0x%llx, bsize = %d, size = %d, fs = %s\n",
234 (unsigned long long)ip
->i_dev
, fs
->fs_bsize
, size
,
236 panic("ffs_alloc: bad size");
239 panic("ffs_alloc: missing credential");
240 #endif /* DIAGNOSTIC */
241 if (size
== fs
->fs_bsize
&& fs
->fs_cstotal
.cs_nbfree
== 0)
243 if (freespace(fs
, fs
->fs_minfree
) <= 0 &&
244 kauth_authorize_system(cred
, KAUTH_SYSTEM_FS_RESERVEDSPACE
, 0, NULL
,
247 #if defined(QUOTA) || defined(QUOTA2)
248 mutex_exit(&ump
->um_lock
);
249 if ((error
= chkdq(ip
, btodb(size
), cred
, 0)) != 0)
251 mutex_enter(&ump
->um_lock
);
254 if (bpref
>= fs
->fs_size
)
257 cg
= ino_to_cg(fs
, ip
->i_number
);
259 cg
= dtog(fs
, bpref
);
260 bno
= ffs_hashalloc(ip
, cg
, bpref
, size
, flags
, ffs_alloccg
);
262 DIP_ADD(ip
, blocks
, btodb(size
));
263 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
267 #if defined(QUOTA) || defined(QUOTA2)
269 * Restore user's disk quota because allocation failed.
271 (void) chkdq(ip
, -btodb(size
), cred
, FORCE
);
273 if (flags
& B_CONTIG
) {
275 * XXX ump->um_lock handling is "suspect" at best.
276 * For the case where ffs_hashalloc() fails early
277 * in the B_CONTIG case we reach here with um_lock
278 * already unlocked, so we can't release it again
279 * like in the normal error path. See kern/39206.
282 * Fail silently - it's up to our caller to report
288 mutex_exit(&ump
->um_lock
);
289 ffs_fserr(fs
, cred
, "file system full");
290 uprintf("\n%s: write failed, file system is full\n", fs
->fs_fsmnt
);
295 * Reallocate a fragment to a bigger size
297 * The number and size of the old block is given, and a preference
298 * and new size is also specified. The allocator attempts to extend
299 * the original block. Failing that, the regular block allocator is
300 * invoked to get an appropriate block.
302 * => called with um_lock held
303 * => return with um_lock released
306 ffs_realloccg(struct inode
*ip
, daddr_t lbprev
, daddr_t bpref
, int osize
,
307 int nsize
, kauth_cred_t cred
, struct buf
**bpp
, daddr_t
*blknop
)
309 struct ufsmount
*ump
;
312 int cg
, request
, error
;
318 KASSERT(mutex_owned(&ump
->um_lock
));
320 #ifdef UVM_PAGE_TRKOWN
323 * Sanity-check that allocations within the file size
324 * do not allow other threads to read the stale contents
325 * of newly allocated blocks.
326 * Unlike in ffs_alloc(), here pages must always exist
327 * for such allocations, because only the last block of a file
328 * can be a fragment and ffs_write() will reallocate the
329 * fragment to the new size using ufs_balloc_range(),
330 * which always creates pages to cover blocks it allocates.
333 if (ITOV(ip
)->v_type
== VREG
) {
335 struct uvm_object
*uobj
= &ITOV(ip
)->v_uobj
;
336 voff_t off
= trunc_page(ffs_lblktosize(fs
, lbprev
));
337 voff_t endoff
= round_page(ffs_lblktosize(fs
, lbprev
) + osize
);
339 mutex_enter(uobj
->vmobjlock
);
340 while (off
< endoff
) {
341 pg
= uvm_pagelookup(uobj
, off
);
342 KASSERT(pg
->owner
== curproc
->p_pid
&&
343 pg
->lowner
== curlwp
->l_lid
);
346 mutex_exit(uobj
->vmobjlock
);
351 if ((u_int
)osize
> fs
->fs_bsize
|| ffs_fragoff(fs
, osize
) != 0 ||
352 (u_int
)nsize
> fs
->fs_bsize
|| ffs_fragoff(fs
, nsize
) != 0) {
354 "dev = 0x%llx, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
355 (unsigned long long)ip
->i_dev
, fs
->fs_bsize
, osize
, nsize
,
357 panic("ffs_realloccg: bad size");
360 panic("ffs_realloccg: missing credential");
361 #endif /* DIAGNOSTIC */
362 if (freespace(fs
, fs
->fs_minfree
) <= 0 &&
363 kauth_authorize_system(cred
, KAUTH_SYSTEM_FS_RESERVEDSPACE
, 0, NULL
,
365 mutex_exit(&ump
->um_lock
);
368 if (fs
->fs_magic
== FS_UFS2_MAGIC
)
369 bprev
= ufs_rw64(ip
->i_ffs2_db
[lbprev
], UFS_FSNEEDSWAP(fs
));
371 bprev
= ufs_rw32(ip
->i_ffs1_db
[lbprev
], UFS_FSNEEDSWAP(fs
));
374 printf("dev = 0x%llx, bsize = %d, bprev = %" PRId64
", fs = %s\n",
375 (unsigned long long)ip
->i_dev
, fs
->fs_bsize
, bprev
,
377 panic("ffs_realloccg: bad bprev");
379 mutex_exit(&ump
->um_lock
);
382 * Allocate the extra space in the buffer.
385 (error
= bread(ITOV(ip
), lbprev
, osize
, 0, &bp
)) != 0) {
388 #if defined(QUOTA) || defined(QUOTA2)
389 if ((error
= chkdq(ip
, btodb(nsize
- osize
), cred
, 0)) != 0) {
397 * Check for extension in the existing location.
399 cg
= dtog(fs
, bprev
);
400 mutex_enter(&ump
->um_lock
);
401 if ((bno
= ffs_fragextend(ip
, cg
, bprev
, osize
, nsize
)) != 0) {
402 DIP_ADD(ip
, blocks
, btodb(nsize
- osize
));
403 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
406 if (bp
->b_blkno
!= FFS_FSBTODB(fs
, bno
))
407 panic("bad blockno");
408 allocbuf(bp
, nsize
, 1);
409 memset((char *)bp
->b_data
+ osize
, 0, nsize
- osize
);
410 mutex_enter(bp
->b_objlock
);
411 KASSERT(!cv_has_waiters(&bp
->b_done
));
412 bp
->b_oflags
|= BO_DONE
;
413 mutex_exit(bp
->b_objlock
);
416 if (blknop
!= NULL
) {
422 * Allocate a new disk location.
424 if (bpref
>= fs
->fs_size
)
426 switch ((int)fs
->fs_optim
) {
429 * Allocate an exact sized fragment. Although this makes
430 * best use of space, we will waste time relocating it if
431 * the file continues to grow. If the fragmentation is
432 * less than half of the minimum free reserve, we choose
433 * to begin optimizing for time.
436 if (fs
->fs_minfree
< 5 ||
437 fs
->fs_cstotal
.cs_nffree
>
438 fs
->fs_dsize
* fs
->fs_minfree
/ (2 * 100))
441 if (ffs_log_changeopt
) {
443 "%s: optimization changed from SPACE to TIME\n",
447 fs
->fs_optim
= FS_OPTTIME
;
451 * At this point we have discovered a file that is trying to
452 * grow a small fragment to a larger fragment. To save time,
453 * we allocate a full sized block, then free the unused portion.
454 * If the file continues to grow, the `ffs_fragextend' call
455 * above will be able to grow it in place without further
456 * copying. If aberrant programs cause disk fragmentation to
457 * grow within 2% of the free reserve, we choose to begin
458 * optimizing for space.
460 request
= fs
->fs_bsize
;
461 if (fs
->fs_cstotal
.cs_nffree
<
462 fs
->fs_dsize
* (fs
->fs_minfree
- 2) / 100)
465 if (ffs_log_changeopt
) {
467 "%s: optimization changed from TIME to SPACE\n",
471 fs
->fs_optim
= FS_OPTSPACE
;
474 printf("dev = 0x%llx, optim = %d, fs = %s\n",
475 (unsigned long long)ip
->i_dev
, fs
->fs_optim
, fs
->fs_fsmnt
);
476 panic("ffs_realloccg: bad optim");
479 bno
= ffs_hashalloc(ip
, cg
, bpref
, request
, 0, ffs_alloccg
);
481 if ((ip
->i_ump
->um_mountp
->mnt_wapbl
) &&
482 (ITOV(ip
)->v_type
!= VREG
)) {
483 UFS_WAPBL_REGISTER_DEALLOCATION(
484 ip
->i_ump
->um_mountp
, FFS_FSBTODB(fs
, bprev
),
487 ffs_blkfree(fs
, ip
->i_devvp
, bprev
, (long)osize
,
490 if (nsize
< request
) {
491 if ((ip
->i_ump
->um_mountp
->mnt_wapbl
) &&
492 (ITOV(ip
)->v_type
!= VREG
)) {
493 UFS_WAPBL_REGISTER_DEALLOCATION(
494 ip
->i_ump
->um_mountp
,
495 FFS_FSBTODB(fs
, (bno
+ ffs_numfrags(fs
, nsize
))),
498 ffs_blkfree(fs
, ip
->i_devvp
,
499 bno
+ ffs_numfrags(fs
, nsize
),
500 (long)(request
- nsize
), ip
->i_number
);
502 DIP_ADD(ip
, blocks
, btodb(nsize
- osize
));
503 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
505 bp
->b_blkno
= FFS_FSBTODB(fs
, bno
);
506 allocbuf(bp
, nsize
, 1);
507 memset((char *)bp
->b_data
+ osize
, 0, (u_int
)nsize
- osize
);
508 mutex_enter(bp
->b_objlock
);
509 KASSERT(!cv_has_waiters(&bp
->b_done
));
510 bp
->b_oflags
|= BO_DONE
;
511 mutex_exit(bp
->b_objlock
);
514 if (blknop
!= NULL
) {
519 mutex_exit(&ump
->um_lock
);
521 #if defined(QUOTA) || defined(QUOTA2)
523 * Restore user's disk quota because allocation failed.
525 (void) chkdq(ip
, -btodb(nsize
- osize
), cred
, FORCE
);
535 ffs_fserr(fs
, cred
, "file system full");
536 uprintf("\n%s: write failed, file system is full\n", fs
->fs_fsmnt
);
541 * Allocate an inode in the file system.
543 * If allocating a directory, use ffs_dirpref to select the inode.
544 * If allocating in a directory, the following hierarchy is followed:
545 * 1) allocate the preferred inode.
546 * 2) allocate an inode in the same cylinder group.
547 * 3) quadradically rehash into other cylinder groups, until an
548 * available inode is located.
549 * If no inode preference is given the following hierarchy is used
550 * to allocate an inode:
551 * 1) allocate an inode in cylinder group 0.
552 * 2) quadradically rehash into other cylinder groups, until an
553 * available inode is located.
555 * => um_lock not held upon entry or return
558 ffs_valloc(struct vnode
*pvp
, int mode
, kauth_cred_t cred
, ino_t
*inop
)
560 struct ufsmount
*ump
;
566 UFS_WAPBL_JUNLOCK_ASSERT(pvp
->v_mount
);
572 error
= UFS_WAPBL_BEGIN(pvp
->v_mount
);
576 mutex_enter(&ump
->um_lock
);
577 if (fs
->fs_cstotal
.cs_nifree
== 0)
580 if ((mode
& IFMT
) == IFDIR
)
581 ipref
= ffs_dirpref(pip
);
583 ipref
= pip
->i_number
;
584 if (ipref
>= fs
->fs_ncg
* fs
->fs_ipg
)
586 cg
= ino_to_cg(fs
, ipref
);
588 * Track number of dirs created one after another
589 * in a same cg without intervening by files.
591 if ((mode
& IFMT
) == IFDIR
) {
592 if (fs
->fs_contigdirs
[cg
] < 255)
593 fs
->fs_contigdirs
[cg
]++;
595 if (fs
->fs_contigdirs
[cg
] > 0)
596 fs
->fs_contigdirs
[cg
]--;
598 ino
= (ino_t
)ffs_hashalloc(pip
, cg
, ipref
, mode
, 0, ffs_nodealloccg
);
601 UFS_WAPBL_END(pvp
->v_mount
);
606 mutex_exit(&ump
->um_lock
);
607 UFS_WAPBL_END(pvp
->v_mount
);
608 ffs_fserr(fs
, cred
, "out of inodes");
609 uprintf("\n%s: create/symlink failed, no inodes free\n", fs
->fs_fsmnt
);
614 * Find a cylinder group in which to place a directory.
616 * The policy implemented by this algorithm is to allocate a
617 * directory inode in the same cylinder group as its parent
618 * directory, but also to reserve space for its files inodes
619 * and data. Restrict the number of directories which may be
620 * allocated one after another in the same cylinder group
621 * without intervening allocation of files.
623 * If we allocate a first level directory then force allocation
624 * in another cylinder group.
627 ffs_dirpref(struct inode
*pip
)
629 register struct fs
*fs
;
631 int64_t dirsize
, cgsize
, curdsz
;
632 int avgifree
, avgbfree
, avgndir
;
633 int minifree
, minbfree
, maxndir
;
637 KASSERT(mutex_owned(&pip
->i_ump
->um_lock
));
641 avgifree
= fs
->fs_cstotal
.cs_nifree
/ fs
->fs_ncg
;
642 avgbfree
= fs
->fs_cstotal
.cs_nbfree
/ fs
->fs_ncg
;
643 avgndir
= fs
->fs_cstotal
.cs_ndir
/ fs
->fs_ncg
;
646 * Force allocation in another cg if creating a first level dir.
648 if (ITOV(pip
)->v_vflag
& VV_ROOT
) {
649 prefcg
= cprng_fast32() % fs
->fs_ncg
;
651 minndir
= fs
->fs_ipg
;
652 for (cg
= prefcg
; cg
< fs
->fs_ncg
; cg
++)
653 if (fs
->fs_cs(fs
, cg
).cs_ndir
< minndir
&&
654 fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
&&
655 fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
657 minndir
= fs
->fs_cs(fs
, cg
).cs_ndir
;
659 for (cg
= 0; cg
< prefcg
; cg
++)
660 if (fs
->fs_cs(fs
, cg
).cs_ndir
< minndir
&&
661 fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
&&
662 fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
664 minndir
= fs
->fs_cs(fs
, cg
).cs_ndir
;
666 return ((ino_t
)(fs
->fs_ipg
* mincg
));
670 * Count various limits which used for
671 * optimal allocation of a directory inode.
672 * Try cylinder groups with >75% avgifree and avgbfree.
673 * Avoid cylinder groups with no free blocks or inodes as that
674 * triggers an I/O-expensive cylinder group scan.
676 maxndir
= min(avgndir
+ fs
->fs_ipg
/ 16, fs
->fs_ipg
);
677 minifree
= avgifree
- avgifree
/ 4;
680 minbfree
= avgbfree
- avgbfree
/ 4;
683 cgsize
= (int64_t)fs
->fs_fsize
* fs
->fs_fpg
;
684 dirsize
= (int64_t)fs
->fs_avgfilesize
* fs
->fs_avgfpdir
;
686 curdsz
= (cgsize
- (int64_t)avgbfree
* fs
->fs_bsize
) / avgndir
;
687 if (dirsize
< curdsz
)
690 if (cgsize
< dirsize
* 255)
691 maxcontigdirs
= (avgbfree
* fs
->fs_bsize
) / dirsize
;
694 if (fs
->fs_avgfpdir
> 0)
695 maxcontigdirs
= min(maxcontigdirs
,
696 fs
->fs_ipg
/ fs
->fs_avgfpdir
);
697 if (maxcontigdirs
== 0)
701 * Limit number of dirs in one cg and reserve space for
702 * regular files, but only if we have no deficit in
705 prefcg
= ino_to_cg(fs
, pip
->i_number
);
706 for (cg
= prefcg
; cg
< fs
->fs_ncg
; cg
++)
707 if (fs
->fs_cs(fs
, cg
).cs_ndir
< maxndir
&&
708 fs
->fs_cs(fs
, cg
).cs_nifree
>= minifree
&&
709 fs
->fs_cs(fs
, cg
).cs_nbfree
>= minbfree
) {
710 if (fs
->fs_contigdirs
[cg
] < maxcontigdirs
)
711 return ((ino_t
)(fs
->fs_ipg
* cg
));
713 for (cg
= 0; cg
< prefcg
; cg
++)
714 if (fs
->fs_cs(fs
, cg
).cs_ndir
< maxndir
&&
715 fs
->fs_cs(fs
, cg
).cs_nifree
>= minifree
&&
716 fs
->fs_cs(fs
, cg
).cs_nbfree
>= minbfree
) {
717 if (fs
->fs_contigdirs
[cg
] < maxcontigdirs
)
718 return ((ino_t
)(fs
->fs_ipg
* cg
));
721 * This is a backstop when we are deficient in space.
723 for (cg
= prefcg
; cg
< fs
->fs_ncg
; cg
++)
724 if (fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
)
725 return ((ino_t
)(fs
->fs_ipg
* cg
));
726 for (cg
= 0; cg
< prefcg
; cg
++)
727 if (fs
->fs_cs(fs
, cg
).cs_nifree
>= avgifree
)
729 return ((ino_t
)(fs
->fs_ipg
* cg
));
733 * Select the desired position for the next block in a file. The file is
734 * logically divided into sections. The first section is composed of the
735 * direct blocks. Each additional section contains fs_maxbpg blocks.
737 * If no blocks have been allocated in the first section, the policy is to
738 * request a block in the same cylinder group as the inode that describes
739 * the file. If no blocks have been allocated in any other section, the
740 * policy is to place the section in a cylinder group with a greater than
741 * average number of free blocks. An appropriate cylinder group is found
742 * by using a rotor that sweeps the cylinder groups. When a new group of
743 * blocks is needed, the sweep begins in the cylinder group following the
744 * cylinder group from which the previous allocation was made. The sweep
745 * continues until a cylinder group with greater than the average number
746 * of free blocks is found. If the allocation is for the first block in an
747 * indirect block, the information on the previous allocation is unavailable;
748 * here a best guess is made based upon the logical block number being
751 * If a section is already partially allocated, the policy is to
752 * contiguously allocate fs_maxcontig blocks. The end of one of these
753 * contiguous blocks and the beginning of the next is laid out
754 * contigously if possible.
756 * => um_lock held on entry and exit
759 ffs_blkpref_ufs1(struct inode
*ip
, daddr_t lbn
, int indx
, int flags
,
760 int32_t *bap
/* XXX ondisk32 */)
764 int avgbfree
, startcg
;
766 KASSERT(mutex_owned(&ip
->i_ump
->um_lock
));
771 * If allocating a contiguous file with B_CONTIG, use the hints
772 * in the inode extentions to return the desired block.
774 * For metadata (indirect blocks) return the address of where
775 * the first indirect block resides - we'll scan for the next
776 * available slot if we need to allocate more than one indirect
777 * block. For data, return the address of the actual block
778 * relative to the address of the first data block.
780 if (flags
& B_CONTIG
) {
781 KASSERT(ip
->i_ffs_first_data_blk
!= 0);
782 KASSERT(ip
->i_ffs_first_indir_blk
!= 0);
783 if (flags
& B_METAONLY
)
784 return ip
->i_ffs_first_indir_blk
;
786 return ip
->i_ffs_first_data_blk
+ ffs_blkstofrags(fs
, lbn
);
789 if (indx
% fs
->fs_maxbpg
== 0 || bap
[indx
- 1] == 0) {
790 if (lbn
< UFS_NDADDR
+ FFS_NINDIR(fs
)) {
791 cg
= ino_to_cg(fs
, ip
->i_number
);
792 return (cgbase(fs
, cg
) + fs
->fs_frag
);
795 * Find a cylinder with greater than average number of
796 * unused data blocks.
798 if (indx
== 0 || bap
[indx
- 1] == 0)
800 ino_to_cg(fs
, ip
->i_number
) + lbn
/ fs
->fs_maxbpg
;
803 ufs_rw32(bap
[indx
- 1], UFS_FSNEEDSWAP(fs
)) + 1);
804 startcg
%= fs
->fs_ncg
;
805 avgbfree
= fs
->fs_cstotal
.cs_nbfree
/ fs
->fs_ncg
;
806 for (cg
= startcg
; cg
< fs
->fs_ncg
; cg
++)
807 if (fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
808 return (cgbase(fs
, cg
) + fs
->fs_frag
);
810 for (cg
= 0; cg
< startcg
; cg
++)
811 if (fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
812 return (cgbase(fs
, cg
) + fs
->fs_frag
);
817 * We just always try to lay things out contiguously.
819 return ufs_rw32(bap
[indx
- 1], UFS_FSNEEDSWAP(fs
)) + fs
->fs_frag
;
823 ffs_blkpref_ufs2(struct inode
*ip
, daddr_t lbn
, int indx
, int flags
,
828 int avgbfree
, startcg
;
830 KASSERT(mutex_owned(&ip
->i_ump
->um_lock
));
835 * If allocating a contiguous file with B_CONTIG, use the hints
836 * in the inode extentions to return the desired block.
838 * For metadata (indirect blocks) return the address of where
839 * the first indirect block resides - we'll scan for the next
840 * available slot if we need to allocate more than one indirect
841 * block. For data, return the address of the actual block
842 * relative to the address of the first data block.
844 if (flags
& B_CONTIG
) {
845 KASSERT(ip
->i_ffs_first_data_blk
!= 0);
846 KASSERT(ip
->i_ffs_first_indir_blk
!= 0);
847 if (flags
& B_METAONLY
)
848 return ip
->i_ffs_first_indir_blk
;
850 return ip
->i_ffs_first_data_blk
+ ffs_blkstofrags(fs
, lbn
);
853 if (indx
% fs
->fs_maxbpg
== 0 || bap
[indx
- 1] == 0) {
854 if (lbn
< UFS_NDADDR
+ FFS_NINDIR(fs
)) {
855 cg
= ino_to_cg(fs
, ip
->i_number
);
856 return (cgbase(fs
, cg
) + fs
->fs_frag
);
859 * Find a cylinder with greater than average number of
860 * unused data blocks.
862 if (indx
== 0 || bap
[indx
- 1] == 0)
864 ino_to_cg(fs
, ip
->i_number
) + lbn
/ fs
->fs_maxbpg
;
867 ufs_rw64(bap
[indx
- 1], UFS_FSNEEDSWAP(fs
)) + 1);
868 startcg
%= fs
->fs_ncg
;
869 avgbfree
= fs
->fs_cstotal
.cs_nbfree
/ fs
->fs_ncg
;
870 for (cg
= startcg
; cg
< fs
->fs_ncg
; cg
++)
871 if (fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
872 return (cgbase(fs
, cg
) + fs
->fs_frag
);
874 for (cg
= 0; cg
< startcg
; cg
++)
875 if (fs
->fs_cs(fs
, cg
).cs_nbfree
>= avgbfree
) {
876 return (cgbase(fs
, cg
) + fs
->fs_frag
);
881 * We just always try to lay things out contiguously.
883 return ufs_rw64(bap
[indx
- 1], UFS_FSNEEDSWAP(fs
)) + fs
->fs_frag
;
888 * Implement the cylinder overflow algorithm.
890 * The policy implemented by this algorithm is:
891 * 1) allocate the block in its requested cylinder group.
892 * 2) quadradically rehash on the cylinder group number.
893 * 3) brute force search for a free block.
895 * => called with um_lock held
896 * => returns with um_lock released on success, held on failure
897 * (*allocator releases lock on success, retains lock on failure)
901 ffs_hashalloc(struct inode
*ip
, int cg
, daddr_t pref
,
902 int size
/* size for data blocks, mode for inodes */,
903 int flags
, daddr_t (*allocator
)(struct inode
*, int, daddr_t
, int, int))
911 * 1: preferred cylinder group
913 result
= (*allocator
)(ip
, cg
, pref
, size
, flags
);
917 if (flags
& B_CONTIG
)
920 * 2: quadratic rehash
922 for (i
= 1; i
< fs
->fs_ncg
; i
*= 2) {
924 if (cg
>= fs
->fs_ncg
)
926 result
= (*allocator
)(ip
, cg
, 0, size
, flags
);
931 * 3: brute force search
932 * Note that we start at i == 2, since 0 was checked initially,
933 * and 1 is always checked in the quadratic rehash.
935 cg
= (icg
+ 2) % fs
->fs_ncg
;
936 for (i
= 2; i
< fs
->fs_ncg
; i
++) {
937 result
= (*allocator
)(ip
, cg
, 0, size
, flags
);
941 if (cg
== fs
->fs_ncg
)
948 * Determine whether a fragment can be extended.
950 * Check to see if the necessary fragments are available, and
951 * if they are, allocate them.
953 * => called with um_lock held
954 * => returns with um_lock released on success, held on failure
957 ffs_fragextend(struct inode
*ip
, int cg
, daddr_t bprev
, int osize
, int nsize
)
959 struct ufsmount
*ump
;
971 KASSERT(mutex_owned(&ump
->um_lock
));
973 if (fs
->fs_cs(fs
, cg
).cs_nffree
< ffs_numfrags(fs
, nsize
- osize
))
975 frags
= ffs_numfrags(fs
, nsize
);
976 bbase
= ffs_fragnum(fs
, bprev
);
977 if (bbase
> ffs_fragnum(fs
, (bprev
+ frags
- 1))) {
978 /* cannot extend across a block boundary */
981 mutex_exit(&ump
->um_lock
);
982 error
= bread(ip
->i_devvp
, FFS_FSBTODB(fs
, cgtod(fs
, cg
)),
983 (int)fs
->fs_cgsize
, B_MODIFY
, &bp
);
986 cgp
= (struct cg
*)bp
->b_data
;
987 if (!cg_chkmagic(cgp
, UFS_FSNEEDSWAP(fs
)))
989 cgp
->cg_old_time
= ufs_rw32(time_second
, UFS_FSNEEDSWAP(fs
));
990 if ((fs
->fs_magic
!= FS_UFS1_MAGIC
) ||
991 (fs
->fs_old_flags
& FS_FLAGS_UPDATED
))
992 cgp
->cg_time
= ufs_rw64(time_second
, UFS_FSNEEDSWAP(fs
));
993 bno
= dtogd(fs
, bprev
);
994 blksfree
= cg_blksfree(cgp
, UFS_FSNEEDSWAP(fs
));
995 for (i
= ffs_numfrags(fs
, osize
); i
< frags
; i
++)
996 if (isclr(blksfree
, bno
+ i
))
999 * the current fragment can be extended
1000 * deduct the count on fragment being extended into
1001 * increase the count on the remaining fragment (if any)
1002 * allocate the extended piece
1004 for (i
= frags
; i
< fs
->fs_frag
- bbase
; i
++)
1005 if (isclr(blksfree
, bno
+ i
))
1007 ufs_add32(cgp
->cg_frsum
[i
- ffs_numfrags(fs
, osize
)], -1, UFS_FSNEEDSWAP(fs
));
1009 ufs_add32(cgp
->cg_frsum
[i
- frags
], 1, UFS_FSNEEDSWAP(fs
));
1010 mutex_enter(&ump
->um_lock
);
1011 for (i
= ffs_numfrags(fs
, osize
); i
< frags
; i
++) {
1012 clrbit(blksfree
, bno
+ i
);
1013 ufs_add32(cgp
->cg_cs
.cs_nffree
, -1, UFS_FSNEEDSWAP(fs
));
1014 fs
->fs_cstotal
.cs_nffree
--;
1015 fs
->fs_cs(fs
, cg
).cs_nffree
--;
1018 ACTIVECG_CLR(fs
, cg
);
1019 mutex_exit(&ump
->um_lock
);
1026 mutex_enter(&ump
->um_lock
);
1031 * Determine whether a block can be allocated.
1033 * Check to see if a block of the appropriate size is available,
1034 * and if it is, allocate it.
1037 ffs_alloccg(struct inode
*ip
, int cg
, daddr_t bpref
, int size
, int flags
)
1039 struct ufsmount
*ump
;
1040 struct fs
*fs
= ip
->i_fs
;
1045 int error
, frags
, allocsiz
, i
;
1047 const int needswap
= UFS_FSNEEDSWAP(fs
);
1051 KASSERT(mutex_owned(&ump
->um_lock
));
1053 if (fs
->fs_cs(fs
, cg
).cs_nbfree
== 0 && size
== fs
->fs_bsize
)
1055 mutex_exit(&ump
->um_lock
);
1056 error
= bread(ip
->i_devvp
, FFS_FSBTODB(fs
, cgtod(fs
, cg
)),
1057 (int)fs
->fs_cgsize
, B_MODIFY
, &bp
);
1060 cgp
= (struct cg
*)bp
->b_data
;
1061 if (!cg_chkmagic(cgp
, needswap
) ||
1062 (cgp
->cg_cs
.cs_nbfree
== 0 && size
== fs
->fs_bsize
))
1064 cgp
->cg_old_time
= ufs_rw32(time_second
, needswap
);
1065 if ((fs
->fs_magic
!= FS_UFS1_MAGIC
) ||
1066 (fs
->fs_old_flags
& FS_FLAGS_UPDATED
))
1067 cgp
->cg_time
= ufs_rw64(time_second
, needswap
);
1068 if (size
== fs
->fs_bsize
) {
1069 mutex_enter(&ump
->um_lock
);
1070 blkno
= ffs_alloccgblk(ip
, bp
, bpref
, flags
);
1071 ACTIVECG_CLR(fs
, cg
);
1072 mutex_exit(&ump
->um_lock
);
1077 * check to see if any fragments are already available
1078 * allocsiz is the size which will be allocated, hacking
1079 * it down to a smaller size if necessary
1081 blksfree
= cg_blksfree(cgp
, needswap
);
1082 frags
= ffs_numfrags(fs
, size
);
1083 for (allocsiz
= frags
; allocsiz
< fs
->fs_frag
; allocsiz
++)
1084 if (cgp
->cg_frsum
[allocsiz
] != 0)
1086 if (allocsiz
== fs
->fs_frag
) {
1088 * no fragments were available, so a block will be
1089 * allocated, and hacked up
1091 if (cgp
->cg_cs
.cs_nbfree
== 0)
1093 mutex_enter(&ump
->um_lock
);
1094 blkno
= ffs_alloccgblk(ip
, bp
, bpref
, flags
);
1095 bno
= dtogd(fs
, blkno
);
1096 for (i
= frags
; i
< fs
->fs_frag
; i
++)
1097 setbit(blksfree
, bno
+ i
);
1098 i
= fs
->fs_frag
- frags
;
1099 ufs_add32(cgp
->cg_cs
.cs_nffree
, i
, needswap
);
1100 fs
->fs_cstotal
.cs_nffree
+= i
;
1101 fs
->fs_cs(fs
, cg
).cs_nffree
+= i
;
1103 ufs_add32(cgp
->cg_frsum
[i
], 1, needswap
);
1104 ACTIVECG_CLR(fs
, cg
);
1105 mutex_exit(&ump
->um_lock
);
1109 bno
= ffs_mapsearch(fs
, cgp
, bpref
, allocsiz
);
1112 * XXX fvdl mapsearch will panic, and never return -1
1113 * also: returning NULL as daddr_t ?
1118 for (i
= 0; i
< frags
; i
++)
1119 clrbit(blksfree
, bno
+ i
);
1120 mutex_enter(&ump
->um_lock
);
1121 ufs_add32(cgp
->cg_cs
.cs_nffree
, -frags
, needswap
);
1122 fs
->fs_cstotal
.cs_nffree
-= frags
;
1123 fs
->fs_cs(fs
, cg
).cs_nffree
-= frags
;
1125 ufs_add32(cgp
->cg_frsum
[allocsiz
], -1, needswap
);
1126 if (frags
!= allocsiz
)
1127 ufs_add32(cgp
->cg_frsum
[allocsiz
- frags
], 1, needswap
);
1128 blkno
= cgbase(fs
, cg
) + bno
;
1129 ACTIVECG_CLR(fs
, cg
);
1130 mutex_exit(&ump
->um_lock
);
1137 mutex_enter(&ump
->um_lock
);
1142 * Allocate a block in a cylinder group.
1144 * This algorithm implements the following policy:
1145 * 1) allocate the requested block.
1146 * 2) allocate a rotationally optimal block in the same cylinder.
1147 * 3) allocate the next available block on the block rotor for the
1148 * specified cylinder group.
1149 * Note that this routine only allocates fs_bsize blocks; these
1150 * blocks may be fragmented by the routine that allocates them.
1153 ffs_alloccgblk(struct inode
*ip
, struct buf
*bp
, daddr_t bpref
, int flags
)
1155 struct fs
*fs
= ip
->i_fs
;
1161 const int needswap
= UFS_FSNEEDSWAP(fs
);
1163 KASSERT(mutex_owned(&ip
->i_ump
->um_lock
));
1165 cgp
= (struct cg
*)bp
->b_data
;
1166 blksfree
= cg_blksfree(cgp
, needswap
);
1167 if (bpref
== 0 || dtog(fs
, bpref
) != ufs_rw32(cgp
->cg_cgx
, needswap
)) {
1168 bpref
= ufs_rw32(cgp
->cg_rotor
, needswap
);
1170 bpref
= ffs_blknum(fs
, bpref
);
1171 bno
= dtogd(fs
, bpref
);
1173 * if the requested block is available, use it
1175 if (ffs_isblock(fs
, blksfree
, ffs_fragstoblks(fs
, bno
)))
1178 * if the requested data block isn't available and we are
1179 * trying to allocate a contiguous file, return an error.
1181 if ((flags
& (B_CONTIG
| B_METAONLY
)) == B_CONTIG
)
1186 * Take the next available block in this cylinder group.
1188 bno
= ffs_mapsearch(fs
, cgp
, bpref
, (int)fs
->fs_frag
);
1191 cgp
->cg_rotor
= ufs_rw32(bno
, needswap
);
1193 blkno
= ffs_fragstoblks(fs
, bno
);
1194 ffs_clrblock(fs
, blksfree
, blkno
);
1195 ffs_clusteracct(fs
, cgp
, blkno
, -1);
1196 ufs_add32(cgp
->cg_cs
.cs_nbfree
, -1, needswap
);
1197 fs
->fs_cstotal
.cs_nbfree
--;
1198 fs
->fs_cs(fs
, ufs_rw32(cgp
->cg_cgx
, needswap
)).cs_nbfree
--;
1199 if ((fs
->fs_magic
== FS_UFS1_MAGIC
) &&
1200 ((fs
->fs_old_flags
& FS_FLAGS_UPDATED
) == 0)) {
1202 cylno
= old_cbtocylno(fs
, bno
);
1203 KASSERT(cylno
>= 0);
1204 KASSERT(cylno
< fs
->fs_old_ncyl
);
1205 KASSERT(old_cbtorpos(fs
, bno
) >= 0);
1206 KASSERT(fs
->fs_old_nrpos
== 0 || old_cbtorpos(fs
, bno
) < fs
->fs_old_nrpos
);
1207 ufs_add16(old_cg_blks(fs
, cgp
, cylno
, needswap
)[old_cbtorpos(fs
, bno
)], -1,
1209 ufs_add32(old_cg_blktot(cgp
, needswap
)[cylno
], -1, needswap
);
1212 cg
= ufs_rw32(cgp
->cg_cgx
, needswap
);
1213 blkno
= cgbase(fs
, cg
) + bno
;
1218 * Determine whether an inode can be allocated.
1220 * Check to see if an inode is available, and if it is,
1221 * allocate it using the following policy:
1222 * 1) allocate the requested inode.
1223 * 2) allocate the next available inode after the requested
1224 * inode in the specified cylinder group.
1227 ffs_nodealloccg(struct inode
*ip
, int cg
, daddr_t ipref
, int mode
, int flags
)
1229 struct ufsmount
*ump
= ip
->i_ump
;
1230 struct fs
*fs
= ip
->i_fs
;
1232 struct buf
*bp
, *ibp
;
1234 int error
, start
, len
, loc
, map
, i
;
1237 struct ufs2_dinode
*dp2
;
1238 const int needswap
= UFS_FSNEEDSWAP(fs
);
1240 KASSERT(mutex_owned(&ump
->um_lock
));
1241 UFS_WAPBL_JLOCK_ASSERT(ip
->i_ump
->um_mountp
);
1243 if (fs
->fs_cs(fs
, cg
).cs_nifree
== 0)
1245 mutex_exit(&ump
->um_lock
);
1249 error
= bread(ip
->i_devvp
, FFS_FSBTODB(fs
, cgtod(fs
, cg
)),
1250 (int)fs
->fs_cgsize
, B_MODIFY
, &bp
);
1253 cgp
= (struct cg
*)bp
->b_data
;
1254 if (!cg_chkmagic(cgp
, needswap
) || cgp
->cg_cs
.cs_nifree
== 0)
1258 initediblk
!= ufs_rw32(cgp
->cg_initediblk
, needswap
)) {
1259 /* Another thread allocated more inodes so we retry the test. */
1264 * Check to see if we need to initialize more inodes.
1266 if (fs
->fs_magic
== FS_UFS2_MAGIC
&& ibp
== NULL
) {
1267 initediblk
= ufs_rw32(cgp
->cg_initediblk
, needswap
);
1268 nalloc
= fs
->fs_ipg
- ufs_rw32(cgp
->cg_cs
.cs_nifree
, needswap
);
1269 if (nalloc
+ FFS_INOPB(fs
) > initediblk
&&
1270 initediblk
< ufs_rw32(cgp
->cg_niblk
, needswap
)) {
1272 * We have to release the cg buffer here to prevent
1273 * a deadlock when reading the inode block will
1274 * run a copy-on-write that might use this cg.
1278 error
= ffs_getblk(ip
->i_devvp
, FFS_FSBTODB(fs
,
1279 ino_to_fsba(fs
, cg
* fs
->fs_ipg
+ initediblk
)),
1280 FFS_NOBLK
, fs
->fs_bsize
, false, &ibp
);
1287 cgp
->cg_old_time
= ufs_rw32(time_second
, needswap
);
1288 if ((fs
->fs_magic
!= FS_UFS1_MAGIC
) ||
1289 (fs
->fs_old_flags
& FS_FLAGS_UPDATED
))
1290 cgp
->cg_time
= ufs_rw64(time_second
, needswap
);
1291 inosused
= cg_inosused(cgp
, needswap
);
1293 ipref
%= fs
->fs_ipg
;
1294 if (isclr(inosused
, ipref
))
1297 start
= ufs_rw32(cgp
->cg_irotor
, needswap
) / NBBY
;
1298 len
= howmany(fs
->fs_ipg
- ufs_rw32(cgp
->cg_irotor
, needswap
),
1300 loc
= skpc(0xff, len
, &inosused
[start
]);
1304 loc
= skpc(0xff, len
, &inosused
[0]);
1306 printf("cg = %d, irotor = %d, fs = %s\n",
1307 cg
, ufs_rw32(cgp
->cg_irotor
, needswap
),
1309 panic("ffs_nodealloccg: map corrupted");
1313 i
= start
+ len
- loc
;
1314 map
= inosused
[i
] ^ 0xff;
1316 printf("fs = %s\n", fs
->fs_fsmnt
);
1317 panic("ffs_nodealloccg: block not in map");
1319 ipref
= i
* NBBY
+ ffs(map
) - 1;
1320 cgp
->cg_irotor
= ufs_rw32(ipref
, needswap
);
1322 UFS_WAPBL_REGISTER_INODE(ip
->i_ump
->um_mountp
, cg
* fs
->fs_ipg
+ ipref
,
1325 * Check to see if we need to initialize more inodes.
1328 KASSERT(initediblk
== ufs_rw32(cgp
->cg_initediblk
, needswap
));
1329 memset(ibp
->b_data
, 0, fs
->fs_bsize
);
1330 dp2
= (struct ufs2_dinode
*)(ibp
->b_data
);
1331 for (i
= 0; i
< FFS_INOPB(fs
); i
++) {
1333 * Don't bother to swap, it's supposed to be
1334 * random, after all.
1336 dp2
->di_gen
= (cprng_fast32() & INT32_MAX
) / 2 + 1;
1339 initediblk
+= FFS_INOPB(fs
);
1340 cgp
->cg_initediblk
= ufs_rw32(initediblk
, needswap
);
1343 mutex_enter(&ump
->um_lock
);
1344 ACTIVECG_CLR(fs
, cg
);
1345 setbit(inosused
, ipref
);
1346 ufs_add32(cgp
->cg_cs
.cs_nifree
, -1, needswap
);
1347 fs
->fs_cstotal
.cs_nifree
--;
1348 fs
->fs_cs(fs
, cg
).cs_nifree
--;
1350 if ((mode
& IFMT
) == IFDIR
) {
1351 ufs_add32(cgp
->cg_cs
.cs_ndir
, 1, needswap
);
1352 fs
->fs_cstotal
.cs_ndir
++;
1353 fs
->fs_cs(fs
, cg
).cs_ndir
++;
1355 mutex_exit(&ump
->um_lock
);
1361 return (cg
* fs
->fs_ipg
+ ipref
);
1367 mutex_enter(&ump
->um_lock
);
1372 * Allocate a block or fragment.
1374 * The specified block or fragment is removed from the
1375 * free map, possibly fragmenting a block in the process.
1377 * This implementation should mirror fs_blkfree
1379 * => um_lock not held on entry or exit
1382 ffs_blkalloc(struct inode
*ip
, daddr_t bno
, long size
)
1386 error
= ffs_check_bad_allocation(__func__
, ip
->i_fs
, bno
, size
,
1387 ip
->i_dev
, ip
->i_uid
);
1391 return ffs_blkalloc_ump(ip
->i_ump
, bno
, size
);
1395 ffs_blkalloc_ump(struct ufsmount
*ump
, daddr_t bno
, long size
)
1397 struct fs
*fs
= ump
->um_fs
;
1400 int32_t fragno
, cgbno
;
1401 int i
, error
, cg
, blk
, frags
, bbase
;
1403 const int needswap
= UFS_FSNEEDSWAP(fs
);
1405 KASSERT((u_int
)size
<= fs
->fs_bsize
&& ffs_fragoff(fs
, size
) == 0 &&
1406 ffs_fragnum(fs
, bno
) + ffs_numfrags(fs
, size
) <= fs
->fs_frag
);
1407 KASSERT(bno
< fs
->fs_size
);
1410 error
= bread(ump
->um_devvp
, FFS_FSBTODB(fs
, cgtod(fs
, cg
)),
1411 (int)fs
->fs_cgsize
, B_MODIFY
, &bp
);
1415 cgp
= (struct cg
*)bp
->b_data
;
1416 if (!cg_chkmagic(cgp
, needswap
)) {
1420 cgp
->cg_old_time
= ufs_rw32(time_second
, needswap
);
1421 cgp
->cg_time
= ufs_rw64(time_second
, needswap
);
1422 cgbno
= dtogd(fs
, bno
);
1423 blksfree
= cg_blksfree(cgp
, needswap
);
1425 mutex_enter(&ump
->um_lock
);
1426 if (size
== fs
->fs_bsize
) {
1427 fragno
= ffs_fragstoblks(fs
, cgbno
);
1428 if (!ffs_isblock(fs
, blksfree
, fragno
)) {
1429 mutex_exit(&ump
->um_lock
);
1433 ffs_clrblock(fs
, blksfree
, fragno
);
1434 ffs_clusteracct(fs
, cgp
, fragno
, -1);
1435 ufs_add32(cgp
->cg_cs
.cs_nbfree
, -1, needswap
);
1436 fs
->fs_cstotal
.cs_nbfree
--;
1437 fs
->fs_cs(fs
, cg
).cs_nbfree
--;
1439 bbase
= cgbno
- ffs_fragnum(fs
, cgbno
);
1441 frags
= ffs_numfrags(fs
, size
);
1442 for (i
= 0; i
< frags
; i
++) {
1443 if (isclr(blksfree
, cgbno
+ i
)) {
1444 mutex_exit(&ump
->um_lock
);
1450 * if a complete block is being split, account for it
1452 fragno
= ffs_fragstoblks(fs
, bbase
);
1453 if (ffs_isblock(fs
, blksfree
, fragno
)) {
1454 ufs_add32(cgp
->cg_cs
.cs_nffree
, fs
->fs_frag
, needswap
);
1455 fs
->fs_cstotal
.cs_nffree
+= fs
->fs_frag
;
1456 fs
->fs_cs(fs
, cg
).cs_nffree
+= fs
->fs_frag
;
1457 ffs_clusteracct(fs
, cgp
, fragno
, -1);
1458 ufs_add32(cgp
->cg_cs
.cs_nbfree
, -1, needswap
);
1459 fs
->fs_cstotal
.cs_nbfree
--;
1460 fs
->fs_cs(fs
, cg
).cs_nbfree
--;
1463 * decrement the counts associated with the old frags
1465 blk
= blkmap(fs
, blksfree
, bbase
);
1466 ffs_fragacct(fs
, blk
, cgp
->cg_frsum
, -1, needswap
);
1468 * allocate the fragment
1470 for (i
= 0; i
< frags
; i
++) {
1471 clrbit(blksfree
, cgbno
+ i
);
1473 ufs_add32(cgp
->cg_cs
.cs_nffree
, -i
, needswap
);
1474 fs
->fs_cstotal
.cs_nffree
-= i
;
1475 fs
->fs_cs(fs
, cg
).cs_nffree
-= i
;
1477 * add back in counts associated with the new frags
1479 blk
= blkmap(fs
, blksfree
, bbase
);
1480 ffs_fragacct(fs
, blk
, cgp
->cg_frsum
, 1, needswap
);
1483 ACTIVECG_CLR(fs
, cg
);
1484 mutex_exit(&ump
->um_lock
);
1490 * Free a block or fragment.
1492 * The specified block or fragment is placed back in the
1493 * free map. If a fragment is deallocated, a possible
1494 * block reassembly is checked.
1496 * => um_lock not held on entry or exit
1499 ffs_blkfree_cg(struct fs
*fs
, struct vnode
*devvp
, daddr_t bno
, long size
)
1503 struct ufsmount
*ump
;
1507 const bool devvp_is_snapshot
= (devvp
->v_type
!= VBLK
);
1508 const int needswap
= UFS_FSNEEDSWAP(fs
);
1510 KASSERT(!devvp_is_snapshot
);
1513 dev
= devvp
->v_rdev
;
1514 ump
= VFSTOUFS(spec_node_getmountedfs(devvp
));
1515 KASSERT(fs
== ump
->um_fs
);
1516 cgblkno
= FFS_FSBTODB(fs
, cgtod(fs
, cg
));
1518 error
= bread(devvp
, cgblkno
, (int)fs
->fs_cgsize
,
1523 cgp
= (struct cg
*)bp
->b_data
;
1524 if (!cg_chkmagic(cgp
, needswap
)) {
1529 ffs_blkfree_common(ump
, fs
, dev
, bp
, bno
, size
, devvp_is_snapshot
);
1534 struct discardopdata
{
1535 struct work wk
; /* must be first */
1536 struct vnode
*devvp
;
1541 struct discarddata
{
1543 struct discardopdata
*entry
;
1546 struct workqueue
*wq
;
1547 int wqcnt
, wqdraining
;
1550 /* timer for flush? */
1554 ffs_blkfree_td(struct fs
*fs
, struct discardopdata
*td
)
1556 struct mount
*mp
= spec_node_getmountedfs(td
->devvp
);
1561 todo
= min(td
->size
,
1562 ffs_lfragtosize(fs
, (fs
->fs_frag
- ffs_fragnum(fs
, td
->bno
))));
1563 error
= UFS_WAPBL_BEGIN(mp
);
1565 printf("ffs: failed to begin wapbl transaction"
1566 " for discard: %d\n", error
);
1569 ffs_blkfree_cg(fs
, td
->devvp
, td
->bno
, todo
);
1571 td
->bno
+= ffs_numfrags(fs
, todo
);
1577 ffs_discardcb(struct work
*wk
, void *arg
)
1579 struct discardopdata
*td
= (void *)wk
;
1580 struct discarddata
*ts
= arg
;
1581 struct fs
*fs
= ts
->fs
;
1587 /* like FSBTODB but emits bytes; XXX move to fs.h */
1588 #ifndef FFS_FSBTOBYTES
1589 #define FFS_FSBTOBYTES(fs, b) ((b) << (fs)->fs_fshift)
1592 start
= FFS_FSBTOBYTES(fs
, td
->bno
);
1597 VOP_FDISCARD(td
->devvp
, start
, len
);
1599 printf("trim(%" PRId64
",%ld):%d\n", td
->bno
, td
->size
, error
);
1602 ffs_blkfree_td(fs
, td
);
1603 kmem_free(td
, sizeof(*td
));
1604 mutex_enter(&ts
->wqlk
);
1606 if (ts
->wqdraining
&& !ts
->wqcnt
)
1607 cv_signal(&ts
->wqcv
);
1608 mutex_exit(&ts
->wqlk
);
1612 ffs_discard_init(struct vnode
*devvp
, struct fs
*fs
)
1614 struct discarddata
*ts
;
1617 ts
= kmem_zalloc(sizeof (*ts
), KM_SLEEP
);
1618 error
= workqueue_create(&ts
->wq
, "trimwq", ffs_discardcb
, ts
,
1621 kmem_free(ts
, sizeof (*ts
));
1624 mutex_init(&ts
->entrylk
, MUTEX_DEFAULT
, IPL_NONE
);
1625 mutex_init(&ts
->wqlk
, MUTEX_DEFAULT
, IPL_NONE
);
1626 cv_init(&ts
->wqcv
, "trimwqcv");
1627 ts
->maxsize
= 100*1024; /* XXX */
1633 ffs_discard_finish(void *vts
, int flags
)
1635 struct discarddata
*ts
= vts
;
1636 struct discardopdata
*td
= NULL
;
1639 /* wait for workqueue to drain */
1640 mutex_enter(&ts
->wqlk
);
1643 res
= cv_timedwait(&ts
->wqcv
, &ts
->wqlk
, mstohz(5000));
1645 mutex_exit(&ts
->wqlk
);
1647 printf("ffs_discarddata drain timeout\n");
1649 mutex_enter(&ts
->entrylk
);
1654 mutex_exit(&ts
->entrylk
);
1656 /* XXX don't tell disk, its optional */
1657 ffs_blkfree_td(ts
->fs
, td
);
1659 printf("finish(%" PRId64
",%ld)\n", td
->bno
, td
->size
);
1661 kmem_free(td
, sizeof(*td
));
1664 cv_destroy(&ts
->wqcv
);
1665 mutex_destroy(&ts
->entrylk
);
1666 mutex_destroy(&ts
->wqlk
);
1667 workqueue_destroy(ts
->wq
);
1668 kmem_free(ts
, sizeof(*ts
));
1672 ffs_blkfree(struct fs
*fs
, struct vnode
*devvp
, daddr_t bno
, long size
,
1675 struct ufsmount
*ump
;
1678 struct discarddata
*ts
;
1679 struct discardopdata
*td
;
1681 dev
= devvp
->v_rdev
;
1682 ump
= VFSTOUFS(spec_node_getmountedfs(devvp
));
1683 if (ffs_snapblkfree(fs
, devvp
, bno
, size
, inum
))
1686 error
= ffs_check_bad_allocation(__func__
, fs
, bno
, size
, dev
, inum
);
1690 if (!ump
->um_discarddata
) {
1691 ffs_blkfree_cg(fs
, devvp
, bno
, size
);
1696 printf("blkfree(%" PRId64
",%ld)\n", bno
, size
);
1698 ts
= ump
->um_discarddata
;
1701 mutex_enter(&ts
->entrylk
);
1704 /* ffs deallocs backwards, check for prepend only */
1705 if (td
->bno
== bno
+ ffs_numfrags(fs
, size
)
1706 && td
->size
+ size
<= ts
->maxsize
) {
1709 if (td
->size
< ts
->maxsize
) {
1711 printf("defer(%" PRId64
",%ld)\n", td
->bno
, td
->size
);
1713 mutex_exit(&ts
->entrylk
);
1716 size
= 0; /* mark done */
1720 mutex_exit(&ts
->entrylk
);
1724 printf("enq old(%" PRId64
",%ld)\n", td
->bno
, td
->size
);
1726 mutex_enter(&ts
->wqlk
);
1728 mutex_exit(&ts
->wqlk
);
1729 workqueue_enqueue(ts
->wq
, &td
->wk
, NULL
);
1734 td
= kmem_alloc(sizeof(*td
), KM_SLEEP
);
1739 if (td
->size
< ts
->maxsize
) { /* XXX always the case */
1740 mutex_enter(&ts
->entrylk
);
1741 if (!ts
->entry
) { /* possible race? */
1743 printf("defer(%" PRId64
",%ld)\n", td
->bno
, td
->size
);
1748 mutex_exit(&ts
->entrylk
);
1752 printf("enq new(%" PRId64
",%ld)\n", td
->bno
, td
->size
);
1754 mutex_enter(&ts
->wqlk
);
1756 mutex_exit(&ts
->wqlk
);
1757 workqueue_enqueue(ts
->wq
, &td
->wk
, NULL
);
1762 * Free a block or fragment from a snapshot cg copy.
1764 * The specified block or fragment is placed back in the
1765 * free map. If a fragment is deallocated, a possible
1766 * block reassembly is checked.
1768 * => um_lock not held on entry or exit
1771 ffs_blkfree_snap(struct fs
*fs
, struct vnode
*devvp
, daddr_t bno
, long size
,
1776 struct ufsmount
*ump
;
1780 const bool devvp_is_snapshot
= (devvp
->v_type
!= VBLK
);
1781 const int needswap
= UFS_FSNEEDSWAP(fs
);
1783 KASSERT(devvp_is_snapshot
);
1786 dev
= VTOI(devvp
)->i_devvp
->v_rdev
;
1787 ump
= VFSTOUFS(devvp
->v_mount
);
1788 cgblkno
= ffs_fragstoblks(fs
, cgtod(fs
, cg
));
1790 error
= ffs_check_bad_allocation(__func__
, fs
, bno
, size
, dev
, inum
);
1794 error
= bread(devvp
, cgblkno
, (int)fs
->fs_cgsize
,
1799 cgp
= (struct cg
*)bp
->b_data
;
1800 if (!cg_chkmagic(cgp
, needswap
)) {
1805 ffs_blkfree_common(ump
, fs
, dev
, bp
, bno
, size
, devvp_is_snapshot
);
1811 ffs_blkfree_common(struct ufsmount
*ump
, struct fs
*fs
, dev_t dev
,
1812 struct buf
*bp
, daddr_t bno
, long size
, bool devvp_is_snapshot
)
1815 int32_t fragno
, cgbno
;
1816 int i
, cg
, blk
, frags
, bbase
;
1818 const int needswap
= UFS_FSNEEDSWAP(fs
);
1821 cgp
= (struct cg
*)bp
->b_data
;
1822 cgp
->cg_old_time
= ufs_rw32(time_second
, needswap
);
1823 if ((fs
->fs_magic
!= FS_UFS1_MAGIC
) ||
1824 (fs
->fs_old_flags
& FS_FLAGS_UPDATED
))
1825 cgp
->cg_time
= ufs_rw64(time_second
, needswap
);
1826 cgbno
= dtogd(fs
, bno
);
1827 blksfree
= cg_blksfree(cgp
, needswap
);
1828 mutex_enter(&ump
->um_lock
);
1829 if (size
== fs
->fs_bsize
) {
1830 fragno
= ffs_fragstoblks(fs
, cgbno
);
1831 if (!ffs_isfreeblock(fs
, blksfree
, fragno
)) {
1832 if (devvp_is_snapshot
) {
1833 mutex_exit(&ump
->um_lock
);
1836 printf("dev = 0x%llx, block = %" PRId64
", fs = %s\n",
1837 (unsigned long long)dev
, bno
, fs
->fs_fsmnt
);
1838 panic("blkfree: freeing free block");
1840 ffs_setblock(fs
, blksfree
, fragno
);
1841 ffs_clusteracct(fs
, cgp
, fragno
, 1);
1842 ufs_add32(cgp
->cg_cs
.cs_nbfree
, 1, needswap
);
1843 fs
->fs_cstotal
.cs_nbfree
++;
1844 fs
->fs_cs(fs
, cg
).cs_nbfree
++;
1845 if ((fs
->fs_magic
== FS_UFS1_MAGIC
) &&
1846 ((fs
->fs_old_flags
& FS_FLAGS_UPDATED
) == 0)) {
1847 i
= old_cbtocylno(fs
, cgbno
);
1849 KASSERT(i
< fs
->fs_old_ncyl
);
1850 KASSERT(old_cbtorpos(fs
, cgbno
) >= 0);
1851 KASSERT(fs
->fs_old_nrpos
== 0 || old_cbtorpos(fs
, cgbno
) < fs
->fs_old_nrpos
);
1852 ufs_add16(old_cg_blks(fs
, cgp
, i
, needswap
)[old_cbtorpos(fs
, cgbno
)], 1,
1854 ufs_add32(old_cg_blktot(cgp
, needswap
)[i
], 1, needswap
);
1857 bbase
= cgbno
- ffs_fragnum(fs
, cgbno
);
1859 * decrement the counts associated with the old frags
1861 blk
= blkmap(fs
, blksfree
, bbase
);
1862 ffs_fragacct(fs
, blk
, cgp
->cg_frsum
, -1, needswap
);
1864 * deallocate the fragment
1866 frags
= ffs_numfrags(fs
, size
);
1867 for (i
= 0; i
< frags
; i
++) {
1868 if (isset(blksfree
, cgbno
+ i
)) {
1869 printf("dev = 0x%llx, block = %" PRId64
1871 (unsigned long long)dev
, bno
+ i
,
1873 panic("blkfree: freeing free frag");
1875 setbit(blksfree
, cgbno
+ i
);
1877 ufs_add32(cgp
->cg_cs
.cs_nffree
, i
, needswap
);
1878 fs
->fs_cstotal
.cs_nffree
+= i
;
1879 fs
->fs_cs(fs
, cg
).cs_nffree
+= i
;
1881 * add back in counts associated with the new frags
1883 blk
= blkmap(fs
, blksfree
, bbase
);
1884 ffs_fragacct(fs
, blk
, cgp
->cg_frsum
, 1, needswap
);
1886 * if a complete block has been reassembled, account for it
1888 fragno
= ffs_fragstoblks(fs
, bbase
);
1889 if (ffs_isblock(fs
, blksfree
, fragno
)) {
1890 ufs_add32(cgp
->cg_cs
.cs_nffree
, -fs
->fs_frag
, needswap
);
1891 fs
->fs_cstotal
.cs_nffree
-= fs
->fs_frag
;
1892 fs
->fs_cs(fs
, cg
).cs_nffree
-= fs
->fs_frag
;
1893 ffs_clusteracct(fs
, cgp
, fragno
, 1);
1894 ufs_add32(cgp
->cg_cs
.cs_nbfree
, 1, needswap
);
1895 fs
->fs_cstotal
.cs_nbfree
++;
1896 fs
->fs_cs(fs
, cg
).cs_nbfree
++;
1897 if ((fs
->fs_magic
== FS_UFS1_MAGIC
) &&
1898 ((fs
->fs_old_flags
& FS_FLAGS_UPDATED
) == 0)) {
1899 i
= old_cbtocylno(fs
, bbase
);
1901 KASSERT(i
< fs
->fs_old_ncyl
);
1902 KASSERT(old_cbtorpos(fs
, bbase
) >= 0);
1903 KASSERT(fs
->fs_old_nrpos
== 0 || old_cbtorpos(fs
, bbase
) < fs
->fs_old_nrpos
);
1904 ufs_add16(old_cg_blks(fs
, cgp
, i
, needswap
)[old_cbtorpos(fs
,
1905 bbase
)], 1, needswap
);
1906 ufs_add32(old_cg_blktot(cgp
, needswap
)[i
], 1, needswap
);
1911 ACTIVECG_CLR(fs
, cg
);
1912 mutex_exit(&ump
->um_lock
);
1919 ffs_vfree(struct vnode
*vp
, ino_t ino
, int mode
)
1922 return ffs_freefile(vp
->v_mount
, ino
, mode
);
1926 * Do the actual free operation.
1927 * The specified inode is placed back in the free map.
1929 * => um_lock not held on entry or exit
1932 ffs_freefile(struct mount
*mp
, ino_t ino
, int mode
)
1934 struct ufsmount
*ump
= VFSTOUFS(mp
);
1935 struct fs
*fs
= ump
->um_fs
;
1936 struct vnode
*devvp
;
1942 const int needswap
= UFS_FSNEEDSWAP(fs
);
1944 cg
= ino_to_cg(fs
, ino
);
1945 devvp
= ump
->um_devvp
;
1946 dev
= devvp
->v_rdev
;
1947 cgbno
= FFS_FSBTODB(fs
, cgtod(fs
, cg
));
1949 if ((u_int
)ino
>= fs
->fs_ipg
* fs
->fs_ncg
)
1950 panic("ifree: range: dev = 0x%llx, ino = %llu, fs = %s",
1951 (long long)dev
, (unsigned long long)ino
, fs
->fs_fsmnt
);
1952 error
= bread(devvp
, cgbno
, (int)fs
->fs_cgsize
,
1957 cgp
= (struct cg
*)bp
->b_data
;
1958 if (!cg_chkmagic(cgp
, needswap
)) {
1963 ffs_freefile_common(ump
, fs
, dev
, bp
, ino
, mode
, false);
1971 ffs_freefile_snap(struct fs
*fs
, struct vnode
*devvp
, ino_t ino
, int mode
)
1973 struct ufsmount
*ump
;
1979 const int needswap
= UFS_FSNEEDSWAP(fs
);
1981 KASSERT(devvp
->v_type
!= VBLK
);
1983 cg
= ino_to_cg(fs
, ino
);
1984 dev
= VTOI(devvp
)->i_devvp
->v_rdev
;
1985 ump
= VFSTOUFS(devvp
->v_mount
);
1986 cgbno
= ffs_fragstoblks(fs
, cgtod(fs
, cg
));
1987 if ((u_int
)ino
>= fs
->fs_ipg
* fs
->fs_ncg
)
1988 panic("ifree: range: dev = 0x%llx, ino = %llu, fs = %s",
1989 (unsigned long long)dev
, (unsigned long long)ino
,
1991 error
= bread(devvp
, cgbno
, (int)fs
->fs_cgsize
,
1996 cgp
= (struct cg
*)bp
->b_data
;
1997 if (!cg_chkmagic(cgp
, needswap
)) {
2001 ffs_freefile_common(ump
, fs
, dev
, bp
, ino
, mode
, true);
2009 ffs_freefile_common(struct ufsmount
*ump
, struct fs
*fs
, dev_t dev
,
2010 struct buf
*bp
, ino_t ino
, int mode
, bool devvp_is_snapshot
)
2015 const int needswap
= UFS_FSNEEDSWAP(fs
);
2017 cg
= ino_to_cg(fs
, ino
);
2018 cgp
= (struct cg
*)bp
->b_data
;
2019 cgp
->cg_old_time
= ufs_rw32(time_second
, needswap
);
2020 if ((fs
->fs_magic
!= FS_UFS1_MAGIC
) ||
2021 (fs
->fs_old_flags
& FS_FLAGS_UPDATED
))
2022 cgp
->cg_time
= ufs_rw64(time_second
, needswap
);
2023 inosused
= cg_inosused(cgp
, needswap
);
2025 if (isclr(inosused
, ino
)) {
2026 printf("ifree: dev = 0x%llx, ino = %llu, fs = %s\n",
2027 (unsigned long long)dev
, (unsigned long long)ino
+
2028 cg
* fs
->fs_ipg
, fs
->fs_fsmnt
);
2029 if (fs
->fs_ronly
== 0)
2030 panic("ifree: freeing free inode");
2032 clrbit(inosused
, ino
);
2033 if (!devvp_is_snapshot
)
2034 UFS_WAPBL_UNREGISTER_INODE(ump
->um_mountp
,
2035 ino
+ cg
* fs
->fs_ipg
, mode
);
2036 if (ino
< ufs_rw32(cgp
->cg_irotor
, needswap
))
2037 cgp
->cg_irotor
= ufs_rw32(ino
, needswap
);
2038 ufs_add32(cgp
->cg_cs
.cs_nifree
, 1, needswap
);
2039 mutex_enter(&ump
->um_lock
);
2040 fs
->fs_cstotal
.cs_nifree
++;
2041 fs
->fs_cs(fs
, cg
).cs_nifree
++;
2042 if ((mode
& IFMT
) == IFDIR
) {
2043 ufs_add32(cgp
->cg_cs
.cs_ndir
, -1, needswap
);
2044 fs
->fs_cstotal
.cs_ndir
--;
2045 fs
->fs_cs(fs
, cg
).cs_ndir
--;
2048 ACTIVECG_CLR(fs
, cg
);
2049 mutex_exit(&ump
->um_lock
);
2053 * Check to see if a file is free.
2056 ffs_checkfreefile(struct fs
*fs
, struct vnode
*devvp
, ino_t ino
)
2063 const bool devvp_is_snapshot
= (devvp
->v_type
!= VBLK
);
2065 KASSERT(devvp_is_snapshot
);
2067 cg
= ino_to_cg(fs
, ino
);
2068 if (devvp_is_snapshot
)
2069 cgbno
= ffs_fragstoblks(fs
, cgtod(fs
, cg
));
2071 cgbno
= FFS_FSBTODB(fs
, cgtod(fs
, cg
));
2072 if ((u_int
)ino
>= fs
->fs_ipg
* fs
->fs_ncg
)
2074 if (bread(devvp
, cgbno
, (int)fs
->fs_cgsize
, 0, &bp
)) {
2077 cgp
= (struct cg
*)bp
->b_data
;
2078 if (!cg_chkmagic(cgp
, UFS_FSNEEDSWAP(fs
))) {
2082 inosused
= cg_inosused(cgp
, UFS_FSNEEDSWAP(fs
));
2084 ret
= isclr(inosused
, ino
);
2090 * Find a block of the specified size in the specified cylinder group.
2092 * It is a panic if a request is made to find a block if none are
2096 ffs_mapsearch(struct fs
*fs
, struct cg
*cgp
, daddr_t bpref
, int allocsiz
)
2099 int start
, len
, loc
, i
;
2100 int blk
, field
, subfield
, pos
;
2103 const int needswap
= UFS_FSNEEDSWAP(fs
);
2105 /* KASSERT(mutex_owned(&ump->um_lock)); */
2108 * find the fragment by searching through the free block
2109 * map for an appropriate bit pattern
2112 start
= dtogd(fs
, bpref
) / NBBY
;
2114 start
= ufs_rw32(cgp
->cg_frotor
, needswap
) / NBBY
;
2115 blksfree
= cg_blksfree(cgp
, needswap
);
2116 len
= howmany(fs
->fs_fpg
, NBBY
) - start
;
2119 loc
= scanc((u_int
)len
,
2120 (const u_char
*)&blksfree
[start
],
2121 (const u_char
*)fragtbl
[fs
->fs_frag
],
2122 (1 << (allocsiz
- 1 + (fs
->fs_frag
& (NBBY
- 1)))));
2126 loc
= scanc((u_int
)len
,
2127 (const u_char
*)&blksfree
[0],
2128 (const u_char
*)fragtbl
[fs
->fs_frag
],
2129 (1 << (allocsiz
- 1 + (fs
->fs_frag
& (NBBY
- 1)))));
2131 printf("start = %d, len = %d, fs = %s\n",
2132 ostart
, olen
, fs
->fs_fsmnt
);
2133 printf("offset=%d %ld\n",
2134 ufs_rw32(cgp
->cg_freeoff
, needswap
),
2135 (long)blksfree
- (long)cgp
);
2136 printf("cg %d\n", cgp
->cg_cgx
);
2137 panic("ffs_alloccg: map corrupted");
2141 bno
= (start
+ len
- loc
) * NBBY
;
2142 cgp
->cg_frotor
= ufs_rw32(bno
, needswap
);
2144 * found the byte in the map
2145 * sift through the bits to find the selected frag
2147 for (i
= bno
+ NBBY
; bno
< i
; bno
+= fs
->fs_frag
) {
2148 blk
= blkmap(fs
, blksfree
, bno
);
2150 field
= around
[allocsiz
];
2151 subfield
= inside
[allocsiz
];
2152 for (pos
= 0; pos
<= fs
->fs_frag
- allocsiz
; pos
++) {
2153 if ((blk
& field
) == subfield
)
2159 printf("bno = %d, fs = %s\n", bno
, fs
->fs_fsmnt
);
2160 panic("ffs_alloccg: block not in map");
2165 * Fserr prints the name of a file system with an error diagnostic.
2167 * The form of the error message is:
2171 ffs_fserr(struct fs
*fs
, kauth_cred_t cred
, const char *cp
)
2173 KASSERT(cred
!= NULL
);
2175 if (cred
== NOCRED
|| cred
== FSCRED
) {
2176 log(LOG_ERR
, "pid %d, command %s, on %s: %s\n",
2177 curproc
->p_pid
, curproc
->p_comm
,
2180 log(LOG_ERR
, "uid %d, pid %d, command %s, on %s: %s\n",
2181 kauth_cred_getuid(cred
), curproc
->p_pid
, curproc
->p_comm
,