xfs_inode.c (01ea173e103edd5ec41acec65b9261b87e123fc2) xfs_inode.c (f736d93d76d3e97d6986c6d26c8eaa32536ccc5c)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6#include <linux/iversion.h>
7
8#include "xfs.h"

--- 752 unchanged lines hidden (view full) ---

761}
762
763/*
764 * Initialise a newly allocated inode and return the in-core inode to the
765 * caller locked exclusively.
766 */
767static int
768xfs_init_new_inode(
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6#include <linux/iversion.h>
7
8#include "xfs.h"

--- 752 unchanged lines hidden (view full) ---

761}
762
763/*
764 * Initialise a newly allocated inode and return the in-core inode to the
765 * caller locked exclusively.
766 */
767static int
768xfs_init_new_inode(
769 struct user_namespace *mnt_userns,
769 struct xfs_trans *tp,
770 struct xfs_inode *pip,
771 xfs_ino_t ino,
772 umode_t mode,
773 xfs_nlink_t nlink,
774 dev_t rdev,
775 prid_t prid,
776 struct xfs_inode **ipp)
777{
770 struct xfs_trans *tp,
771 struct xfs_inode *pip,
772 xfs_ino_t ino,
773 umode_t mode,
774 xfs_nlink_t nlink,
775 dev_t rdev,
776 prid_t prid,
777 struct xfs_inode **ipp)
778{
778 struct inode *dir = pip ? VFS_I(pip) : NULL;
779 struct xfs_mount *mp = tp->t_mountp;
780 struct xfs_inode *ip;
781 unsigned int flags;
782 int error;
783 struct timespec64 tv;
784 struct inode *inode;
785
786 /*

--- 13 unchanged lines hidden (view full) ---

800 * others from looking at until we're done.
801 */
802 error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE, XFS_ILOCK_EXCL, &ip);
803 if (error)
804 return error;
805
806 ASSERT(ip != NULL);
807 inode = VFS_I(ip);
779 struct xfs_mount *mp = tp->t_mountp;
780 struct xfs_inode *ip;
781 unsigned int flags;
782 int error;
783 struct timespec64 tv;
784 struct inode *inode;
785
786 /*

--- 13 unchanged lines hidden (view full) ---

800 * others from looking at until we're done.
801 */
802 error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE, XFS_ILOCK_EXCL, &ip);
803 if (error)
804 return error;
805
806 ASSERT(ip != NULL);
807 inode = VFS_I(ip);
808 inode->i_mode = mode;
808 set_nlink(inode, nlink);
809 set_nlink(inode, nlink);
810 inode->i_uid = fsuid_into_mnt(mnt_userns);
809 inode->i_rdev = rdev;
810 ip->i_d.di_projid = prid;
811
811 inode->i_rdev = rdev;
812 ip->i_d.di_projid = prid;
813
812 if (dir && !(dir->i_mode & S_ISGID) &&
813 (mp->m_flags & XFS_MOUNT_GRPID)) {
814 inode->i_uid = current_fsuid();
815 inode->i_gid = dir->i_gid;
816 inode->i_mode = mode;
814 if (pip && XFS_INHERIT_GID(pip)) {
815 inode->i_gid = VFS_I(pip)->i_gid;
816 if ((VFS_I(pip)->i_mode & S_ISGID) && S_ISDIR(mode))
817 inode->i_mode |= S_ISGID;
817 } else {
818 } else {
818 inode_init_owner(inode, dir, mode);
819 inode->i_gid = fsgid_into_mnt(mnt_userns);
819 }
820
821 /*
822 * If the group ID of the new file does not match the effective group
823 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
824 * (and only if the irix_sgid_inherit compatibility variable is set).
825 */
826 if (irix_sgid_inherit &&
820 }
821
822 /*
823 * If the group ID of the new file does not match the effective group
824 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
825 * (and only if the irix_sgid_inherit compatibility variable is set).
826 */
827 if (irix_sgid_inherit &&
827 (inode->i_mode & S_ISGID) && !in_group_p(inode->i_gid))
828 (inode->i_mode & S_ISGID) &&
829 !in_group_p(i_gid_into_mnt(mnt_userns, inode)))
828 inode->i_mode &= ~S_ISGID;
829
830 ip->i_d.di_size = 0;
831 ip->i_df.if_nextents = 0;
832 ASSERT(ip->i_d.di_nblocks == 0);
833
834 tv = current_time(inode);
835 inode->i_mtime = tv;

