Skip to content

Commit e5f86dc

Browse files
committed
ceph: avoid d_parent in ceph_dentry_hash; fix ceph_encode_fh() hashing bug
Have caller pass in a safely-obtained reference to the parent directory for calculating a dentry's hash valud. While we're here, simpify the flow through ceph_encode_fh() so that there is a single exit point and cleanup. Also fix a bug with the dentry hash calculation: calculate the hash for the dentry we were given, not its parent. Reviewed-by: Yehuda Sadeh <yehuda@hq.newdream.net> Signed-off-by: Sage Weil <sage@newdream.net>
1 parent bf1c6ac commit e5f86dc

4 files changed

Lines changed: 18 additions & 13 deletions

File tree

‎fs/ceph/dir.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,9 +1244,8 @@ void ceph_dentry_lru_del(struct dentry *dn)
12441244
* Return name hash for a given dentry. This is dependent on
12451245
* the parent directory's hash function.
12461246
*/
1247-
unsigned ceph_dentry_hash(struct dentry *dn)
1247+
unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
12481248
{
1249-
struct inode *dir = dn->d_parent->d_inode;
12501249
struct ceph_inode_info *dci = ceph_inode(dir);
12511250

12521251
switch (dci->i_dir_layout.dl_dir_hash) {

‎fs/ceph/export.c‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static int ceph_encode_fh(struct dentry *dentry, u32 *rawfh, int *max_len,
4646
int type;
4747
struct ceph_nfs_fh *fh = (void *)rawfh;
4848
struct ceph_nfs_confh *cfh = (void *)rawfh;
49-
struct dentry *parent = dentry->d_parent;
49+
struct dentry *parent;
5050
struct inode *inode = dentry->d_inode;
5151
int connected_handle_length = sizeof(*cfh)/4;
5252
int handle_length = sizeof(*fh)/4;
@@ -55,26 +55,33 @@ static int ceph_encode_fh(struct dentry *dentry, u32 *rawfh, int *max_len,
5555
if (ceph_snap(inode) != CEPH_NOSNAP)
5656
return -EINVAL;
5757

58+
spin_lock(&dentry->d_lock);
59+
parent = dget(dentry->d_parent);
60+
spin_unlock(&dentry->d_lock);
61+
5862
if (*max_len >= connected_handle_length) {
5963
dout("encode_fh %p connectable\n", dentry);
6064
cfh->ino = ceph_ino(dentry->d_inode);
6165
cfh->parent_ino = ceph_ino(parent->d_inode);
62-
cfh->parent_name_hash = ceph_dentry_hash(parent);
66+
cfh->parent_name_hash = ceph_dentry_hash(parent->d_inode,
67+
dentry);
6368
*max_len = connected_handle_length;
6469
type = 2;
6570
} else if (*max_len >= handle_length) {
6671
if (connectable) {
6772
*max_len = connected_handle_length;
68-
return 255;
73+
type = 255;
74+
} else {
75+
dout("encode_fh %p\n", dentry);
76+
fh->ino = ceph_ino(dentry->d_inode);
77+
*max_len = handle_length;
78+
type = 1;
6979
}
70-
dout("encode_fh %p\n", dentry);
71-
fh->ino = ceph_ino(dentry->d_inode);
72-
*max_len = handle_length;
73-
type = 1;
7480
} else {
7581
*max_len = handle_length;
76-
return 255;
82+
type = 255;
7783
}
84+
dput(parent);
7885
return type;
7986
}
8087

@@ -123,7 +130,6 @@ static struct dentry *__fh_to_dentry(struct super_block *sb,
123130
return dentry;
124131
}
125132
err = ceph_init_dentry(dentry);
126-
127133
if (err < 0) {
128134
iput(inode);
129135
return ERR_PTR(err);

‎fs/ceph/mds_client.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ static int __choose_mds(struct ceph_mds_client *mdsc,
670670
} else {
671671
/* dir + name */
672672
inode = dir;
673-
hash = ceph_dentry_hash(req->r_dentry);
673+
hash = ceph_dentry_hash(dir, req->r_dentry);
674674
is_hash = true;
675675
}
676676
}

‎fs/ceph/super.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ extern void ceph_dentry_lru_add(struct dentry *dn);
800800
extern void ceph_dentry_lru_touch(struct dentry *dn);
801801
extern void ceph_dentry_lru_del(struct dentry *dn);
802802
extern void ceph_invalidate_dentry_lease(struct dentry *dentry);
803-
extern unsigned ceph_dentry_hash(struct dentry *dn);
803+
extern unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn);
804804
extern struct inode *ceph_get_dentry_parent_inode(struct dentry *dentry);
805805

806806
/*

0 commit comments

Comments
 (0)