@@ -81,7 +81,8 @@ void ufs_free_fragments(struct inode *inode, u64 fragment, unsigned count)
8181 ufs_error (sb , "ufs_free_fragments" ,
8282 "bit already cleared for fragment %u" , i );
8383 }
84-
84+
85+ inode_sub_bytes (inode , count << uspi -> s_fshift );
8586 fs32_add (sb , & ucg -> cg_cs .cs_nffree , count );
8687 uspi -> cs_total .cs_nffree += count ;
8788 fs32_add (sb , & UFS_SB (sb )-> fs_cs (cgno ).cs_nffree , count );
@@ -183,6 +184,7 @@ void ufs_free_blocks(struct inode *inode, u64 fragment, unsigned count)
183184 ufs_error (sb , "ufs_free_blocks" , "freeing free fragment" );
184185 }
185186 ubh_setblock (UCPI_UBH (ucpi ), ucpi -> c_freeoff , blkno );
187+ inode_sub_bytes (inode , uspi -> s_fpb << uspi -> s_fshift );
186188 if ((UFS_SB (sb )-> s_flags & UFS_CG_MASK ) == UFS_CG_44BSD )
187189 ufs_clusteracct (sb , ucpi , blkno , 1 );
188190
@@ -494,6 +496,20 @@ u64 ufs_new_fragments(struct inode *inode, void *p, u64 fragment,
494496 return 0 ;
495497}
496498
499+ static bool try_add_frags (struct inode * inode , unsigned frags )
500+ {
501+ unsigned size = frags * i_blocksize (inode );
502+ spin_lock (& inode -> i_lock );
503+ __inode_add_bytes (inode , size );
504+ if (unlikely ((u32 )inode -> i_blocks != inode -> i_blocks )) {
505+ __inode_sub_bytes (inode , size );
506+ spin_unlock (& inode -> i_lock );
507+ return false;
508+ }
509+ spin_unlock (& inode -> i_lock );
510+ return true;
511+ }
512+
497513static u64 ufs_add_fragments (struct inode * inode , u64 fragment ,
498514 unsigned oldcount , unsigned newcount )
499515{
@@ -530,6 +546,9 @@ static u64 ufs_add_fragments(struct inode *inode, u64 fragment,
530546 for (i = oldcount ; i < newcount ; i ++ )
531547 if (ubh_isclr (UCPI_UBH (ucpi ), ucpi -> c_freeoff , fragno + i ))
532548 return 0 ;
549+
550+ if (!try_add_frags (inode , count ))
551+ return 0 ;
533552 /*
534553 * Block can be extended
535554 */
@@ -647,6 +666,7 @@ static u64 ufs_alloc_fragments(struct inode *inode, unsigned cgno,
647666 ubh_setbit (UCPI_UBH (ucpi ), ucpi -> c_freeoff , goal + i );
648667 i = uspi -> s_fpb - count ;
649668
669+ inode_sub_bytes (inode , i << uspi -> s_fshift );
650670 fs32_add (sb , & ucg -> cg_cs .cs_nffree , i );
651671 uspi -> cs_total .cs_nffree += i ;
652672 fs32_add (sb , & UFS_SB (sb )-> fs_cs (cgno ).cs_nffree , i );
@@ -657,6 +677,8 @@ static u64 ufs_alloc_fragments(struct inode *inode, unsigned cgno,
657677 result = ufs_bitmap_search (sb , ucpi , goal , allocsize );
658678 if (result == INVBLOCK )
659679 return 0 ;
680+ if (!try_add_frags (inode , count ))
681+ return 0 ;
660682 for (i = 0 ; i < count ; i ++ )
661683 ubh_clrbit (UCPI_UBH (ucpi ), ucpi -> c_freeoff , result + i );
662684
@@ -716,6 +738,8 @@ static u64 ufs_alloccg_block(struct inode *inode,
716738 return INVBLOCK ;
717739 ucpi -> c_rotor = result ;
718740gotit :
741+ if (!try_add_frags (inode , uspi -> s_fpb ))
742+ return 0 ;
719743 blkno = ufs_fragstoblks (result );
720744 ubh_clrblock (UCPI_UBH (ucpi ), ucpi -> c_freeoff , blkno );
721745 if ((UFS_SB (sb )-> s_flags & UFS_CG_MASK ) == UFS_CG_44BSD )
0 commit comments