--- 60 unchanged lines hidden (view full) ---

896 *
897 * If we are allocating quota inodes, we do not have a parent inode to attach to
898 * or associate with (i.e. dp == NULL) because they are not linked into the
899 * directory structure - they are attached directly to the superblock - and so
900 * have no parent.
901 */
902int
903xfs_dir_ialloc(
830 inode->i_mode &= ~S_ISGID;
831
832 ip->i_d.di_size = 0;
833 ip->i_df.if_nextents = 0;
834 ASSERT(ip->i_d.di_nblocks == 0);
835
836 tv = current_time(inode);
837 inode->i_mtime = tv;

--- 60 unchanged lines hidden (view full) ---

898 *
899 * If we are allocating quota inodes, we do not have a parent inode to attach to
900 * or associate with (i.e. dp == NULL) because they are not linked into the
901 * directory structure - they are attached directly to the superblock - and so
902 * have no parent.
903 */
904int
905xfs_dir_ialloc(
906 struct user_namespace *mnt_userns,
904 struct xfs_trans **tpp,
905 struct xfs_inode *dp,
906 umode_t mode,
907 xfs_nlink_t nlink,
908 dev_t rdev,
909 prid_t prid,
910 struct xfs_inode **ipp)
911{

--- 16 unchanged lines hidden (view full) ---

928 return -ENOSPC;
929
930 /* Allocate an inode from the selected AG */
931 error = xfs_dialloc_ag(*tpp, agibp, parent_ino, &ino);
932 if (error)
933 return error;
934 ASSERT(ino != NULLFSINO);
935
907 struct xfs_trans **tpp,
908 struct xfs_inode *dp,
909 umode_t mode,
910 xfs_nlink_t nlink,
911 dev_t rdev,
912 prid_t prid,
913 struct xfs_inode **ipp)
914{

--- 16 unchanged lines hidden (view full) ---

931 return -ENOSPC;
932
933 /* Allocate an inode from the selected AG */
934 error = xfs_dialloc_ag(*tpp, agibp, parent_ino, &ino);
935 if (error)
936 return error;
937 ASSERT(ino != NULLFSINO);
938
936 return xfs_init_new_inode(*tpp, dp, ino, mode, nlink, rdev, prid, ipp);
939 return xfs_init_new_inode(mnt_userns, *tpp, dp, ino, mode, nlink, rdev,
940 prid, ipp);
937}
938
939/*
940 * Decrement the link count on an inode & log the change. If this causes the
941 * link count to go to zero, move the inode to AGI unlinked list so that it can
942 * be freed when the last active reference goes away via xfs_inactive().
943 */
944static int /* error */

--- 23 unchanged lines hidden (view full) ---

968 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
969
970 inc_nlink(VFS_I(ip));
971 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
972}
973
974int
975xfs_create(
941}
942
943/*
944 * Decrement the link count on an inode & log the change. If this causes the
945 * link count to go to zero, move the inode to AGI unlinked list so that it can
946 * be freed when the last active reference goes away via xfs_inactive().
947 */
948static int /* error */

--- 23 unchanged lines hidden (view full) ---

