ceph: avoid d_parent in ceph_dentry_hash; fix ceph_encode_fh() hashing bug
authorSage Weil <sage@newdream.net>
Tue, 26 Jul 2011 18:30:55 +0000 (11:30 -0700)
committerSage Weil <sage@newdream.net>
Tue, 26 Jul 2011 18:30:55 +0000 (11:30 -0700)
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>
fs/ceph/dir.c
fs/ceph/export.c
fs/ceph/mds_client.c
fs/ceph/super.h

index 31d27f8f8261c6c1e51153fc52f8ed1e44a27dff..33a19df7228885b8bf59a26c1772001cb6e8c20f 100644 (file)
@@ -1244,9 +1244,8 @@ void ceph_dentry_lru_del(struct dentry *dn)
  * Return name hash for a given dentry.  This is dependent on
  * the parent directory's hash function.
  */
-unsigned ceph_dentry_hash(struct dentry *dn)
+unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
 {
-       struct inode *dir = dn->d_parent->d_inode;
        struct ceph_inode_info *dci = ceph_inode(dir);
 
        switch (dci->i_dir_layout.dl_dir_hash) {
index f67b687550dea4cd00e27554650ce892845d4d90..9fbcdecaaccdc1ef2edfa9a8578b60c6a9b64118 100644 (file)
@@ -46,7 +46,7 @@ static int ceph_encode_fh(struct dentry *dentry, u32 *rawfh, int *max_len,
        int type;
        struct ceph_nfs_fh *fh = (void *)rawfh;
        struct ceph_nfs_confh *cfh = (void *)rawfh;
-       struct dentry *parent = dentry->d_parent;
+       struct dentry *parent;
        struct inode *inode = dentry->d_inode;
        int connected_handle_length = sizeof(*cfh)/4;
        int handle_length = sizeof(*fh)/4;
@@ -55,26 +55,33 @@ static int ceph_encode_fh(struct dentry *dentry, u32 *rawfh, int *max_len,
        if (ceph_snap(inode) != CEPH_NOSNAP)
                return -EINVAL;
 
+       spin_lock(&dentry->d_lock);
+       parent = dget(dentry->d_parent);
+       spin_unlock(&dentry->d_lock);
+
        if (*max_len >= connected_handle_length) {
                dout("encode_fh %p connectable\n", dentry);
                cfh->ino = ceph_ino(dentry->d_inode);
                cfh->parent_ino = ceph_ino(parent->d_inode);
-               cfh->parent_name_hash = ceph_dentry_hash(parent);
+               cfh->parent_name_hash = ceph_dentry_hash(parent->d_inode,
+                                                        dentry);
                *max_len = connected_handle_length;
                type = 2;
        } else if (*max_len >= handle_length) {
                if (connectable) {
                        *max_len = connected_handle_length;
-                       return 255;
+                       type = 255;
+               } else {
+                       dout("encode_fh %p\n", dentry);
+                       fh->ino = ceph_ino(dentry->d_inode);
+                       *max_len = handle_length;
+                       type = 1;
                }
-               dout("encode_fh %p\n", dentry);
-               fh->ino = ceph_ino(dentry->d_inode);
-               *max_len = handle_length;
-               type = 1;
        } else {
                *max_len = handle_length;
-               return 255;
+               type = 255;
        }
+       dput(parent);
        return type;
 }
 
@@ -123,7 +130,6 @@ static struct dentry *__fh_to_dentry(struct super_block *sb,
                return dentry;
        }
        err = ceph_init_dentry(dentry);
-
        if (err < 0) {
                iput(inode);
                return ERR_PTR(err);
index 3b1e743b8c8d288b1780f32ca8eeafe9b4053ec3..8a09cd5a659efa2e1c045ef2d3c9d6e86c10eb42 100644 (file)
@@ -670,7 +670,7 @@ static int __choose_mds(struct ceph_mds_client *mdsc,
                } else {
                        /* dir + name */
                        inode = dir;
-                       hash = ceph_dentry_hash(req->r_dentry);
+                       hash = ceph_dentry_hash(dir, req->r_dentry);
                        is_hash = true;
                }
        }
index c1eb9a014b741a00641e0268de3d3ec566b90596..35dc9656e499065bb0ac753e4d03533aad7c4701 100644 (file)
@@ -800,7 +800,7 @@ extern void ceph_dentry_lru_add(struct dentry *dn);
 extern void ceph_dentry_lru_touch(struct dentry *dn);
 extern void ceph_dentry_lru_del(struct dentry *dn);
 extern void ceph_invalidate_dentry_lease(struct dentry *dentry);
-extern unsigned ceph_dentry_hash(struct dentry *dn);
+extern unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn);
 extern struct inode *ceph_get_dentry_parent_inode(struct dentry *dentry);
 
 /*