Skip to content

Commit 79714f7

Browse files
author
Al Viro
committed
get rid of kern_path_parent()
all callers want the same thing, actually - a kinda-sorta analog of kern_path_create(). I.e. they want parent vfsmount/dentry (with ->i_mutex held, to make sure the child dentry is still their child) + the child dentry. Signed-off-by Al Viro <viro@zeniv.linux.org.uk>
1 parent 1acf0af commit 79714f7

4 files changed

Lines changed: 65 additions & 82 deletions

File tree

‎drivers/base/devtmpfs.c‎

Lines changed: 41 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -227,33 +227,24 @@ static int handle_create(const char *nodename, umode_t mode, struct device *dev)
227227

228228
static int dev_rmdir(const char *name)
229229
{
230-
struct nameidata nd;
230+
struct path parent;
231231
struct dentry *dentry;
232232
int err;
233233

234-
err = kern_path_parent(name, &nd);
235-
if (err)
236-
return err;
237-
238-
mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
239-
dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
240-
if (!IS_ERR(dentry)) {
241-
if (dentry->d_inode) {
242-
if (dentry->d_inode->i_private == &thread)
243-
err = vfs_rmdir(nd.path.dentry->d_inode,
244-
dentry);
245-
else
246-
err = -EPERM;
247-
} else {
248-
err = -ENOENT;
249-
}
250-
dput(dentry);
234+
dentry = kern_path_locked(name, &parent);
235+
if (IS_ERR(dentry))
236+
return PTR_ERR(dentry);
237+
if (dentry->d_inode) {
238+
if (dentry->d_inode->i_private == &thread)
239+
err = vfs_rmdir(parent.dentry->d_inode, dentry);
240+
else
241+
err = -EPERM;
251242
} else {
252-
err = PTR_ERR(dentry);
243+
err = -ENOENT;
253244
}
254-
255-
mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
256-
path_put(&nd.path);
245+
dput(dentry);
246+
mutex_unlock(&parent.dentry->d_inode->i_mutex);
247+
path_put(&parent);
257248
return err;
258249
}
259250

@@ -305,50 +296,43 @@ static int dev_mynode(struct device *dev, struct inode *inode, struct kstat *sta
305296

306297
static int handle_remove(const char *nodename, struct device *dev)
307298
{
308-
struct nameidata nd;
299+
struct path parent;
309300
struct dentry *dentry;
310-
struct kstat stat;
311301
int deleted = 1;
312302
int err;
313303

314-
err = kern_path_parent(nodename, &nd);
315-
if (err)
316-
return err;
304+
dentry = kern_path_locked(nodename, &parent);
305+
if (IS_ERR(dentry))
306+
return PTR_ERR(dentry);
317307

318-
mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
319-
dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
320-
if (!IS_ERR(dentry)) {
321-
if (dentry->d_inode) {
322-
err = vfs_getattr(nd.path.mnt, dentry, &stat);
323-
if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
324-
struct iattr newattrs;
325-
/*
326-
* before unlinking this node, reset permissions
327-
* of possible references like hardlinks
328-
*/
329-
newattrs.ia_uid = 0;
330-
newattrs.ia_gid = 0;
331-
newattrs.ia_mode = stat.mode & ~0777;
332-
newattrs.ia_valid =
333-
ATTR_UID|ATTR_GID|ATTR_MODE;
334-
mutex_lock(&dentry->d_inode->i_mutex);
335-
notify_change(dentry, &newattrs);
336-
mutex_unlock(&dentry->d_inode->i_mutex);
337-
err = vfs_unlink(nd.path.dentry->d_inode,
338-
dentry);
339-
if (!err || err == -ENOENT)
340-
deleted = 1;
341-
}
342-
} else {
343-
err = -ENOENT;
308+
if (dentry->d_inode) {
309+
struct kstat stat;
310+
err = vfs_getattr(parent.mnt, dentry, &stat);
311+
if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
312+
struct iattr newattrs;
313+
/*
314+
* before unlinking this node, reset permissions
315+
* of possible references like hardlinks
316+
*/
317+
newattrs.ia_uid = 0;
318+
newattrs.ia_gid = 0;
319+
newattrs.ia_mode = stat.mode & ~0777;
320+
newattrs.ia_valid =
321+
ATTR_UID|ATTR_GID|ATTR_MODE;
322+
mutex_lock(&dentry->d_inode->i_mutex);
323+
notify_change(dentry, &newattrs);
324+
mutex_unlock(&dentry->d_inode->i_mutex);
325+
err = vfs_unlink(parent.dentry->d_inode, dentry);
326+
if (!err || err == -ENOENT)
327+
deleted = 1;
344328
}
345-
dput(dentry);
346329
} else {
347-
err = PTR_ERR(dentry);
330+
err = -ENOENT;
348331
}
349-
mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
332+
dput(dentry);
333+
mutex_unlock(&parent.dentry->d_inode->i_mutex);
350334

351-
path_put(&nd.path);
335+
path_put(&parent);
352336
if (deleted && strchr(nodename, '/'))
353337
delete_path(nodename);
354338
return err;

‎fs/namei.c‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,9 +1814,27 @@ static int do_path_lookup(int dfd, const char *name,
18141814
return retval;
18151815
}
18161816

1817-
int kern_path_parent(const char *name, struct nameidata *nd)
1817+
/* does lookup, returns the object with parent locked */
1818+
struct dentry *kern_path_locked(const char *name, struct path *path)
18181819
{
1819-
return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
1820+
struct nameidata nd;
1821+
struct dentry *d;
1822+
int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
1823+
if (err)
1824+
return ERR_PTR(err);
1825+
if (nd.last_type != LAST_NORM) {
1826+
path_put(&nd.path);
1827+
return ERR_PTR(-EINVAL);
1828+
}
1829+
mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
1830+
d = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
1831+
if (IS_ERR(d)) {
1832+
mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1833+
path_put(&nd.path);
1834+
return d;
1835+
}
1836+
*path = nd.path;
1837+
return d;
18201838
}
18211839

18221840
int kern_path(const char *name, unsigned int flags, struct path *path)

‎include/linux/namei.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extern int kern_path(const char *, unsigned, struct path *);
6767

6868
extern struct dentry *kern_path_create(int, const char *, struct path *, int);
6969
extern struct dentry *user_path_create(int, const char __user *, struct path *, int);
70-
extern int kern_path_parent(const char *, struct nameidata *);
70+
extern struct dentry *kern_path_locked(const char *, struct path *);
7171
extern int vfs_path_lookup(struct dentry *, struct vfsmount *,
7272
const char *, unsigned int, struct path *);
7373

‎kernel/audit_watch.c‎

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -355,34 +355,15 @@ static void audit_remove_parent_watches(struct audit_parent *parent)
355355
/* Get path information necessary for adding watches. */
356356
static int audit_get_nd(struct audit_watch *watch, struct path *parent)
357357
{
358-
struct nameidata nd;
359-
struct dentry *d;
360-
int err;
361-
362-
err = kern_path_parent(watch->path, &nd);
363-
if (err)
364-
return err;
365-
366-
if (nd.last_type != LAST_NORM) {
367-
path_put(&nd.path);
368-
return -EINVAL;
369-
}
370-
371-
mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
372-
d = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
373-
if (IS_ERR(d)) {
374-
mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
375-
path_put(&nd.path);
358+
struct dentry *d = kern_path_locked(watch->path, parent);
359+
if (IS_ERR(d))
376360
return PTR_ERR(d);
377-
}
361+
mutex_unlock(&parent->dentry->d_inode->i_mutex);
378362
if (d->d_inode) {
379363
/* update watch filter fields */
380364
watch->dev = d->d_inode->i_sb->s_dev;
381365
watch->ino = d->d_inode->i_ino;
382366
}
383-
mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
384-
385-
*parent = nd.path;
386367
dput(d);
387368
return 0;
388369
}

0 commit comments

Comments
 (0)