Skip to content

Commit a4464db

Browse files
author
Al Viro
committed
Make ->d_sb assign-once and always non-NULL
New helper (non-exported, fs/internal.h-only): __d_alloc(sb, name). Allocates dentry, sets its ->d_sb to given superblock and sets ->d_op accordingly. Old d_alloc(NULL, name) callers are converted to that (all of them know what superblock they want). d_alloc() itself is left only for parent != NULl case; uses __d_alloc(), inserts result into the list of parent's children. Note that now ->d_sb is assign-once and never NULL *and* ->d_parent is never NULL either. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent e3c3d9c commit a4464db

3 files changed

Lines changed: 47 additions & 39 deletions

File tree

fs/dcache.c

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,16 +1275,16 @@ static struct shrinker dcache_shrinker = {
12751275
};
12761276

12771277
/**
1278-
* d_alloc - allocate a dcache entry
1279-
* @parent: parent of entry to allocate
1278+
* __d_alloc - allocate a dcache entry
1279+
* @sb: filesystem it will belong to
12801280
* @name: qstr of the name
12811281
*
12821282
* Allocates a dentry. It returns %NULL if there is insufficient memory
12831283
* available. On a success the dentry is returned. The name passed in is
12841284
* copied and the copy passed in may be reused after this call.
12851285
*/
12861286

1287-
struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1287+
struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
12881288
{
12891289
struct dentry *dentry;
12901290
char *dname;
@@ -1314,45 +1314,56 @@ struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
13141314
spin_lock_init(&dentry->d_lock);
13151315
seqcount_init(&dentry->d_seq);
13161316
dentry->d_inode = NULL;
1317-
dentry->d_parent = NULL;
1318-
dentry->d_sb = NULL;
1317+
dentry->d_parent = dentry;
1318+
dentry->d_sb = sb;
13191319
dentry->d_op = NULL;
13201320
dentry->d_fsdata = NULL;
13211321
INIT_HLIST_BL_NODE(&dentry->d_hash);
13221322
INIT_LIST_HEAD(&dentry->d_lru);
13231323
INIT_LIST_HEAD(&dentry->d_subdirs);
13241324
INIT_LIST_HEAD(&dentry->d_alias);
13251325
INIT_LIST_HEAD(&dentry->d_u.d_child);
1326-
1327-
if (parent) {
1328-
spin_lock(&parent->d_lock);
1329-
/*
1330-
* don't need child lock because it is not subject
1331-
* to concurrency here
1332-
*/
1333-
__dget_dlock(parent);
1334-
dentry->d_parent = parent;
1335-
dentry->d_sb = parent->d_sb;
1336-
d_set_d_op(dentry, dentry->d_sb->s_d_op);
1337-
list_add(&dentry->d_u.d_child, &parent->d_subdirs);
1338-
spin_unlock(&parent->d_lock);
1339-
}
1326+
d_set_d_op(dentry, dentry->d_sb->s_d_op);
13401327

13411328
this_cpu_inc(nr_dentry);
13421329

13431330
return dentry;
13441331
}
1332+
1333+
/**
1334+
* d_alloc - allocate a dcache entry
1335+
* @parent: parent of entry to allocate
1336+
* @name: qstr of the name
1337+
*
1338+
* Allocates a dentry. It returns %NULL if there is insufficient memory
1339+
* available. On a success the dentry is returned. The name passed in is
1340+
* copied and the copy passed in may be reused after this call.
1341+
*/
1342+
struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1343+
{
1344+
struct dentry *dentry = __d_alloc(parent->d_sb, name);
1345+
if (!dentry)
1346+
return NULL;
1347+
1348+
spin_lock(&parent->d_lock);
1349+
/*
1350+
* don't need child lock because it is not subject
1351+
* to concurrency here
1352+
*/
1353+
__dget_dlock(parent);
1354+
dentry->d_parent = parent;
1355+
list_add(&dentry->d_u.d_child, &parent->d_subdirs);
1356+
spin_unlock(&parent->d_lock);
1357+
1358+
return dentry;
1359+
}
13451360
EXPORT_SYMBOL(d_alloc);
13461361

