From: Darrick J. Wong <djwong@xxxxxxxxxx> Use the iroot reset function to reset root inodes instead of open-coding the reset routine. While we're at it, fix a longstanding memory leak if the inode being reset actually had an xattr fork full of mappings. Signed-off-by: Darrick J. Wong <djwong@xxxxxxxxxx> --- libxfs/libxfs_api_defs.h | 2 + repair/phase6.c | 126 +++++++++------------------------------------- 2 files changed, 28 insertions(+), 100 deletions(-) diff --git a/libxfs/libxfs_api_defs.h b/libxfs/libxfs_api_defs.h index 782a551ee1c..2b2b958d8a9 100644 --- a/libxfs/libxfs_api_defs.h +++ b/libxfs/libxfs_api_defs.h @@ -133,6 +133,7 @@ #define xfs_dquot_verify libxfs_dquot_verify #define xfs_finobt_calc_reserves libxfs_finobt_calc_reserves +#define xfs_fixed_inode_reset libxfs_fixed_inode_reset #define xfs_free_extent libxfs_free_extent #define xfs_free_perag libxfs_free_perag #define xfs_fs_geometry libxfs_fs_geometry @@ -159,6 +160,7 @@ #define xfs_inobt_stage_cursor libxfs_inobt_stage_cursor #define xfs_inode_from_disk libxfs_inode_from_disk #define xfs_inode_from_disk_ts libxfs_inode_from_disk_ts +#define xfs_inode_init libxfs_inode_init #define xfs_inode_to_disk libxfs_inode_to_disk #define xfs_inode_validate_cowextsize libxfs_inode_validate_cowextsize #define xfs_inode_validate_extsize libxfs_inode_validate_extsize diff --git a/repair/phase6.c b/repair/phase6.c index 0c24cfbf144..5765c2b1250 100644 --- a/repair/phase6.c +++ b/repair/phase6.c @@ -445,20 +445,28 @@ res_failed( do_error(_("xfs_trans_reserve returned %d\n"), err); } -static inline void -reset_inode_fields(struct xfs_inode *ip) +/* + * Forcibly reinitialize a fixed-location inode, such as a filesystem root + * directory or the realtime metadata inodes. The inode must not otherwise be + * in use; the data fork must be empty, and the attr fork will be reset. + */ +static void +reset_root_ino( + struct xfs_trans *tp, + umode_t mode, + struct xfs_inode *ip) { - ip->i_projid = 0; - ip->i_disk_size = 0; - ip->i_nblocks = 0; - ip->i_extsize = 0; - ip->i_cowextsize = 0; - ip->i_flushiter = 0; + struct xfs_icreate_args args = { + .nlink = S_ISDIR(mode) ? 2 : 1, + }; + + libxfs_icreate_args_rootfile(&args, mode); + + /* Erase the attr fork since libxfs_inode_init won't do it for us. */ ip->i_forkoff = 0; - ip->i_diflags = 0; - ip->i_diflags2 = 0; - ip->i_crtime.tv_sec = 0; - ip->i_crtime.tv_nsec = 0; + libxfs_ifork_zap_attr(ip); + + libxfs_inode_init(tp, &args, ip); } static void @@ -472,7 +480,6 @@ mk_rbmino(xfs_mount_t *mp) int error; xfs_fileoff_t bno; xfs_bmbt_irec_t map[XFS_BMAP_MAX_NMAP]; - int times; uint blocks; /* @@ -489,34 +496,9 @@ mk_rbmino(xfs_mount_t *mp) error); } - reset_inode_fields(ip); - - VFS_I(ip)->i_mode = S_IFREG; - ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS; - libxfs_ifork_zap_attr(ip); - - set_nlink(VFS_I(ip), 1); /* account for sb ptr */ - - times = XFS_ICHGTIME_CHG | XFS_ICHGTIME_MOD; - if (xfs_has_v3inodes(mp)) { - VFS_I(ip)->i_version = 1; - ip->i_diflags2 = 0; - times |= XFS_ICHGTIME_CREATE; - } - libxfs_trans_ichgtime(tp, ip, times); - - /* - * now the ifork - */ - ip->i_df.if_bytes = 0; - ip->i_df.if_u1.if_root = NULL; - + /* Reset the realtime bitmap inode. */ + reset_root_ino(tp, S_IFREG, ip); ip->i_disk_size = mp->m_sb.sb_rbmblocks * mp->m_sb.sb_blocksize; - - /* - * commit changes - */ - libxfs_trans_ijoin(tp, ip, 0); libxfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); error = -libxfs_trans_commit(tp); if (error) @@ -711,7 +693,6 @@ mk_rsumino(xfs_mount_t *mp) int nsumblocks; xfs_fileoff_t bno; xfs_bmbt_irec_t map[XFS_BMAP_MAX_NMAP]; - int times; uint blocks; /* @@ -728,34 +709,9 @@ mk_rsumino(xfs_mount_t *mp) error); } - reset_inode_fields(ip); - - VFS_I(ip)->i_mode = S_IFREG; - ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS; - libxfs_ifork_zap_attr(ip); - - set_nlink(VFS_I(ip), 1); /* account for sb ptr */ - - times = XFS_ICHGTIME_CHG | XFS_ICHGTIME_MOD; - if (xfs_has_v3inodes(mp)) { - VFS_I(ip)->i_version = 1; - ip->i_diflags2 = 0; - times |= XFS_ICHGTIME_CREATE; - } - libxfs_trans_ichgtime(tp, ip, times); - - /* - * now the ifork - */ - ip->i_df.if_bytes = 0; - ip->i_df.if_u1.if_root = NULL; - + /* Reset the rt summary inode. */ + reset_root_ino(tp, S_IFREG, ip); ip->i_disk_size = mp->m_rsumsize; - - /* - * commit changes - */ - libxfs_trans_ijoin(tp, ip, 0); libxfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); error = -libxfs_trans_commit(tp); if (error) @@ -811,7 +767,6 @@ mk_root_dir(xfs_mount_t *mp) int error; const mode_t mode = 0755; ino_tree_node_t *irec; - int times; ip = NULL; i = -libxfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 10, 0, 0, &tp); @@ -823,37 +778,8 @@ mk_root_dir(xfs_mount_t *mp) do_error(_("could not iget root inode -- error - %d\n"), error); } - /* - * take care of the core since we didn't call the libxfs ialloc function - * (comment changed to avoid tangling xfs/437) - */ - reset_inode_fields(ip); - - VFS_I(ip)->i_mode = mode|S_IFDIR; - ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS; - libxfs_ifork_zap_attr(ip); - - set_nlink(VFS_I(ip), 2); /* account for . and .. */ - - times = XFS_ICHGTIME_CHG | XFS_ICHGTIME_MOD; - if (xfs_has_v3inodes(mp)) { - VFS_I(ip)->i_version = 1; - ip->i_diflags2 = 0; - times |= XFS_ICHGTIME_CREATE; - } - libxfs_trans_ichgtime(tp, ip, times); - libxfs_trans_ijoin(tp, ip, 0); - libxfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); - - /* - * now the ifork - */ - ip->i_df.if_bytes = 0; - ip->i_df.if_u1.if_root = NULL; - - /* - * initialize the directory - */ + /* Reset the root directory. */ + reset_root_ino(tp, mode | S_IFDIR, ip); libxfs_dir_init(tp, ip, ip); error = -libxfs_trans_commit(tp);