/* * sfs_bmap_free_nolock - free a block with logical index in inode and reset the inode's fields */ static int sfs_bmap_free_nolock(struct sfs_fs *sfs, struct sfs_inode *sin, uint32_t index) { struct sfs_disk_inode *din = sin->din; int ret; uint32_t ent, ino; if (index < SFS_NDIRECT) { if ((ino = din->direct[index]) != 0) { // free the block sfs_block_free(sfs, ino); din->direct[index] = 0; sin->dirty = 1; } return 0; } index -= SFS_NDIRECT; if (index < SFS_BLK_NENTRY) { if ((ent = din->indirect) != 0) { // set the entry item to 0 in the indirect block if ((ret = sfs_bmap_free_sub_nolock(sfs, ent, index)) != 0) { return ret; } } return 0; } return 0; }
static int sfs_reclaim(struct inode *node) { struct sfs_fs *sfs = fsop_info(vop_fs(node), sfs); struct sfs_inode *sin = vop_info(node, sfs_inode); lock_sfs_fs(sfs); int ret = -E_BUSY; assert(sin->reclaim_count > 0); if ((-- sin->reclaim_count) != 0) { goto failed_unlock; } assert(inode_ref_count(node) == 0 && inode_open_count(node) == 0); if (sin->din->nlinks == 0) { uint32_t nblks; for (nblks = sin->din->blocks; nblks != 0; nblks --) { sfs_bmap_truncate_nolock(sfs, sin); } } else if (sin->dirty) { if ((ret = vop_fsync(node)) != 0) { goto failed_unlock; } } sfs_remove_links(sin); unlock_sfs_fs(sfs); if (sin->din->nlinks == 0) { sfs_block_free(sfs, sin->ino); uint32_t ent; if ((ent = sin->din->indirect) != 0) { sfs_block_free(sfs, ent); } if ((ent = sin->din->db_indirect) != 0) { int i; for (i = 0; i < SFS_BLK_NENTRY; i ++) { sfs_bmap_free_sub_nolock(sfs, ent, i); } sfs_block_free(sfs, ent); } } kfree(sin->din); vop_kill(node); return 0; failed_unlock: unlock_sfs_fs(sfs); return ret; }
static int sfs_bmap_free_nolock(struct sfs_fs *sfs, struct sfs_inode *sin, uint32_t index) { struct sfs_disk_inode *din = sin->din; int ret; uint32_t ent, ino; if (index < SFS_NDIRECT) { if ((ino = din->direct[index]) != 0) { sfs_block_free(sfs, ino); din->direct[index] = 0; sin->dirty = 1; } return 0; } index -= SFS_NDIRECT; if (index < SFS_BLK_NENTRY) { if ((ent = din->indirect) != 0) { if ((ret = sfs_bmap_free_sub_nolock(sfs, ent, index)) != 0) { return ret; } } return 0; } index -= SFS_BLK_NENTRY; if ((ent = din->db_indirect) != 0) { if ((ret = sfs_bmap_get_sub_nolock(sfs, &ent, index / SFS_BLK_NENTRY, 0, &ino)) != 0) { return ret; } if ((ent = ino) != 0) { if ((ret = sfs_bmap_free_sub_nolock(sfs, ent, index % SFS_BLK_NENTRY)) != 0) { return ret; } } } return 0; }