972 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
973
974 inc_nlink(VFS_I(ip));
975 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
976}
977
978int
979xfs_create(
980 struct user_namespace *mnt_userns,
976 xfs_inode_t *dp,
977 struct xfs_name *name,
978 umode_t mode,
979 dev_t rdev,
980 xfs_inode_t **ipp)
981{
982 int is_dir = S_ISDIR(mode);
983 struct xfs_mount *mp = dp->i_mount;

--- 53 unchanged lines hidden (view full) ---

1037 /*
1038 * Reserve disk quota and the inode.
1039 */
1040 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1041 pdqp, resblks, 1, 0);
1042 if (error)
1043 goto out_trans_cancel;
1044
981 xfs_inode_t *dp,
982 struct xfs_name *name,
983 umode_t mode,
984 dev_t rdev,
985 xfs_inode_t **ipp)
986{
987 int is_dir = S_ISDIR(mode);
988 struct xfs_mount *mp = dp->i_mount;

--- 53 unchanged lines hidden (view full) ---

1042 /*
1043 * Reserve disk quota and the inode.
1044 */
1045 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1046 pdqp, resblks, 1, 0);
1047 if (error)
1048 goto out_trans_cancel;
1049
1045 error = xfs_iext_count_may_overflow(dp, XFS_DATA_FORK,
1046 XFS_IEXT_DIR_MANIP_CNT(mp));
1047 if (error)
1048 goto out_trans_cancel;
1049
1050 /*
1051 * A newly created regular or special file just has one directory
1052 * entry pointing to them, but a directory also the "." entry
1053 * pointing to itself.
1054 */
1050 /*
1051 * A newly created regular or special file just has one directory
1052 * entry pointing to them, but a directory also the "." entry
1053 * pointing to itself.
1054 */
1055 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, prid, &ip);
1055 error = xfs_dir_ialloc(mnt_userns, &tp, dp, mode, is_dir ? 2 : 1, rdev,
1056 prid, &ip);
1056 if (error)
1057 goto out_trans_cancel;
1058
1059 /*
1060 * Now we join the directory inode to the transaction. We do not do it
1061 * earlier because xfs_dir_ialloc might commit the previous transaction
1062 * (and release all the locks). An error from here on will result in
1063 * the transaction cancel unlocking dp so don't do it explicitly in the

--- 64 unchanged lines hidden (view full) ---

1128
1129 if (unlock_dp_on_error)
1130 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1131 return error;
1132}
1133
1134int
1135xfs_create_tmpfile(
1057 if (error)
1058 goto out_trans_cancel;
1059
1060 /*
1061 * Now we join the directory inode to the transaction. We do not do it
1062 * earlier because xfs_dir_ialloc might commit the previous transaction
1063 * (and release all the locks). An error from here on will result in
1064 * the transaction cancel unlocking dp so don't do it explicitly in the

--- 64 unchanged lines hidden (view full) ---

1129
1130 if (unlock_dp_on_error)
1131 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1132 return error;
1133}
1134
1135int
1136xfs_create_tmpfile(
1137 struct user_namespace *mnt_userns,
1136 struct xfs_inode *dp,
1137 umode_t mode,
1138 struct xfs_inode **ipp)
1139{
1140 struct xfs_mount *mp = dp->i_mount;
1141 struct xfs_inode *ip = NULL;
1142 struct xfs_trans *tp = NULL;
1143 int error;

--- 25 unchanged lines hidden (view full) ---

1169 if (error)
1170 goto out_release_inode;
1171
1172 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1173 pdqp, resblks, 1, 0);
1174 if (error)
1175 goto out_trans_cancel;
1176
1138 struct xfs_inode *dp,
1139 umode_t mode,
1140 struct xfs_inode **ipp)
1141{
1142 struct xfs_mount *mp = dp->i_mount;
1143 struct xfs_inode *ip = NULL;
1144 struct xfs_trans *tp = NULL;
1145 int error;

--- 25 unchanged lines hidden (view full) ---

1171 if (error)
1172 goto out_release_inode;
1173
1174 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1175 pdqp, resblks, 1, 0);
1176 if (error)
1177 goto out_trans_cancel;
1178
1177 error = xfs_dir_ialloc(&tp, dp, mode, 0, 0, prid, &ip);
1179 error = xfs_dir_ialloc(mnt_userns, &tp, dp, mode, 0, 0, prid, &ip);
1178 if (error)
1179 goto out_trans_cancel;
1180
1181 if (mp->m_flags & XFS_MOUNT_WSYNC)
1182 xfs_trans_set_sync(tp);
1183
1184 /*
1185 * Attach the dquot(s) to the inodes and modify them incore.

--- 72 unchanged lines hidden (view full) ---

1258 if (error)
1259 goto std_return;
1260
1261 xfs_lock_two_inodes(sip, XFS_ILOCK_EXCL, tdp, XFS_ILOCK_EXCL);
1262
1263 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1264 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
1265
1180 if (error)
1181 goto out_trans_cancel;
1182
1183 if (mp->m_flags & XFS_MOUNT_WSYNC)
1184 xfs_trans_set_sync(tp);
1185
1186 /*
1187 * Attach the dquot(s) to the inodes and modify them incore.

--- 72 unchanged lines hidden (view full) ---

1260 if (error)
1261 goto std_return;
1262
1263 xfs_lock_two_inodes(sip, XFS_ILOCK_EXCL, tdp, XFS_ILOCK_EXCL);
1264
1265 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1266 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
1267
1266 error = xfs_iext_count_may_overflow(tdp, XFS_DATA_FORK,
1267 XFS_IEXT_DIR_MANIP_CNT(mp));
1268 if (error)
1269 goto error_return;
1270
1271 /*
1272 * If we are using project inheritance, we only allow hard link
1273 * creation in our tree when the project IDs are the same; else
1274 * the tree quota mechanism could be circumvented.
1275 */
1276 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1277 tdp->i_d.di_projid != sip->i_d.di_projid)) {
1278 error = -EXDEV;

--- 1703 unchanged lines hidden (view full) ---

2982 *
2983 * Return a referenced, unlinked, unlocked inode that can be used as a
2984 * whiteout in a rename transaction. We use a tmpfile inode here so that if we
2985 * crash between allocating the inode and linking it into the rename transaction
2986 * recovery will free the inode and we won't leak it.
2987 */
2988static int
2989xfs_rename_alloc_whiteout(
1268 /*
1269 * If we are using project inheritance, we only allow hard link
1270 * creation in our tree when the project IDs are the same; else
1271 * the tree quota mechanism could be circumvented.
1272 */
1273 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1274 tdp->i_d.di_projid != sip->i_d.di_projid)) {
1275 error = -EXDEV;

--- 1703 unchanged lines hidden (view full) ---

2979 *
2980 * Return a referenced, unlinked, unlocked inode that can be used as a
2981 * whiteout in a rename transaction. We use a tmpfile inode here so that if we
2982 * crash between allocating the inode and linking it into the rename transaction
2983 * recovery will free the inode and we won't leak it.
2984 */
2985static int
2986xfs_rename_alloc_whiteout(
2987 struct user_namespace *mnt_userns,
2990 struct xfs_inode *dp,
2991 struct xfs_inode **wip)
2992{
2993 struct xfs_inode *tmpfile;
2994 int error;
2995
2988 struct xfs_inode *dp,
2989 struct xfs_inode **wip)
2990{
2991 struct xfs_inode *tmpfile;
2992 int error;
2993
2996 error = xfs_create_tmpfile(dp, S_IFCHR | WHITEOUT_MODE, &tmpfile);
2994 error = xfs_create_tmpfile(mnt_userns, dp, S_IFCHR | WHITEOUT_MODE,
2995 &tmpfile);
2997 if (error)
2998 return error;
2999
3000 /*
3001 * Prepare the tmpfile inode as if it were created through the VFS.
3002 * Complete the inode setup and flag it as linkable. nlink is already
3003 * zero, so we can skip the drop_nlink.
3004 */

--- 5 unchanged lines hidden (view full) ---

3010 return 0;
3011}
3012
3013/*
3014 * xfs_rename
3015 */
3016int
3017xfs_rename(
2996 if (error)
2997 return error;
2998
2999 /*
3000 * Prepare the tmpfile inode as if it were created through the VFS.
3001 * Complete the inode setup and flag it as linkable. nlink is already
3002 * zero, so we can skip the drop_nlink.
3003 */

--- 5 unchanged lines hidden (view full) ---

3009 return 0;
3010}
3011
3012/*
3013 * xfs_rename
3014 */
3015int
3016xfs_rename(
3017 struct user_namespace *mnt_userns,
3018 struct xfs_inode *src_dp,
3019 struct xfs_name *src_name,
3020 struct xfs_inode *src_ip,
3021 struct xfs_inode *target_dp,
3022 struct xfs_name *target_name,
3023 struct xfs_inode *target_ip,
3024 unsigned int flags)
3025{
3026 struct xfs_mount *mp = src_dp->i_mount;
3027 struct xfs_trans *tp;
3028 struct xfs_inode *wip = NULL; /* whiteout inode */
3029 struct xfs_inode *inodes[__XFS_SORT_INODES];
3018 struct xfs_inode *src_dp,
3019 struct xfs_name *src_name,
3020 struct xfs_inode *src_ip,
3021 struct xfs_inode *target_dp,
3022 struct xfs_name *target_name,
3023 struct xfs_inode *target_ip,
3024 unsigned int flags)
3025{
3026 struct xfs_mount *mp = src_dp->i_mount;
3027 struct xfs_trans *tp;
3028 struct xfs_inode *wip = NULL; /* whiteout inode */
3029 struct xfs_inode *inodes[__XFS_SORT_INODES];
3030 int i;
3030 struct xfs_buf *agibp;
3031 int num_inodes = __XFS_SORT_INODES;
3032 bool new_parent = (src_dp != target_dp);
3033 bool src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
3034 int spaceres;
3035 int error;
3036
3037 trace_xfs_rename(src_dp, target_dp, src_name, target_name);
3038
3039 if ((flags & RENAME_EXCHANGE) && !target_ip)
3040 return -EINVAL;
3041
3042 /*
3043 * If we are doing a whiteout operation, allocate the whiteout inode
3044 * we will be placing at the target and ensure the type is set
3045 * appropriately.
3046 */
3047 if (flags & RENAME_WHITEOUT) {
3048 ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)));
3031 int num_inodes = __XFS_SORT_INODES;
3032 bool new_parent = (src_dp != target_dp);
3033 bool src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
3034 int spaceres;
3035 int error;
3036
3037 trace_xfs_rename(src_dp, target_dp, src_name, target_name);
3038
3039 if ((flags & RENAME_EXCHANGE) && !target_ip)
3040 return -EINVAL;
3041
3042 /*
3043 * If we are doing a whiteout operation, allocate the whiteout inode
3044 * we will be placing at the target and ensure the type is set
3045 * appropriately.
3046 */
3047 if (flags & RENAME_WHITEOUT) {
3048 ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)));
3049 error = xfs_rename_alloc_whiteout(target_dp, &wip);
3049 error = xfs_rename_alloc_whiteout(mnt_userns, target_dp, &wip);
3050 if (error)
3051 return error;
3052
3053 /* setup target dirent info as whiteout */
3054 src_name->type = XFS_DIR3_FT_CHRDEV;
3055 }
3056
3057 xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,

