@@ -1670,6 +1670,8 @@ static struct dentry *lookup_dcache(const struct qstr *name,
1670
1670
* dentries - as the matter of fact, this only gets called
1671
1671
* when directory is guaranteed to have no in-lookup children
1672
1672
* 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.
1673
1675
*/
1674
1676
struct dentry * lookup_one_qstr_excl (const struct qstr * name ,
1675
1677
struct dentry * base ,
@@ -1680,7 +1682,7 @@ struct dentry *lookup_one_qstr_excl(const struct qstr *name,
1680
1682
struct inode * dir = base -> d_inode ;
1681
1683
1682
1684
if (dentry )
1683
- return dentry ;
1685
+ goto found ;
1684
1686
1685
1687
/* Don't create child dentry for a dead directory. */
1686
1688
if (unlikely (IS_DEADDIR (dir )))
@@ -1695,6 +1697,17 @@ struct dentry *lookup_one_qstr_excl(const struct qstr *name,
1695
1697
dput (dentry );
1696
1698
dentry = old ;
1697
1699
}
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
+ }
1698
1711
return dentry ;
1699
1712
}
1700
1713
EXPORT_SYMBOL (lookup_one_qstr_excl );
@@ -2741,10 +2754,6 @@ static struct dentry *__kern_path_locked(int dfd, struct filename *name, struct
2741
2754
}
2742
2755
inode_lock_nested (path -> dentry -> d_inode , I_MUTEX_PARENT );
2743
2756
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
- }
2748
2757
if (IS_ERR (d )) {
2749
2758
inode_unlock (path -> dentry -> d_inode );
2750
2759
path_put (path );
@@ -4082,27 +4091,13 @@ static struct dentry *filename_create(int dfd, struct filename *name,
4082
4091
* '/', and a directory wasn't requested.
4083
4092
*/
4084
4093
if (last .name [last .len ] && !want_dir )
4085
- create_flags = 0 ;
4094
+ create_flags &= ~ LOOKUP_CREATE ;
4086
4095
inode_lock_nested (path -> dentry -> d_inode , I_MUTEX_PARENT );
4087
4096
dentry = lookup_one_qstr_excl (& last , path -> dentry ,
4088
4097
reval_flag | create_flags );
4089
4098
if (IS_ERR (dentry ))
4090
4099
goto unlock ;
4091
4100
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
- }
4106
4101
if (unlikely (err2 )) {
4107
4102
error = err2 ;
4108
4103
goto fail ;
@@ -4449,10 +4444,6 @@ int do_rmdir(int dfd, struct filename *name)
4449
4444
error = PTR_ERR (dentry );
4450
4445
if (IS_ERR (dentry ))
4451
4446
goto exit3 ;
4452
- if (!dentry -> d_inode ) {
4453
- error = - ENOENT ;
4454
- goto exit4 ;
4455
- }
4456
4447
error = security_path_rmdir (& path , dentry );
4457
4448
if (error )
4458
4449
goto exit4 ;
@@ -4583,7 +4574,7 @@ int do_unlinkat(int dfd, struct filename *name)
4583
4574
if (!IS_ERR (dentry )) {
4584
4575
4585
4576
/* 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 ])
4587
4578
goto slashes ;
4588
4579
inode = dentry -> d_inode ;
4589
4580
ihold (inode );
@@ -4617,9 +4608,7 @@ int do_unlinkat(int dfd, struct filename *name)
4617
4608
return error ;
4618
4609
4619
4610
slashes :
4620
- if (d_is_negative (dentry ))
4621
- error = - ENOENT ;
4622
- else if (d_is_dir (dentry ))
4611
+ if (d_is_dir (dentry ))
4623
4612
error = - EISDIR ;
4624
4613
else
4625
4614
error = - ENOTDIR ;
@@ -5119,7 +5108,8 @@ int do_renameat2(int olddfd, struct filename *from, int newdfd,
5119
5108
struct qstr old_last , new_last ;
5120
5109
int old_type , new_type ;
5121
5110
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 ;
5123
5113
bool should_retry = false;
5124
5114
int error = - EINVAL ;
5125
5115
@@ -5132,6 +5122,8 @@ int do_renameat2(int olddfd, struct filename *from, int newdfd,
5132
5122
5133
5123
if (flags & RENAME_EXCHANGE )
5134
5124
target_flags = 0 ;
5125
+ if (flags & RENAME_NOREPLACE )
5126
+ target_flags |= LOOKUP_EXCL ;
5135
5127
5136
5128
retry :
5137
5129
error = filename_parentat (olddfd , from , lookup_flags , & old_path ,
@@ -5173,23 +5165,12 @@ int do_renameat2(int olddfd, struct filename *from, int newdfd,
5173
5165
error = PTR_ERR (old_dentry );
5174
5166
if (IS_ERR (old_dentry ))
5175
5167
goto exit3 ;
5176
- /* source must exist */
5177
- error = - ENOENT ;
5178
- if (d_is_negative (old_dentry ))
5179
- goto exit4 ;
5180
5168
new_dentry = lookup_one_qstr_excl (& new_last , new_path .dentry ,
5181
5169
lookup_flags | target_flags );
5182
5170
error = PTR_ERR (new_dentry );
5183
5171
if (IS_ERR (new_dentry ))
5184
5172
goto exit4 ;
5185
- error = - EEXIST ;
5186
- if ((flags & RENAME_NOREPLACE ) && d_is_positive (new_dentry ))
5187
- goto exit5 ;
5188
5173
if (flags & RENAME_EXCHANGE ) {
5189
- error = - ENOENT ;
5190
- if (d_is_negative (new_dentry ))
5191
- goto exit5 ;
5192
-
5193
5174
if (!d_is_dir (new_dentry )) {
5194
5175
error = - ENOTDIR ;
5195
5176
if (new_last .name [new_last .len ])
0 commit comments