1- /* $NetBSD: lfs_alloc.c,v 1.147 2025/11/03 22:21:12 perseant Exp $ */
1+ /* $NetBSD: lfs_alloc.c,v 1.148 2025/12/02 01:23:09 perseant Exp $ */
22
33/*-
44 * Copyright (c) 1999, 2000, 2001, 2002, 2003, 2007 The NetBSD Foundation, Inc.
6060 */
6161
6262#include <sys/cdefs.h>
63- __KERNEL_RCSID (0 , "$NetBSD: lfs_alloc.c,v 1.147 2025/11/03 22:21:12 perseant Exp $" );
63+ __KERNEL_RCSID (0 , "$NetBSD: lfs_alloc.c,v 1.148 2025/12/02 01:23:09 perseant Exp $" );
6464
6565#if defined(_KERNEL_OPT )
6666#include "opt_quota.h"
@@ -305,7 +305,7 @@ lfs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred,
305305 */
306306 lfs_if_setdaddr (fs , ifp , LFS_ILLEGAL_DADDR );
307307 lfs_if_setnextfree (fs , ifp , LFS_UNUSED_INUM );
308- LFS_BWRITE_LOG ( bp );
308+ LFS_WRITEIENTRY ( ifp , fs , * ino , bp );
309309
310310 if (lfs_sb_getfreehd (fs ) == LFS_UNUSED_INUM ) {
311311 /*
@@ -448,7 +448,7 @@ lfs_valloc_fixed(struct lfs *fs, ino_t ino, int vers)
448448 lfs_if_setnextfree (fs , ifp , LFS_UNUSED_INUM );
449449 /* See comment in lfs_valloc */
450450 lfs_if_setdaddr (fs , ifp , LFS_ILLEGAL_DADDR );
451- LFS_BWRITE_LOG ( bp );
451+ LFS_WRITEIENTRY ( ifp , fs , ino , bp );
452452
453453 if (lfs_sb_getfreehd (fs ) == LFS_UNUSED_INUM ) {
454454 int error ;
@@ -556,7 +556,6 @@ lfs_freelist_prev(struct lfs *fs, ino_t ino)
556556int
557557lfs_vfree (struct vnode * vp , ino_t ino , int mode )
558558{
559- SEGUSE * sup ;
560559 CLEANERINFO * cip ;
561560 struct buf * cbp , * bp ;
562561 IFILE * ifp ;
@@ -569,9 +568,10 @@ lfs_vfree(struct vnode *vp, ino_t ino, int mode)
569568 fs = ip -> i_lfs ;
570569 ino = ip -> i_number ;
571570
572- /* XXX: assert not readonly */
573-
574571 ASSERT_NO_SEGLOCK (fs );
572+ KASSERTMSG ((ino != LFS_UNUSED_INUM ), "inode 0 freed" );
573+ KASSERT (!fs -> lfs_ronly );
574+
575575 DLOG ((DLOG_ALLOC , "lfs_vfree: free ino %lld\n" , (long long )ino ));
576576
577577 /* Drain of pending writes */
@@ -668,7 +668,7 @@ lfs_vfree(struct vnode *vp, ino_t ino, int mode)
668668 LFS_PUT_HEADFREE (fs , cip , cbp , ino );
669669
670670 /* write the ifile block */
671- ( void ) LFS_BWRITE_LOG ( bp ); /* Ifile */
671+ LFS_WRITEIENTRY ( ifp , fs , ino , bp );
672672#if 0
673673 } else {
674674 ino_t tino , onf , otail ;
@@ -679,7 +679,7 @@ lfs_vfree(struct vnode *vp, ino_t ino, int mode)
679679 * it seems both silly and dangerous.
680680 */
681681 lfs_if_setnextfree (fs , ifp , LFS_UNUSED_INUM );
682- ( void ) LFS_BWRITE_LOG ( bp ); /* Ifile */
682+ LFS_WRITEIENTRY ( ifp , fs , ino , bp );
683683
684684 /*
685685 * Insert on freelist in order.
@@ -704,7 +704,7 @@ lfs_vfree(struct vnode *vp, ino_t ino, int mode)
704704 DLOG ((DLOG_ALLOC , "lfs_vfree: headfree %lld -> %lld\n" ,
705705 (long long )nextfree , (long long )ino ));
706706 /* write the ifile block */
707- LFS_BWRITE_LOG ( bp ); /* Ifile */
707+ LFS_WRITEIENTRY ( ifp , fs , ino , bp );
708708
709709 /* If the list was empty, set tail too */
710710 LFS_GET_TAILFREE (fs , cip , cbp , & otail );
@@ -730,14 +730,14 @@ lfs_vfree(struct vnode *vp, ino_t ino, int mode)
730730 onf = lfs_if_getnextfree (fs , ifp );
731731 lfs_if_setnextfree (fs , ifp , ino );
732732 /* write the block */
733- LFS_BWRITE_LOG ( bp ); /* Ifile */
733+ LFS_WRITEIENTRY ( ifp , fs , tino , bp );
734734
735735 /* load this inode's ifile block */
736736 LFS_IENTRY (ifp , fs , ino , bp );
737737 /* update the list pointer */
738738 lfs_if_setnextfree (fs , ifp , onf );
739739 /* write the block */
740- LFS_BWRITE_LOG ( bp ); /* Ifile */
740+ LFS_WRITEIENTRY ( ifp , fs , tino , bp );
741741
742742 /* If we're last, put us on the tail */
743743 if (onf == LFS_UNUSED_INUM ) {
@@ -750,27 +750,12 @@ lfs_vfree(struct vnode *vp, ino_t ino, int mode)
750750 }
751751 }
752752#endif
753- /* XXX: shouldn't this check be further up *before* we trash the fs? */
754- KASSERTMSG ((ino != LFS_UNUSED_INUM ), "inode 0 freed" );
755753
756754 /*
757755 * Update the segment summary for the segment where the on-disk
758756 * copy used to be.
759757 */
760- if (!DADDR_IS_BAD (old_iaddr )) {
761- /* load it */
762- LFS_SEGENTRY (sup , fs , lfs_dtosn (fs , old_iaddr ), bp );
763- /* the number of bytes in the segment should not become < 0 */
764- KASSERTMSG ((sup -> su_nbytes >= DINOSIZE (fs )),
765- "lfs_vfree: negative byte count"
766- " (segment %" PRIu32 " short by %d)\n" ,
767- lfs_dtosn (fs , old_iaddr ),
768- (int )DINOSIZE (fs ) - sup -> su_nbytes );
769- /* update the number of bytes in the segment */
770- sup -> su_nbytes -= DINOSIZE (fs );
771- /* write the segment entry */
772- LFS_WRITESEGENTRY (sup , fs , lfs_dtosn (fs , old_iaddr ), bp ); /* Ifile */
773- }
758+ lfs_subtract_inode (fs , ip -> i_number , old_iaddr );
774759
775760 /* Set superblock modified bit. */
776761 mutex_enter (& lfs_lock );
@@ -889,7 +874,7 @@ lfs_order_freelist(struct lfs *fs, ino_t **orphanp, size_t *norphanp)
889874 /* set the list pointer */
890875 lfs_if_setnextfree (fs , ifp , ino );
891876 /* write the block */
892- LFS_BWRITE_LOG ( bp );
877+ LFS_WRITEIENTRY ( ifp , fs , lastino , bp );
893878
894879 /* reload this inode's ifile entry */
895880 LFS_IENTRY (ifp , fs , ino , bp );
@@ -984,15 +969,18 @@ lfs_orphan(struct lfs *fs, struct vnode *vp)
984969 mutex_exit (vp -> v_interlock );
985970
986971 /* If not already done, mark this inode orphaned. */
972+ rw_enter (& fs -> lfs_fraglock , RW_READER );
987973 LFS_IENTRY (ifp , fs , ip -> i_number , bp );
988974 nextfree = lfs_if_getnextfree (fs , ifp );
989975 if (nextfree == LFS_ORPHAN_NEXTFREE (fs )) {
990976 brelse (bp , 0 );
977+ rw_exit (& fs -> lfs_fraglock );
991978 return ;
992979 }
993980 KASSERT (nextfree == LFS_UNUSED_INUM );
994981 lfs_if_setnextfree (fs , ifp , LFS_ORPHAN_NEXTFREE (fs ));
995- LFS_BWRITE_LOG (bp );
982+ LFS_WRITEIENTRY (ifp , fs , ip -> i_number , bp );
983+ rw_exit (& fs -> lfs_fraglock );
996984}
997985
998986/*
@@ -1057,6 +1045,8 @@ lfs_free_orphans(struct lfs *fs, ino_t *orphan, size_t norphan)
10571045 error );
10581046 vput (vp );
10591047
1048+ rw_enter (& fs -> lfs_fraglock , RW_READER );
1049+
10601050 /* Update the number of bytes in the segment summary. */
10611051 LFS_SEGENTRY (sup , fs , segno , bp );
10621052 KASSERT (sup -> su_nbytes >= DINOSIZE (fs ));
@@ -1066,7 +1056,9 @@ lfs_free_orphans(struct lfs *fs, ino_t *orphan, size_t norphan)
10661056 /* Drop the on-disk address. */
10671057 LFS_IENTRY (ifp , fs , ino , bp );
10681058 lfs_if_setdaddr (fs , ifp , LFS_UNUSED_DADDR );
1069- LFS_BWRITE_LOG (bp );
1059+ LFS_WRITEIENTRY (ifp , fs , ino , bp );
1060+
1061+ rw_exit (& fs -> lfs_fraglock );
10701062 }
10711063
10721064 if (orphan )
0 commit comments