@@ -1670,6 +1670,8 @@ static struct dentry *lookup_dcache(const struct qstr *name,
16701670 * dentries - as the matter of fact, this only gets called
16711671 * when directory is guaranteed to have no in-lookup children
16721672 * at all.
1673+ * Will return -ENOENT if name isn't found and LOOKUP_CREATE wasn't passed.
1674+ * Will return -EEXIST if name is found and LOOKUP_EXCL was passed.
16731675 */
16741676struct dentry * lookup_one_qstr_excl (const struct qstr * name ,
16751677 struct dentry * base ,
@@ -1680,7 +1682,7 @@ struct dentry *lookup_one_qstr_excl(const struct qstr *name,
16801682 struct inode * dir = base -> d_inode ;
16811683
16821684 if (dentry )
1683- return dentry ;
1685+ goto found ;
16841686
16851687 /* Don't create child dentry for a dead directory. */
16861688 if (unlikely (IS_DEADDIR (dir )))
@@ -1695,6 +1697,17 @@ struct dentry *lookup_one_qstr_excl(const struct qstr *name,
16951697 dput (dentry );
16961698 dentry = old ;
16971699 }
1700+ found :
1701+ if (IS_ERR (dentry ))
1702+ return dentry ;
1703+ if (d_is_negative (dentry ) && !(flags & LOOKUP_CREATE )) {
1704+ dput (dentry );
1705+ return ERR_PTR (- ENOENT );
1706+ }
1707+ if (d_is_positive (dentry ) && (flags & LOOKUP_EXCL )) {
1708+ dput (dentry );
1709+ return ERR_PTR (- EEXIST );
1710+ }
16981711 return dentry ;
16991712}
17001713EXPORT_SYMBOL (lookup_one_qstr_excl );
@@ -2741,10 +2754,6 @@ static struct dentry *__kern_path_locked(int dfd, struct filename *name, struct
27412754 }
27422755 inode_lock_nested (path -> dentry -> d_inode , I_MUTEX_PARENT );
27432756 d = lookup_one_qstr_excl (& last , path -> dentry , 0 );
2744- if (!IS_ERR (d ) && d_is_negative (d )) {
2745- dput (d );
2746- d = ERR_PTR (- ENOENT );
2747- }
27482757 if (IS_ERR (d )) {
27492758 inode_unlock (path -> dentry -> d_inode );
27502759 path_put (path );
@@ -4082,27 +4091,13 @@ static struct dentry *filename_create(int dfd, struct filename *name,
40824091 * '/', and a directory wasn't requested.
40834092 */
40844093 if (last .name [last .len ] && !want_dir )
4085- create_flags = 0 ;
4094+ create_flags &= ~ LOOKUP_CREATE ;
40864095 inode_lock_nested (path -> dentry -> d_inode , I_MUTEX_PARENT );
40874096 dentry = lookup_one_qstr_excl (& last , path -> dentry ,
40884097 reval_flag | create_flags );
40894098 if (IS_ERR (dentry ))
40904099 goto unlock ;
40914100
4092- error = - EEXIST ;
4093- if (d_is_positive (dentry ))
4094- goto fail ;
4095-
4096- /*
4097- * Special case - lookup gave negative, but... we had foo/bar/
4098- * From the vfs_mknod() POV we just have a negative dentry -
4099- * all is fine. Let's be bastards - you had / on the end, you've
4100- * been asking for (non-existent) directory. -ENOENT for you.
4101- */
4102- if (unlikely (!create_flags )) {
4103- error = - ENOENT ;
4104- goto fail ;
4105- }
41064101 if (unlikely (err2 )) {
41074102 error = err2 ;
41084103 goto fail ;
@@ -4449,10 +4444,6 @@ int do_rmdir(int dfd, struct filename *name)
44494444 error = PTR_ERR (dentry );
44504445 if (IS_ERR (dentry ))
44514446 goto exit3 ;
4452- if (!dentry -> d_inode ) {
4453- error = - ENOENT ;
4454- goto exit4 ;
4455- }
44564447 error = security_path_rmdir (& path , dentry );
44574448 if (error )
44584449 goto exit4 ;
@@ -4583,7 +4574,7 @@ int do_unlinkat(int dfd, struct filename *name)
45834574 if (!IS_ERR (dentry )) {
45844575
45854576 /* Why not before? Because we want correct error value */
4586- if (last .name [last .len ] || d_is_negative ( dentry ) )
4577+ if (last .name [last .len ])
45874578 goto slashes ;
45884579 inode = dentry -> d_inode ;
45894580 ihold (inode );
@@ -4617,9 +4608,7 @@ int do_unlinkat(int dfd, struct filename *name)
46174608 return error ;
46184609
46194610slashes :
4620- if (d_is_negative (dentry ))
4621- error = - ENOENT ;
4622- else if (d_is_dir (dentry ))
4611+ if (d_is_dir (dentry ))
46234612 error = - EISDIR ;
46244613 else
46254614 error = - ENOTDIR ;
@@ -5119,7 +5108,8 @@ int do_renameat2(int olddfd, struct filename *from, int newdfd,
51195108 struct qstr old_last , new_last ;
51205109 int old_type , new_type ;
51215110 struct inode * delegated_inode = NULL ;
5122- unsigned int lookup_flags = 0 , target_flags = LOOKUP_RENAME_TARGET ;
5111+ unsigned int lookup_flags = 0 , target_flags =
5112+ LOOKUP_RENAME_TARGET | LOOKUP_CREATE ;
51235113 bool should_retry = false;
51245114 int error = - EINVAL ;
51255115
@@ -5132,6 +5122,8 @@ int do_renameat2(int olddfd, struct filename *from, int newdfd,
51325122
51335123 if (flags & RENAME_EXCHANGE )
51345124 target_flags = 0 ;
5125+ if (flags & RENAME_NOREPLACE )
5126+ target_flags |= LOOKUP_EXCL ;
51355127
51365128retry :
51375129 error = filename_parentat (olddfd , from , lookup_flags , & old_path ,
@@ -5173,23 +5165,12 @@ int do_renameat2(int olddfd, struct filename *from, int newdfd,
51735165 error = PTR_ERR (old_dentry );
51745166 if (IS_ERR (old_dentry ))
51755167 goto exit3 ;
5176- /* source must exist */
5177- error = - ENOENT ;
5178- if (d_is_negative (old_dentry ))
5179- goto exit4 ;
51805168 new_dentry = lookup_one_qstr_excl (& new_last , new_path .dentry ,
51815169 lookup_flags | target_flags );
51825170 error = PTR_ERR (new_dentry );
51835171 if (IS_ERR (new_dentry ))
51845172 goto exit4 ;
5185- error = - EEXIST ;
5186- if ((flags & RENAME_NOREPLACE ) && d_is_positive (new_dentry ))
5187- goto exit5 ;
51885173 if (flags & RENAME_EXCHANGE ) {
5189- error = - ENOENT ;
5190- if (d_is_negative (new_dentry ))
5191- goto exit5 ;
5192-
51935174 if (!d_is_dir (new_dentry )) {
51945175 error = - ENOTDIR ;
51955176 if (new_last .name [new_last .len ])
0 commit comments