diff options
Diffstat (limited to 'fs/namei.c')
| -rw-r--r-- | fs/namei.c | 348 | 
1 files changed, 186 insertions, 162 deletions
diff --git a/fs/namei.c b/fs/namei.c index 385f7817bfcc..88339f59efb5 100644 --- a/fs/namei.c +++ b/fs/namei.c  | |||
| @@ -1109,7 +1109,7 @@ static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, | |||
| 1109 | return false; | 1109 | return false; | 
| 1110 | 1110 | ||
| 1111 | if (!d_mountpoint(path->dentry)) | 1111 | if (!d_mountpoint(path->dentry)) | 
| 1112 | break; | 1112 | return true; | 
| 1113 | 1113 | ||
| 1114 | mounted = __lookup_mnt(path->mnt, path->dentry); | 1114 | mounted = __lookup_mnt(path->mnt, path->dentry); | 
| 1115 | if (!mounted) | 1115 | if (!mounted) | 
| @@ -1125,20 +1125,7 @@ static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, | |||
| 1125 | */ | 1125 | */ | 
| 1126 | *inode = path->dentry->d_inode; | 1126 | *inode = path->dentry->d_inode; | 
| 1127 | } | 1127 | } | 
| 1128 | return true; | 1128 | return read_seqretry(&mount_lock, nd->m_seq); | 
| 1129 | } | ||
| 1130 | |||
| 1131 | static void follow_mount_rcu(struct nameidata *nd) | ||
| 1132 | { | ||
| 1133 | while (d_mountpoint(nd->path.dentry)) { | ||
| 1134 | struct mount *mounted; | ||
| 1135 | mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry); | ||
| 1136 | if (!mounted) | ||
| 1137 | break; | ||
| 1138 | nd->path.mnt = &mounted->mnt; | ||
| 1139 | nd->path.dentry = mounted->mnt.mnt_root; | ||
| 1140 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); | ||
| 1141 | } | ||
| 1142 | } | 1129 | } | 
| 1143 | 1130 | ||
| 1144 | static int follow_dotdot_rcu(struct nameidata *nd) | 1131 | static int follow_dotdot_rcu(struct nameidata *nd) | 
| @@ -1166,7 +1153,17 @@ static int follow_dotdot_rcu(struct nameidata *nd) | |||
| 1166 | break; | 1153 | break; | 
| 1167 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); | 1154 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); | 
| 1168 | } | 1155 | } | 
| 1169 | follow_mount_rcu(nd); | 1156 | while (d_mountpoint(nd->path.dentry)) { | 
| 1157 | struct mount *mounted; | ||
| 1158 | mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry); | ||
| 1159 | if (!mounted) | ||
| 1160 | break; | ||
| 1161 | nd->path.mnt = &mounted->mnt; | ||
| 1162 | nd->path.dentry = mounted->mnt.mnt_root; | ||
| 1163 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); | ||
| 1164 | if (!read_seqretry(&mount_lock, nd->m_seq)) | ||
| 1165 | goto failed; | ||
| 1166 | } | ||
| 1170 | nd->inode = nd->path.dentry->d_inode; | 1167 | nd->inode = nd->path.dentry->d_inode; | 
| 1171 | return 0; | 1168 | return 0; | 
| 1172 | 1169 | ||
| @@ -1799,7 +1796,7 @@ static int link_path_walk(const char *name, struct nameidata *nd) | |||
| 1799 | if (err) | 1796 | if (err) | 
| 1800 | return err; | 1797 | return err; | 
| 1801 | } | 1798 | } | 
| 1802 | if (!d_is_directory(nd->path.dentry)) { | 1799 | if (!d_can_lookup(nd->path.dentry)) { | 
| 1803 | err = -ENOTDIR; | 1800 | err = -ENOTDIR; | 
| 1804 | break; | 1801 | break; | 
| 1805 | } | 1802 | } | 
| @@ -1820,7 +1817,7 @@ static int path_init(int dfd, const char *name, unsigned int flags, | |||
| 1820 | struct dentry *root = nd->root.dentry; | 1817 | struct dentry *root = nd->root.dentry; | 
| 1821 | struct inode *inode = root->d_inode; | 1818 | struct inode *inode = root->d_inode; | 
| 1822 | if (*name) { | 1819 | if (*name) { | 
| 1823 | if (!d_is_directory(root)) | 1820 | if (!d_can_lookup(root)) | 
| 1824 | return -ENOTDIR; | 1821 | return -ENOTDIR; | 
| 1825 | retval = inode_permission(inode, MAY_EXEC); | 1822 | retval = inode_permission(inode, MAY_EXEC); | 
| 1826 | if (retval) | 1823 | if (retval) | 
| @@ -1876,7 +1873,7 @@ static int path_init(int dfd, const char *name, unsigned int flags, | |||
| 1876 | dentry = f.file->f_path.dentry; | 1873 | dentry = f.file->f_path.dentry; | 
| 1877 | 1874 | ||
| 1878 | if (*name) { | 1875 | if (*name) { | 
| 1879 | if (!d_is_directory(dentry)) { | 1876 | if (!d_can_lookup(dentry)) { | 
| 1880 | fdput(f); | 1877 | fdput(f); | 
| 1881 | return -ENOTDIR; | 1878 | return -ENOTDIR; | 
| 1882 | } | 1879 | } | 
| @@ -1884,7 +1881,7 @@ static int path_init(int dfd, const char *name, unsigned int flags, | |||
| 1884 | 1881 | ||
| 1885 | nd->path = f.file->f_path; | 1882 | nd->path = f.file->f_path; | 
| 1886 | if (flags & LOOKUP_RCU) { | 1883 | if (flags & LOOKUP_RCU) { | 
| 1887 | if (f.need_put) | 1884 | if (f.flags & FDPUT_FPUT) | 
| 1888 | *fp = f.file; | 1885 | *fp = f.file; | 
| 1889 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); | 1886 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); | 
| 1890 | rcu_read_lock(); | 1887 | rcu_read_lock(); | 
| @@ -1958,7 +1955,7 @@ static int path_lookupat(int dfd, const char *name, | |||
| 1958 | err = complete_walk(nd); | 1955 | err = complete_walk(nd); | 
| 1959 | 1956 | ||
| 1960 | if (!err && nd->flags & LOOKUP_DIRECTORY) { | 1957 | if (!err && nd->flags & LOOKUP_DIRECTORY) { | 
| 1961 | if (!d_is_directory(nd->path.dentry)) { | 1958 | if (!d_can_lookup(nd->path.dentry)) { | 
| 1962 | path_put(&nd->path); | 1959 | path_put(&nd->path); | 
| 1963 | err = -ENOTDIR; | 1960 | err = -ENOTDIR; | 
| 1964 | } | 1961 | } | 
| @@ -2417,11 +2414,11 @@ static int may_delete(struct inode *dir, struct dentry *victim, bool isdir) | |||
| 2417 | IS_IMMUTABLE(inode) || IS_SWAPFILE(inode)) | 2414 | IS_IMMUTABLE(inode) || IS_SWAPFILE(inode)) | 
| 2418 | return -EPERM; | 2415 | return -EPERM; | 
| 2419 | if (isdir) { | 2416 | if (isdir) { | 
| 2420 | if (!d_is_directory(victim) && !d_is_autodir(victim)) | 2417 | if (!d_is_dir(victim)) | 
| 2421 | return -ENOTDIR; | 2418 | return -ENOTDIR; | 
| 2422 | if (IS_ROOT(victim)) | 2419 | if (IS_ROOT(victim)) | 
| 2423 | return -EBUSY; | 2420 | return -EBUSY; | 
| 2424 | } else if (d_is_directory(victim) || d_is_autodir(victim)) | 2421 | } else if (d_is_dir(victim)) | 
| 2425 | return -EISDIR; | 2422 | return -EISDIR; | 
| 2426 | if (IS_DEADDIR(dir)) | 2423 | if (IS_DEADDIR(dir)) | 
| 2427 | return -ENOENT; | 2424 | return -ENOENT; | 
| @@ -2572,7 +2569,7 @@ static int handle_truncate(struct file *filp) | |||
| 2572 | /* | 2569 | /* | 
| 2573 | * Refuse to truncate files with mandatory locks held on them. | 2570 | * Refuse to truncate files with mandatory locks held on them. | 
| 2574 | */ | 2571 | */ | 
| 2575 | error = locks_verify_locked(inode); | 2572 | error = locks_verify_locked(filp); | 
| 2576 | if (!error) | 2573 | if (!error) | 
| 2577 | error = security_path_truncate(path); | 2574 | error = security_path_truncate(path); | 
| 2578 | if (!error) { | 2575 | if (!error) { | 
| @@ -3019,11 +3016,10 @@ finish_open: | |||
| 3019 | } | 3016 | } | 
| 3020 | audit_inode(name, nd->path.dentry, 0); | 3017 | audit_inode(name, nd->path.dentry, 0); | 
| 3021 | error = -EISDIR; | 3018 | error = -EISDIR; | 
| 3022 | if ((open_flag & O_CREAT) && | 3019 | if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry)) | 
| 3023 | (d_is_directory(nd->path.dentry) || d_is_autodir(nd->path.dentry))) | ||
| 3024 | goto out; | 3020 | goto out; | 
| 3025 | error = -ENOTDIR; | 3021 | error = -ENOTDIR; | 
| 3026 | if ((nd->flags & LOOKUP_DIRECTORY) && !d_is_directory(nd->path.dentry)) | 3022 | if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) | 
| 3027 | goto out; | 3023 | goto out; | 
| 3028 | if (!S_ISREG(nd->inode->i_mode)) | 3024 | if (!S_ISREG(nd->inode->i_mode)) | 
| 3029 | will_truncate = false; | 3025 | will_truncate = false; | 
| @@ -3747,7 +3743,7 @@ exit1: | |||
| 3747 | slashes: | 3743 | slashes: | 
| 3748 | if (d_is_negative(dentry)) | 3744 | if (d_is_negative(dentry)) | 
| 3749 | error = -ENOENT; | 3745 | error = -ENOENT; | 
| 3750 | else if (d_is_directory(dentry) || d_is_autodir(dentry)) | 3746 | else if (d_is_dir(dentry)) | 
| 3751 | error = -EISDIR; | 3747 | error = -EISDIR; | 
| 3752 | else | 3748 | else | 
| 3753 | error = -ENOTDIR; | 3749 | error = -ENOTDIR; | 
| @@ -3977,7 +3973,28 @@ SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname | |||
| 3977 | return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); | 3973 | return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); | 
| 3978 | } | 3974 | } | 
| 3979 | 3975 | ||
| 3980 | /* | 3976 | /** | 
| 3977 | * vfs_rename - rename a filesystem object | ||
| 3978 | * @old_dir: parent of source | ||
| 3979 | * @old_dentry: source | ||
| 3980 | * @new_dir: parent of destination | ||
| 3981 | * @new_dentry: destination | ||
| 3982 | * @delegated_inode: returns an inode needing a delegation break | ||
| 3983 | * @flags: rename flags | ||
| 3984 | * | ||
| 3985 | * The caller must hold multiple mutexes--see lock_rename()). | ||
| 3986 | * | ||
| 3987 | * If vfs_rename discovers a delegation in need of breaking at either | ||
| 3988 | * the source or destination, it will return -EWOULDBLOCK and return a | ||
| 3989 | * reference to the inode in delegated_inode. The caller should then | ||
| 3990 | * break the delegation and retry. Because breaking a delegation may | ||
| 3991 | * take a long time, the caller should drop all locks before doing | ||
| 3992 | * so. | ||
| 3993 | * | ||
| 3994 | * Alternatively, a caller may pass NULL for delegated_inode. This may | ||
| 3995 | * be appropriate for callers that expect the underlying filesystem not | ||
| 3996 | * to be NFS exported. | ||
| 3997 | * | ||
| 3981 | * The worst of all namespace operations - renaming directory. "Perverted" | 3998 | * The worst of all namespace operations - renaming directory. "Perverted" | 
| 3982 | * doesn't even start to describe it. Somebody in UCB had a heck of a trip... | 3999 | * doesn't even start to describe it. Somebody in UCB had a heck of a trip... | 
| 3983 | * Problems: | 4000 | * Problems: | 
| @@ -4005,163 +4022,139 @@ SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname | |||
| 4005 | * ->i_mutex on parents, which works but leads to some truly excessive | 4022 | * ->i_mutex on parents, which works but leads to some truly excessive | 
| 4006 | * locking]. | 4023 | * locking]. | 
| 4007 | */ | 4024 | */ | 
| 4008 | static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, | 4025 | int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, | 
| 4009 | struct inode *new_dir, struct dentry *new_dentry) | 4026 | struct inode *new_dir, struct dentry *new_dentry, | 
| 4027 | struct inode **delegated_inode, unsigned int flags) | ||
| 4010 | { | 4028 | { | 
| 4011 | int error = 0; | 4029 | int error; | 
| 4030 | bool is_dir = d_is_dir(old_dentry); | ||
| 4031 | const unsigned char *old_name; | ||
| 4032 | struct inode *source = old_dentry->d_inode; | ||
| 4012 | struct inode *target = new_dentry->d_inode; | 4033 | struct inode *target = new_dentry->d_inode; | 
| 4034 | bool new_is_dir = false; | ||
| 4013 | unsigned max_links = new_dir->i_sb->s_max_links; | 4035 | unsigned max_links = new_dir->i_sb->s_max_links; | 
| 4014 | 4036 | ||
| 4037 | if (source == target) | ||
| 4038 | return 0; | ||
| 4039 | |||
| 4040 | error = may_delete(old_dir, old_dentry, is_dir); | ||
| 4041 | if (error) | ||
| 4042 | return error; | ||
| 4043 | |||
| 4044 | if (!target) { | ||
| 4045 | error = may_create(new_dir, new_dentry); | ||
| 4046 | } else { | ||
| 4047 | new_is_dir = d_is_dir(new_dentry); | ||
| 4048 | |||
| 4049 | if (!(flags & RENAME_EXCHANGE)) | ||
| 4050 | error = may_delete(new_dir, new_dentry, is_dir); | ||
| 4051 | else | ||
| 4052 | error = may_delete(new_dir, new_dentry, new_is_dir); | ||
| 4053 | } | ||
| 4054 | if (error) | ||
| 4055 | return error; | ||
| 4056 | |||
| 4057 | if (!old_dir->i_op->rename) | ||
| 4058 | return -EPERM; | ||
| 4059 | |||
| 4060 | if (flags && !old_dir->i_op->rename2) | ||
| 4061 | return -EINVAL; | ||
| 4062 | |||
| 4015 | /* | 4063 | /* | 
| 4016 | * If we are going to change the parent - check write permissions, | 4064 | * If we are going to change the parent - check write permissions, | 
| 4017 | * we'll need to flip '..'. | 4065 | * we'll need to flip '..'. | 
| 4018 | */ | 4066 | */ | 
| 4019 | if (new_dir != old_dir) { | 4067 | if (new_dir != old_dir) { | 
| 4020 | error = inode_permission(old_dentry->d_inode, MAY_WRITE); | 4068 | if (is_dir) { | 
| 4021 | if (error) | 4069 | error = inode_permission(source, MAY_WRITE); | 
| 4022 | return error; | 4070 | if (error) | 
| 4071 | return error; | ||
| 4072 | } | ||
| 4073 | if ((flags & RENAME_EXCHANGE) && new_is_dir) { | ||
| 4074 | error = inode_permission(target, MAY_WRITE); | ||
| 4075 | if (error) | ||
| 4076 | return error; | ||
| 4077 | } | ||
| 4023 | } | 4078 | } | 
| 4024 | 4079 | ||
| 4025 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); | 4080 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry, | 
| 4081 | flags); | ||
| 4026 | if (error) | 4082 | if (error) | 
| 4027 | return error; | 4083 | return error; | 
| 4028 | 4084 | ||
| 4085 | old_name = fsnotify_oldname_init(old_dentry->d_name.name); | ||
| 4029 | dget(new_dentry); | 4086 | dget(new_dentry); | 
| 4030 | if (target) | 4087 | if (!is_dir || (flags & RENAME_EXCHANGE)) | 
| 4088 | lock_two_nondirectories(source, target); | ||
| 4089 | else if (target) | ||
| 4031 | mutex_lock(&target->i_mutex); | 4090 | mutex_lock(&target->i_mutex); | 
| 4032 | 4091 | ||
| 4033 | error = -EBUSY; | 4092 | error = -EBUSY; | 
| 4034 | if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) | 4093 | if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) | 
| 4035 | goto out; | 4094 | goto out; | 
| 4036 | 4095 | ||
| 4037 | error = -EMLINK; | 4096 | if (max_links && new_dir != old_dir) { | 
| 4038 | if (max_links && !target && new_dir != old_dir && | 4097 | error = -EMLINK; | 
| 4039 | new_dir->i_nlink >= max_links) | 4098 | if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links) | 
| 4040 | goto out; | 4099 | goto out; | 
| 4041 | 4100 | if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir && | |
| 4042 | if (target) | 4101 | old_dir->i_nlink >= max_links) | 
| 4102 | goto out; | ||
| 4103 | } | ||
| 4104 | if (is_dir && !(flags & RENAME_EXCHANGE) && target) | ||
| 4043 | shrink_dcache_parent(new_dentry); | 4105 | shrink_dcache_parent(new_dentry); | 
| 4044 | error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); | 4106 | if (!is_dir) { | 
| 4045 | if (error) | 4107 | error = try_break_deleg(source, delegated_inode); | 
| 4046 | goto out; | 4108 | if (error) | 
| 4047 | 4109 | goto out; | |
| 4048 | if (target) { | ||
| 4049 | target->i_flags |= S_DEAD; | ||
| 4050 | dont_mount(new_dentry); | ||
| 4051 | } | 4110 | } | 
| 4052 | out: | 4111 | if (target && !new_is_dir) { | 
| 4053 | if (target) | ||
| 4054 | mutex_unlock(&target->i_mutex); | ||
| 4055 | dput(new_dentry); | ||
| 4056 | if (!error) | ||
| 4057 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) | ||
| 4058 | d_move(old_dentry,new_dentry); | ||
| 4059 | return error; | ||
| 4060 | } | ||
| 4061 | |||
| 4062 | static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, | ||
| 4063 | struct inode *new_dir, struct dentry *new_dentry, | ||
| 4064 | struct inode **delegated_inode) | ||
| 4065 | { | ||
| 4066 | struct inode *target = new_dentry->d_inode; | ||
| 4067 | struct inode *source = old_dentry->d_inode; | ||
| 4068 | int error; | ||
| 4069 | |||
| 4070 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); | ||
| 4071 | if (error) | ||
| 4072 | return error; | ||
| 4073 | |||
| 4074 | dget(new_dentry); | ||
| 4075 | lock_two_nondirectories(source, target); | ||
| 4076 | |||
| 4077 | error = -EBUSY; | ||
| 4078 | if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) | ||
| 4079 | goto out; | ||
| 4080 | |||
| 4081 | error = try_break_deleg(source, delegated_inode); | ||
| 4082 | if (error) | ||
| 4083 | goto out; | ||
| 4084 | if (target) { | ||
| 4085 | error = try_break_deleg(target, delegated_inode); | 4112 | error = try_break_deleg(target, delegated_inode); | 
| 4086 | if (error) | 4113 | if (error) | 
| 4087 | goto out; | 4114 | goto out; | 
| 4088 | } | 4115 | } | 
| 4089 | error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); | 4116 | if (!flags) { | 
| 4117 | error = old_dir->i_op->rename(old_dir, old_dentry, | ||
| 4118 | new_dir, new_dentry); | ||
| 4119 | } else { | ||
| 4120 | error = old_dir->i_op->rename2(old_dir, old_dentry, | ||
| 4121 | new_dir, new_dentry, flags); | ||
| 4122 | } | ||
| 4090 | if (error) | 4123 | if (error) | 
| 4091 | goto out; | 4124 | goto out; | 
| 4092 | 4125 | ||
| 4093 | if (target) | 4126 | if (!(flags & RENAME_EXCHANGE) && target) { | 
| 4127 | if (is_dir) | ||
| 4128 | target->i_flags |= S_DEAD; | ||
| 4094 | dont_mount(new_dentry); | 4129 | dont_mount(new_dentry); | 
| 4095 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) | 4130 | } | 
| 4096 | d_move(old_dentry, new_dentry); | 4131 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { | 
| 4132 | if (!(flags & RENAME_EXCHANGE)) | ||
| 4133 | d_move(old_dentry, new_dentry); | ||
| 4134 | else | ||
| 4135 | d_exchange(old_dentry, new_dentry); | ||
| 4136 | } | ||
| 4097 | out: | 4137 | out: | 
| 4098 | unlock_two_nondirectories(source, target); | 4138 | if (!is_dir || (flags & RENAME_EXCHANGE)) | 
| 4139 | unlock_two_nondirectories(source, target); | ||
| 4140 | else if (target) | ||
| 4141 | mutex_unlock(&target->i_mutex); | ||
| 4099 | dput(new_dentry); | 4142 | dput(new_dentry); | 
| 4100 | return error; | 4143 | if (!error) { | 
| 4101 | } | ||
| 4102 | |||
| 4103 | /** | ||
| 4104 | * vfs_rename - rename a filesystem object | ||
| 4105 | * @old_dir: parent of source | ||
| 4106 | * @old_dentry: source | ||
| 4107 | * @new_dir: parent of destination | ||
| 4108 | * @new_dentry: destination | ||
| 4109 | * @delegated_inode: returns an inode needing a delegation break | ||
| 4110 | * | ||
| 4111 | * The caller must hold multiple mutexes--see lock_rename()). | ||
| 4112 | * | ||
| 4113 | * If vfs_rename discovers a delegation in need of breaking at either | ||
| 4114 | * the source or destination, it will return -EWOULDBLOCK and return a | ||
| 4115 | * reference to the inode in delegated_inode. The caller should then | ||
| 4116 | * break the delegation and retry. Because breaking a delegation may | ||
| 4117 | * take a long time, the caller should drop all locks before doing | ||
| 4118 | * so. | ||
| 4119 | * | ||
| 4120 | * Alternatively, a caller may pass NULL for delegated_inode. This may | ||
| 4121 | * be appropriate for callers that expect the underlying filesystem not | ||
| 4122 | * to be NFS exported. | ||
| 4123 | */ | ||
| 4124 | int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, | ||
| 4125 | struct inode *new_dir, struct dentry *new_dentry, | ||
| 4126 | struct inode **delegated_inode) | ||
| 4127 | { | ||
| 4128 | int error; | ||
| 4129 | int is_dir = d_is_directory(old_dentry) || d_is_autodir(old_dentry); | ||
| 4130 | const unsigned char *old_name; | ||
| 4131 | |||
| 4132 | if (old_dentry->d_inode == new_dentry->d_inode) | ||
| 4133 | return 0; | ||
| 4134 | |||
| 4135 | error = may_delete(old_dir, old_dentry, is_dir); | ||
| 4136 | if (error) | ||
| 4137 | return error; | ||
| 4138 | |||
| 4139 | if (!new_dentry->d_inode) | ||
| 4140 | error = may_create(new_dir, new_dentry); | ||
| 4141 | else | ||
| 4142 | error = may_delete(new_dir, new_dentry, is_dir); | ||
| 4143 | if (error) | ||
| 4144 | return error; | ||
| 4145 | |||
| 4146 | if (!old_dir->i_op->rename) | ||
| 4147 | return -EPERM; | ||
| 4148 | |||
| 4149 | old_name = fsnotify_oldname_init(old_dentry->d_name.name); | ||
| 4150 | |||
| 4151 | if (is_dir) | ||
| 4152 | error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); | ||
| 4153 | else | ||
| 4154 | error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry,delegated_inode); | ||
| 4155 | if (!error) | ||
| 4156 | fsnotify_move(old_dir, new_dir, old_name, is_dir, | 4144 | fsnotify_move(old_dir, new_dir, old_name, is_dir, | 
| 4157 | new_dentry->d_inode, old_dentry); | 4145 | !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry); | 
| 4146 | if (flags & RENAME_EXCHANGE) { | ||
| 4147 | fsnotify_move(new_dir, old_dir, old_dentry->d_name.name, | ||
| 4148 | new_is_dir, NULL, new_dentry); | ||
| 4149 | } | ||
| 4150 | } | ||
| 4158 | fsnotify_oldname_free(old_name); | 4151 | fsnotify_oldname_free(old_name); | 
| 4159 | 4152 | ||
| 4160 | return error; | 4153 | return error; | 
| 4161 | } | 4154 | } | 
| 4162 | 4155 | ||
| 4163 | SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, | 4156 | SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname, | 
| 4164 | int, newdfd, const char __user *, newname) | 4157 | int, newdfd, const char __user *, newname, unsigned int, flags) | 
| 4165 | { | 4158 | { | 
| 4166 | struct dentry *old_dir, *new_dir; | 4159 | struct dentry *old_dir, *new_dir; | 
| 4167 | struct dentry *old_dentry, *new_dentry; | 4160 | struct dentry *old_dentry, *new_dentry; | 
| @@ -4173,6 +4166,13 @@ SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, | |||
| 4173 | unsigned int lookup_flags = 0; | 4166 | unsigned int lookup_flags = 0; | 
| 4174 | bool should_retry = false; | 4167 | bool should_retry = false; | 
| 4175 | int error; | 4168 | int error; | 
| 4169 | |||
| 4170 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE)) | ||
| 4171 | return -EINVAL; | ||
| 4172 | |||
| 4173 | if ((flags & RENAME_NOREPLACE) && (flags & RENAME_EXCHANGE)) | ||
| 4174 | return -EINVAL; | ||
| 4175 | |||
| 4176 | retry: | 4176 | retry: | 
| 4177 | from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags); | 4177 | from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags); | 
| 4178 | if (IS_ERR(from)) { | 4178 | if (IS_ERR(from)) { | 
| @@ -4196,6 +4196,8 @@ retry: | |||
| 4196 | goto exit2; | 4196 | goto exit2; | 
| 4197 | 4197 | ||
| 4198 | new_dir = newnd.path.dentry; | 4198 | new_dir = newnd.path.dentry; | 
| 4199 | if (flags & RENAME_NOREPLACE) | ||
| 4200 | error = -EEXIST; | ||
| 4199 | if (newnd.last_type != LAST_NORM) | 4201 | if (newnd.last_type != LAST_NORM) | 
| 4200 | goto exit2; | 4202 | goto exit2; | 
| 4201 | 4203 | ||
| @@ -4205,7 +4207,8 @@ retry: | |||
| 4205 | 4207 | ||
| 4206 | oldnd.flags &= ~LOOKUP_PARENT; | 4208 | oldnd.flags &= ~LOOKUP_PARENT; | 
| 4207 | newnd.flags &= ~LOOKUP_PARENT; | 4209 | newnd.flags &= ~LOOKUP_PARENT; | 
| 4208 | newnd.flags |= LOOKUP_RENAME_TARGET; | 4210 | if (!(flags & RENAME_EXCHANGE)) | 
| 4211 | newnd.flags |= LOOKUP_RENAME_TARGET; | ||
| 4209 | 4212 | ||
| 4210 | retry_deleg: | 4213 | retry_deleg: | 
| 4211 | trap = lock_rename(new_dir, old_dir); | 4214 | trap = lock_rename(new_dir, old_dir); | 
| @@ -4218,34 +4221,49 @@ retry_deleg: | |||
| 4218 | error = -ENOENT; | 4221 | error = -ENOENT; | 
| 4219 | if (d_is_negative(old_dentry)) | 4222 | if (d_is_negative(old_dentry)) | 
| 4220 | goto exit4; | 4223 | goto exit4; | 
| 4224 | new_dentry = lookup_hash(&newnd); | ||
| 4225 | error = PTR_ERR(new_dentry); | ||
| 4226 | if (IS_ERR(new_dentry)) | ||
| 4227 | goto exit4; | ||
| 4228 | error = -EEXIST; | ||
| 4229 | if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) | ||
| 4230 | goto exit5; | ||
| 4231 | if (flags & RENAME_EXCHANGE) { | ||
| 4232 | error = -ENOENT; | ||
| 4233 | if (d_is_negative(new_dentry)) | ||
| 4234 | goto exit5; | ||
| 4235 | |||
| 4236 | if (!d_is_dir(new_dentry)) { | ||
| 4237 | error = -ENOTDIR; | ||
| 4238 | if (newnd.last.name[newnd.last.len]) | ||
| 4239 | goto exit5; | ||
| 4240 | } | ||
| 4241 | } | ||
| 4221 | /* unless the source is a directory trailing slashes give -ENOTDIR */ | 4242 | /* unless the source is a directory trailing slashes give -ENOTDIR */ | 
| 4222 | if (!d_is_directory(old_dentry) && !d_is_autodir(old_dentry)) { | 4243 | if (!d_is_dir(old_dentry)) { | 
| 4223 | error = -ENOTDIR; | 4244 | error = -ENOTDIR; | 
| 4224 | if (oldnd.last.name[oldnd.last.len]) | 4245 | if (oldnd.last.name[oldnd.last.len]) | 
| 4225 | goto exit4; | 4246 | goto exit5; | 
| 4226 | if (newnd.last.name[newnd.last.len]) | 4247 | if (!(flags & RENAME_EXCHANGE) && newnd.last.name[newnd.last.len]) | 
| 4227 | goto exit4; | 4248 | goto exit5; | 
| 4228 | } | 4249 | } | 
| 4229 | /* source should not be ancestor of target */ | 4250 | /* source should not be ancestor of target */ | 
| 4230 | error = -EINVAL; | 4251 | error = -EINVAL; | 
| 4231 | if (old_dentry == trap) | 4252 | if (old_dentry == trap) | 
| 4232 | goto exit4; | 4253 | goto exit5; | 
| 4233 | new_dentry = lookup_hash(&newnd); | ||
| 4234 | error = PTR_ERR(new_dentry); | ||
| 4235 | if (IS_ERR(new_dentry)) | ||
| 4236 | goto exit4; | ||
| 4237 | /* target should not be an ancestor of source */ | 4254 | /* target should not be an ancestor of source */ | 
| 4238 | error = -ENOTEMPTY; | 4255 | if (!(flags & RENAME_EXCHANGE)) | 
| 4256 | error = -ENOTEMPTY; | ||
| 4239 | if (new_dentry == trap) | 4257 | if (new_dentry == trap) | 
| 4240 | goto exit5; | 4258 | goto exit5; | 
| 4241 | 4259 | ||
| 4242 | error = security_path_rename(&oldnd.path, old_dentry, | 4260 | error = security_path_rename(&oldnd.path, old_dentry, | 
| 4243 | &newnd.path, new_dentry); | 4261 | &newnd.path, new_dentry, flags); | 
| 4244 | if (error) | 4262 | if (error) | 
| 4245 | goto exit5; | 4263 | goto exit5; | 
| 4246 | error = vfs_rename(old_dir->d_inode, old_dentry, | 4264 | error = vfs_rename(old_dir->d_inode, old_dentry, | 
| 4247 | new_dir->d_inode, new_dentry, | 4265 | new_dir->d_inode, new_dentry, | 
| 4248 | &delegated_inode); | 4266 | &delegated_inode, flags); | 
| 4249 | exit5: | 4267 | exit5: | 
| 4250 | dput(new_dentry); | 4268 | dput(new_dentry); | 
| 4251 | exit4: | 4269 | exit4: | 
| @@ -4275,9 +4293,15 @@ exit: | |||
| 4275 | return error; | 4293 | return error; | 
| 4276 | } | 4294 | } | 
| 4277 | 4295 | ||
| 4296 | SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, | ||
| 4297 | int, newdfd, const char __user *, newname) | ||
| 4298 | { | ||
| 4299 | return sys_renameat2(olddfd, oldname, newdfd, newname, 0); | ||
| 4300 | } | ||
| 4301 | |||
| 4278 | SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) | 4302 | SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) | 
| 4279 | { | 4303 | { | 
| 4280 | return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); | 4304 | return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0); | 
| 4281 | } | 4305 | } | 
| 4282 | 4306 | ||
| 4283 | int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) | 4307 | int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) | 
