From: Allison Henderson <allison.henderson@xxxxxxxxxx> This patch removes the old parent pointer attribute during the rename operation, and re-adds the updated parent pointer. Signed-off-by: Allison Henderson <allison.henderson@xxxxxxxxxx> Reviewed-by: Darrick J. Wong <djwong@xxxxxxxxxx> [djwong: adjust to new ondisk format] Signed-off-by: Darrick J. Wong <djwong@xxxxxxxxxx> --- libxfs/xfs_parent.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++ libxfs/xfs_parent.h | 20 +++++++++++++++ libxfs/xfs_trans_space.c | 25 ++++++++++++++++++ libxfs/xfs_trans_space.h | 6 +++- 4 files changed, 112 insertions(+), 2 deletions(-) diff --git a/libxfs/xfs_parent.c b/libxfs/xfs_parent.c index b3004af9161..6c98f95f274 100644 --- a/libxfs/xfs_parent.c +++ b/libxfs/xfs_parent.c @@ -139,6 +139,19 @@ xfs_init_parent_davalue( args->value = (void *)name->name; } +/* + * Point the da args new value fields at the non-key parts of a replacement + * parent pointer. + */ +static inline void +xfs_init_parent_danewvalue( + struct xfs_da_args *args, + const struct xfs_name *name) +{ + args->new_valuelen = name->len; + args->new_value = (void *)name->name; +} + /* * Allocate memory to control a logged parent pointer update as part of a * dirent operation. @@ -234,6 +247,56 @@ xfs_parent_removename( return 0; } +/* Replace one parent pointer with another to reflect a rename. */ +int +xfs_parent_replacename( + struct xfs_trans *tp, + struct xfs_parent_args *ppargs, + struct xfs_inode *old_dp, + const struct xfs_name *old_name, + struct xfs_inode *new_dp, + const struct xfs_name *new_name, + struct xfs_inode *child) +{ + struct xfs_da_args *args = &ppargs->args; + + if (XFS_IS_CORRUPT(tp->t_mountp, + !xfs_parent_valuecheck(tp->t_mountp, old_name->name, + old_name->len))) + return -EFSCORRUPTED; + + if (XFS_IS_CORRUPT(tp->t_mountp, + !xfs_parent_valuecheck(tp->t_mountp, new_name->name, + new_name->len))) + return -EFSCORRUPTED; + + /* + * For regular attrs, replacing an attr from a !hasattr inode becomes + * an attr-set operation. For replacing a parent pointer, however, we + * require that the old pointer must exist. + */ + if (XFS_IS_CORRUPT(child->i_mount, !xfs_inode_hasattr(child))) { + xfs_inode_mark_sick(child, XFS_SICK_INO_PARENT); + return -EFSCORRUPTED; + } + + xfs_init_parent_name_rec(&ppargs->rec, old_dp, old_name, child); + args->hashval = xfs_parent_hashname(old_dp, ppargs); + + xfs_init_parent_name_rec(&ppargs->new_rec, new_dp, new_name, child); + args->new_name = (const uint8_t *)&ppargs->new_rec; + args->new_namelen = sizeof(struct xfs_parent_name_rec); + + args->trans = tp; + args->dp = child; + + xfs_init_parent_davalue(&ppargs->args, old_name); + xfs_init_parent_danewvalue(&ppargs->args, new_name); + + xfs_attr_defer_add(args, XFS_ATTRI_OP_FLAGS_REPLACE); + return 0; +} + /* Free a parent pointer context object. */ void xfs_parent_args_free( diff --git a/libxfs/xfs_parent.h b/libxfs/xfs_parent.h index 31349130a33..c68c501388e 100644 --- a/libxfs/xfs_parent.h +++ b/libxfs/xfs_parent.h @@ -24,6 +24,7 @@ extern struct kmem_cache *xfs_parent_args_cache; */ struct xfs_parent_args { struct xfs_parent_name_rec rec; + struct xfs_parent_name_rec new_rec; struct xfs_da_args args; }; @@ -95,6 +96,25 @@ xfs_parent_remove(struct xfs_trans *tp, struct xfs_parent_args *ppargs, return 0; } +int xfs_parent_replacename(struct xfs_trans *tp, + struct xfs_parent_args *ppargs, + struct xfs_inode *old_dp, const struct xfs_name *old_name, + struct xfs_inode *new_dp, const struct xfs_name *new_name, + struct xfs_inode *child); + +/* Schedule a parent pointer replacement. */ +static inline int +xfs_parent_replace(struct xfs_trans *tp, struct xfs_parent_args *ppargs, + struct xfs_inode *old_dp, const struct xfs_name *old_name, + struct xfs_inode *new_dp, const struct xfs_name *new_name, + struct xfs_inode *child) +{ + if (ppargs) + return xfs_parent_replacename(tp, ppargs, old_dp, old_name, + new_dp, new_name, child); + return 0; +} + void xfs_parent_args_free(struct xfs_mount *mp, struct xfs_parent_args *ppargs); /* Finish a parent pointer update by freeing the context object. */ diff --git a/libxfs/xfs_trans_space.c b/libxfs/xfs_trans_space.c index 86a91a3a844..373f5cc2497 100644 --- a/libxfs/xfs_trans_space.c +++ b/libxfs/xfs_trans_space.c @@ -94,3 +94,28 @@ xfs_remove_space_res( return ret; } + +unsigned int +xfs_rename_space_res( + struct xfs_mount *mp, + unsigned int src_namelen, + bool target_exists, + unsigned int target_namelen, + bool has_whiteout) +{ + unsigned int ret; + + ret = XFS_DIRREMOVE_SPACE_RES(mp) + + XFS_DIRENTER_SPACE_RES(mp, target_namelen); + + if (xfs_has_parent(mp)) { + if (has_whiteout) + ret += xfs_parent_calc_space_res(mp, src_namelen); + ret += 2 * xfs_parent_calc_space_res(mp, target_namelen); + } + + if (target_exists) + ret += xfs_parent_calc_space_res(mp, target_namelen); + + return ret; +} diff --git a/libxfs/xfs_trans_space.h b/libxfs/xfs_trans_space.h index a4490813c56..1155ff2d37e 100644 --- a/libxfs/xfs_trans_space.h +++ b/libxfs/xfs_trans_space.h @@ -91,8 +91,6 @@ XFS_DQUOT_CLUSTER_SIZE_FSB) #define XFS_QM_QINOCREATE_SPACE_RES(mp) \ XFS_IALLOC_SPACE_RES(mp) -#define XFS_RENAME_SPACE_RES(mp,nl) \ - (XFS_DIRREMOVE_SPACE_RES(mp) + XFS_DIRENTER_SPACE_RES(mp,nl)) #define XFS_IFREE_SPACE_RES(mp) \ (xfs_has_finobt(mp) ? M_IGEO(mp)->inobt_maxlevels : 0) @@ -106,4 +104,8 @@ unsigned int xfs_symlink_space_res(struct xfs_mount *mp, unsigned int namelen, unsigned int fsblocks); unsigned int xfs_remove_space_res(struct xfs_mount *mp, unsigned int namelen); +unsigned int xfs_rename_space_res(struct xfs_mount *mp, + unsigned int src_namelen, bool target_exists, + unsigned int target_namelen, bool has_whiteout); + #endif /* __XFS_TRANS_SPACE_H__ */