13471362
struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
13481363
{
1349-
struct dentry *dentry = d_alloc(NULL, name);
1350-
if (dentry) {
1351-
dentry->d_sb = sb;
1352-
d_set_d_op(dentry, dentry->d_sb->s_d_op);
1353-
dentry->d_parent = dentry;
1364+
struct dentry *dentry = __d_alloc(sb, name);
1365+
if (dentry)
13541366
dentry->d_flags |= DCACHE_DISCONNECTED;
1355-
}
13561367
return dentry;
13571368
}
13581369
EXPORT_SYMBOL(d_alloc_pseudo);
@@ -1522,13 +1533,9 @@ struct dentry * d_alloc_root(struct inode * root_inode)
15221533
if (root_inode) {
15231534
static const struct qstr name = { .name = "/", .len = 1 };
15241535

1525-
res = d_alloc(NULL, &name);
1526-
if (res) {
1527-
res->d_sb = root_inode->i_sb;
1528-
d_set_d_op(res, res->d_sb->s_d_op);
1529-
res->d_parent = res;
1536+
res = __d_alloc(root_inode->i_sb, &name);
1537+
if (res)
15301538
d_instantiate(res, root_inode);
1531-
}
15321539
}
15331540
return res;
15341541
}
@@ -1589,13 +1596,11 @@ struct dentry *d_obtain_alias(struct inode *inode)
15891596
if (res)
15901597
goto out_iput;
15911598

1592-
tmp = d_alloc(NULL, &anonstring);
1599+
tmp = __d_alloc(inode->i_sb, &anonstring);
15931600
if (!tmp) {
15941601
res = ERR_PTR(-ENOMEM);
15951602
goto out_iput;
15961603
}
1597-
tmp->d_parent = tmp; /* make sure dput doesn't croak */
1598-
15991604

16001605
spin_lock(&inode->i_lock);
16011606
res = __d_find_any_alias(inode);
@@ -1607,8 +1612,6 @@ struct dentry *d_obtain_alias(struct inode *inode)
16071612

16081613
/* attach a disconnected dentry */
16091614
spin_lock(&tmp->d_lock);
1610-
tmp->d_sb = inode->i_sb;
1611-
d_set_d_op(tmp, tmp->d_sb->s_d_op);
16121615
tmp->d_inode = inode;
16131616
tmp->d_flags |= DCACHE_DISCONNECTED;
16141617
list_add(&tmp->d_alias, &inode->i_dentry);

fs/internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,8 @@ extern void inode_wb_list_del(struct inode *inode);
135135
extern int get_nr_dirty_inodes(void);
136136
extern void evict_inodes(struct super_block *);
137137
extern int invalidate_inodes(struct super_block *, bool);
138+
139+
/*
140+
* dcache.c
141+
*/
142+
extern struct dentry *__d_alloc(struct super_block *, const struct qstr *);

fs/libfs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include <asm/uaccess.h>
1818

19+
#include "internal.h"
20+
1921
static inline int simple_positive(struct dentry *dentry)
2022
{
2123
return dentry->d_inode && !d_unhashed(dentry);
@@ -246,13 +248,11 @@ struct dentry *mount_pseudo(struct file_system_type *fs_type, char *name,
246248
root->i_ino = 1;
247249
root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
248250
root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
249-
dentry = d_alloc(NULL, &d_name);
251+
dentry = __d_alloc(s, &d_name);
250252
if (!dentry) {
251253
iput(root);
252254
goto Enomem;
253255
}
254-
dentry->d_sb = s;
255-
dentry->d_parent = dentry;
256256
d_instantiate(dentry, root);
257257
s->s_root = dentry;
258258
s->s_d_op = dops;

0 commit comments

Comments
 (0)