--- 53 unchanged lines hidden (view full) ---

3111 if (flags & RENAME_EXCHANGE)
3112 return xfs_cross_rename(tp, src_dp, src_name, src_ip,
3113 target_dp, target_name, target_ip,
3114 spaceres);
3115
3116 /*
3117 * Check for expected errors before we dirty the transaction
3118 * so we can return an error without a transaction abort.
3050 if (error)
3051 return error;
3052
3053 /* setup target dirent info as whiteout */
3054 src_name->type = XFS_DIR3_FT_CHRDEV;
3055 }
3056
3057 xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,

--- 53 unchanged lines hidden (view full) ---

3111 if (flags & RENAME_EXCHANGE)
3112 return xfs_cross_rename(tp, src_dp, src_name, src_ip,
3113 target_dp, target_name, target_ip,
3114 spaceres);
3115
3116 /*
3117 * Check for expected errors before we dirty the transaction
3118 * so we can return an error without a transaction abort.
3119 *
3120 * Extent count overflow check:
3121 *
3122 * From the perspective of src_dp, a rename operation is essentially a
3123 * directory entry remove operation. Hence the only place where we check
3124 * for extent count overflow for src_dp is in
3125 * xfs_bmap_del_extent_real(). xfs_bmap_del_extent_real() returns
3126 * -ENOSPC when it detects a possible extent count overflow and in
3127 * response, the higher layers of directory handling code do the
3128 * following:
3129 * 1. Data/Free blocks: XFS lets these blocks linger until a
3130 * future remove operation removes them.
3131 * 2. Dabtree blocks: XFS swaps the blocks with the last block in the
3132 * Leaf space and unmaps the last block.
3133 *
3134 * For target_dp, there are two cases depending on whether the
3135 * destination directory entry exists or not.
3136 *
3137 * When destination directory entry does not exist (i.e. target_ip ==
3138 * NULL), extent count overflow check is performed only when transaction
3139 * has a non-zero sized space reservation associated with it. With a
3140 * zero-sized space reservation, XFS allows a rename operation to
3141 * continue only when the directory has sufficient free space in its
3142 * data/leaf/free space blocks to hold the new entry.
3143 *
3144 * When destination directory entry exists (i.e. target_ip != NULL), all
3145 * we need to do is change the inode number associated with the already
3146 * existing entry. Hence there is no need to perform an extent count
3147 * overflow check.
3148 */
3149 if (target_ip == NULL) {
3150 /*
3151 * If there's no space reservation, check the entry will
3152 * fit before actually inserting it.
3153 */
3154 if (!spaceres) {
3155 error = xfs_dir_canenter(tp, target_dp, target_name);
3156 if (error)
3157 goto out_trans_cancel;
3119 */
3120 if (target_ip == NULL) {
3121 /*
3122 * If there's no space reservation, check the entry will
3123 * fit before actually inserting it.
3124 */
3125 if (!spaceres) {
3126 error = xfs_dir_canenter(tp, target_dp, target_name);
3127 if (error)
3128 goto out_trans_cancel;
3158 } else {
3159 error = xfs_iext_count_may_overflow(target_dp,
3160 XFS_DATA_FORK,
3161 XFS_IEXT_DIR_MANIP_CNT(mp));
3162 if (error)
3163 goto out_trans_cancel;
3164 }
3165 } else {
3166 /*
3167 * If target exists and it's a directory, check that whether
3168 * it can be destroyed.
3169 */
3170 if (S_ISDIR(VFS_I(target_ip)->i_mode) &&
3171 (!xfs_dir_isempty(target_ip) ||
3172 (VFS_I(target_ip)->i_nlink > 2))) {
3173 error = -EEXIST;
3174 goto out_trans_cancel;
3175 }
3176 }
3177
3178 /*
3129 }
3130 } else {
3131 /*
3132 * If target exists and it's a directory, check that whether
3133 * it can be destroyed.
3134 */
3135 if (S_ISDIR(VFS_I(target_ip)->i_mode) &&
3136 (!xfs_dir_isempty(target_ip) ||
3137 (VFS_I(target_ip)->i_nlink > 2))) {
3138 error = -EEXIST;
3139 goto out_trans_cancel;
3140 }
3141 }
3142
3143 /*
3179 * Lock the AGI buffers we need to handle bumping the nlink of the
3180 * whiteout inode off the unlinked list and to handle dropping the
3181 * nlink of the target inode. Per locking order rules, do this in
3182 * increasing AG order and before directory block allocation tries to
3183 * grab AGFs because we grab AGIs before AGFs.
3184 *
3185 * The (vfs) caller must ensure that if src is a directory then
3186 * target_ip is either null or an empty directory.
3187 */
3188 for (i = 0; i < num_inodes && inodes[i] != NULL; i++) {
3189 if (inodes[i] == wip ||
3190 (inodes[i] == target_ip &&
3191 (VFS_I(target_ip)->i_nlink == 1 || src_is_directory))) {
3192 struct xfs_buf *bp;
3193 xfs_agnumber_t agno;
3194
3195 agno = XFS_INO_TO_AGNO(mp, inodes[i]->i_ino);
3196 error = xfs_read_agi(mp, tp, agno, &bp);
3197 if (error)
3198 goto out_trans_cancel;
3199 }
3200 }
3201
3202 /*
3203 * Directory entry creation below may acquire the AGF. Remove
3204 * the whiteout from the unlinked list first to preserve correct
3205 * AGI/AGF locking order. This dirties the transaction so failures
3206 * after this point will abort and log recovery will clean up the
3207 * mess.
3208 *
3209 * For whiteouts, we need to bump the link count on the whiteout
3210 * inode. After this point, we have a real link, clear the tmpfile

--- 35 unchanged lines hidden (view full) ---

3246 * Link the source inode under the target name.
3247 * If the source inode is a directory and we are moving
3248 * it across directories, its ".." entry will be
3249 * inconsistent until we replace that down below.
3250 *
3251 * In case there is already an entry with the same
3252 * name at the destination directory, remove it first.
3253 */
3144 * Directory entry creation below may acquire the AGF. Remove
3145 * the whiteout from the unlinked list first to preserve correct
3146 * AGI/AGF locking order. This dirties the transaction so failures
3147 * after this point will abort and log recovery will clean up the
3148 * mess.
3149 *
3150 * For whiteouts, we need to bump the link count on the whiteout
3151 * inode. After this point, we have a real link, clear the tmpfile

--- 35 unchanged lines hidden (view full) ---

3187 * Link the source inode under the target name.
3188 * If the source inode is a directory and we are moving
3189 * it across directories, its ".." entry will be
3190 * inconsistent until we replace that down below.
3191 *
3192 * In case there is already an entry with the same
3193 * name at the destination directory, remove it first.
3194 */
3195
3196 /*
3197 * Check whether the replace operation will need to allocate
3198 * blocks. This happens when the shortform directory lacks
3199 * space and we have to convert it to a block format directory.
3200 * When more blocks are necessary, we must lock the AGI first
3201 * to preserve locking order (AGI -> AGF).
3202 */
3203 if (xfs_dir2_sf_replace_needblock(target_dp, src_ip->i_ino)) {
3204 error = xfs_read_agi(mp, tp,
3205 XFS_INO_TO_AGNO(mp, target_ip->i_ino),
3206 &agibp);
3207 if (error)
3208 goto out_trans_cancel;
3209 }
3210
3254 error = xfs_dir_replace(tp, target_dp, target_name,
3255 src_ip->i_ino, spaceres);
3256 if (error)
3257 goto out_trans_cancel;
3258
3259 xfs_trans_ichgtime(tp, target_dp,
3260 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3261

--- 59 unchanged lines hidden (view full) ---

3321 /*
3322 * For whiteouts, we only need to update the source dirent with the
3323 * inode number of the whiteout inode rather than removing it
3324 * altogether.
3325 */
3326 if (wip) {
3327 error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
3328 spaceres);
3211 error = xfs_dir_replace(tp, target_dp, target_name,
3212 src_ip->i_ino, spaceres);
3213 if (error)
3214 goto out_trans_cancel;
3215
3216 xfs_trans_ichgtime(tp, target_dp,
3217 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3218

--- 59 unchanged lines hidden (view full) ---

3278 /*
3279 * For whiteouts, we only need to update the source dirent with the
3280 * inode number of the whiteout inode rather than removing it
3281 * altogether.
3282 */
3283 if (wip) {
3284 error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
3285 spaceres);
3329 } else {
3330 /*
3331 * NOTE: We don't need to check for extent count overflow here
3332 * because the dir remove name code will leave the dir block in
3333 * place if the extent count would overflow.
3334 */
3286 } else
3335 error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
3336 spaceres);
3287 error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
3288 spaceres);
3337 }
3338
3339 if (error)
3340 goto out_trans_cancel;
3341
3342 xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3343 xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
3344 if (new_parent)
3345 xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
3346

--- 394 unchanged lines hidden ---
3289 if (error)
3290 goto out_trans_cancel;
3291
3292 xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3293 xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
3294 if (new_parent)
3295 xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
3296

--- 394 unchanged lines hidden ---