Skip to content

Commit 1c3cb50

Browse files
neilbrownbrauner
authored andcommitted
VFS: change kern_path_locked() and user_path_locked_at() to never return negative dentry
No callers of kern_path_locked() or user_path_locked_at() want a negative dentry. So change them to return -ENOENT instead. This simplifies callers. This results in a subtle change to bcachefs in that an ioctl will now return -ENOENT in preference to -EXDEV. I believe this restores the behaviour to what it was prior to Commit bbe6a7c ("bch2_ioctl_subvolume_destroy(): fix locking") Signed-off-by: NeilBrown <neilb@suse.de> Link: https://lore.kernel.org/r/20250217003020.3170652-2-neilb@suse.de Acked-by: Paul Moore <paul@paul-moore.com> Reviewed-by: Jeff Layton <jlayton@kernel.org> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 2c3230f commit 1c3cb50

5 files changed

Lines changed: 48 additions & 45 deletions

File tree

Documentation/filesystems/porting.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,3 +1157,11 @@ in normal case it points into the pathname being looked up.
11571157
NOTE: if you need something like full path from the root of filesystem,
11581158
you are still on your own - this assists with simple cases, but it's not
11591159
magic.
1160+
1161+
---
1162+
1163+
** recommended**
1164+
1165+
kern_path_locked() and user_path_locked() no longer return a negative
1166+
dentry so this doesn't need to be checked. If the name cannot be found,
1167+
ERR_PTR(-ENOENT) is returned.

drivers/base/devtmpfs.c

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,12 @@ static int dev_rmdir(const char *name)
245245
dentry = kern_path_locked(name, &parent);
246246
if (IS_ERR(dentry))
247247
return PTR_ERR(dentry);
248-
if (d_really_is_positive(dentry)) {
249-
if (d_inode(dentry)->i_private == &thread)
250-
err = vfs_rmdir(&nop_mnt_idmap, d_inode(parent.dentry),
251-
dentry);
252-
else
253-
err = -EPERM;
254-
} else {
255-
err = -ENOENT;
256-
}
248+
if (d_inode(dentry)->i_private == &thread)
249+
err = vfs_rmdir(&nop_mnt_idmap, d_inode(parent.dentry),
250+
dentry);
251+
else
252+
err = -EPERM;
253+
257254
dput(dentry);
258255
inode_unlock(d_inode(parent.dentry));
259256
path_put(&parent);
@@ -310,39 +307,37 @@ static int handle_remove(const char *nodename, struct device *dev)
310307
{
311308
struct path parent;
312309
struct dentry *dentry;
310+
struct kstat stat;
311+
struct path p;
313312
int deleted = 0;
314313
int err;
315314

316315
dentry = kern_path_locked(nodename, &parent);
317316
if (IS_ERR(dentry))
318317
return PTR_ERR(dentry);
319318

320-
if (d_really_is_positive(dentry)) {
321-
struct kstat stat;
322-
struct path p = {.mnt = parent.mnt, .dentry = dentry};
323-
err = vfs_getattr(&p, &stat, STATX_TYPE | STATX_MODE,
324-
AT_STATX_SYNC_AS_STAT);
325-
if (!err && dev_mynode(dev, d_inode(dentry), &stat)) {
326-
struct iattr newattrs;
327-
/*
328-
* before unlinking this node, reset permissions
329-
* of possible references like hardlinks
330-
*/
331-
newattrs.ia_uid = GLOBAL_ROOT_UID;
332-
newattrs.ia_gid = GLOBAL_ROOT_GID;
333-
newattrs.ia_mode = stat.mode & ~0777;
334-
newattrs.ia_valid =
335-
ATTR_UID|ATTR_GID|ATTR_MODE;
336-
inode_lock(d_inode(dentry));
337-
notify_change(&nop_mnt_idmap, dentry, &newattrs, NULL);
338-
inode_unlock(d_inode(dentry));
339-
err = vfs_unlink(&nop_mnt_idmap, d_inode(parent.dentry),
340-
dentry, NULL);
341-
if (!err || err == -ENOENT)
342-
deleted = 1;
343-
}
344-
} else {
345-
err = -ENOENT;
319+
p.mnt = parent.mnt;
320+
p.dentry = dentry;
321+
err = vfs_getattr(&p, &stat, STATX_TYPE | STATX_MODE,
322+
AT_STATX_SYNC_AS_STAT);
323+
if (!err && dev_mynode(dev, d_inode(dentry), &stat)) {
324+
struct iattr newattrs;
325+
/*
326+
* before unlinking this node, reset permissions
327+
* of possible references like hardlinks
328+
*/
329+
newattrs.ia_uid = GLOBAL_ROOT_UID;
330+
newattrs.ia_gid = GLOBAL_ROOT_GID;
331+
newattrs.ia_mode = stat.mode & ~0777;
332+
newattrs.ia_valid =
333+
ATTR_UID|ATTR_GID|ATTR_MODE;
334+
inode_lock(d_inode(dentry));
335+
notify_change(&nop_mnt_idmap, dentry, &newattrs, NULL);
336+
inode_unlock(d_inode(dentry));
337+
err = vfs_unlink(&nop_mnt_idmap, d_inode(parent.dentry),
338+
dentry, NULL);
339+
if (!err || err == -ENOENT)
340+
deleted = 1;
346341
}
347342
dput(dentry);
348343
inode_unlock(d_inode(parent.dentry));

fs/bcachefs/fs-ioctl.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,6 @@ static long bch2_ioctl_subvolume_destroy(struct bch_fs *c, struct file *filp,
511511
ret = -EXDEV;
512512
goto err;
513513
}
514-
if (!d_is_positive(victim)) {
515-
ret = -ENOENT;
516-
goto err;
517-
}
518514
ret = __bch2_unlink(dir, victim, true);
519515
if (!ret) {
520516
fsnotify_rmdir(dir, victim);

fs/namei.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2741,6 +2741,10 @@ static struct dentry *__kern_path_locked(int dfd, struct filename *name, struct
27412741
}
27422742
inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
27432743
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+
}
27442748
if (IS_ERR(d)) {
27452749
inode_unlock(path->dentry->d_inode);
27462750
path_put(path);

kernel/audit_watch.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,10 @@ static int audit_get_nd(struct audit_watch *watch, struct path *parent)
350350
struct dentry *d = kern_path_locked(watch->path, parent);
351351
if (IS_ERR(d))
352352
return PTR_ERR(d);
353-
if (d_is_positive(d)) {
354-
/* update watch filter fields */
355-
watch->dev = d->d_sb->s_dev;
356-
watch->ino = d_backing_inode(d)->i_ino;
357-
}
353+
/* update watch filter fields */
354+
watch->dev = d->d_sb->s_dev;
355+
watch->ino = d_backing_inode(d)->i_ino;
356+
358357
inode_unlock(d_backing_inode(parent->dentry));
359358
dput(d);
360359
return 0;
@@ -419,10 +418,11 @@ int audit_add_watch(struct audit_krule *krule, struct list_head **list)
419418
/* caller expects mutex locked */
420419
mutex_lock(&audit_filter_mutex);
421420

422-
if (ret) {
421+
if (ret && ret != -ENOENT) {
423422
audit_put_watch(watch);
424423
return ret;
425424
}
425+
ret = 0;
426426

427427
/* either find an old parent or attach a new one */
428428
parent = audit_find_parent(d_backing_inode(parent_path.dentry));

0 commit comments

Comments
 (0)