staging: erofs: harden inode lookup for 32-bit platforms
authorGao Xiang <gaoxiang25@huawei.com>
Tue, 9 Oct 2018 14:07:13 +0000 (22:07 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 10 Oct 2018 11:15:23 +0000 (13:15 +0200)
This patch introduces inode hash function, test and set callbacks,
and iget5_locked to find the right inode for 32-bit platforms.

Reviewed-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Gao Xiang <gaoxiang25@huawei.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/staging/erofs/inode.c
drivers/staging/erofs/internal.h

index da8693a7c3d342c0378704ff7a0f355eea99511f..04c61a9d7b76692c3d63ac12822ca0ea78e48707 100644 (file)
@@ -232,10 +232,45 @@ out_unlock:
        return err;
 }
 
+/*
+ * erofs nid is 64bits, but i_ino is 'unsigned long', therefore
+ * we should do more for 32-bit platform to find the right inode.
+ */
+#if BITS_PER_LONG == 32
+static int erofs_ilookup_test_actor(struct inode *inode, void *opaque)
+{
+       const erofs_nid_t nid = *(erofs_nid_t *)opaque;
+
+       return EROFS_V(inode)->nid == nid;
+}
+
+static int erofs_iget_set_actor(struct inode *inode, void *opaque)
+{
+       const erofs_nid_t nid = *(erofs_nid_t *)opaque;
+
+       inode->i_ino = erofs_inode_hash(nid);
+       return 0;
+}
+#endif
+
+static inline struct inode *erofs_iget_locked(struct super_block *sb,
+                                             erofs_nid_t nid)
+{
+       const unsigned long hashval = erofs_inode_hash(nid);
+
+#if BITS_PER_LONG >= 64
+       /* it is safe to use iget_locked for >= 64-bit platform */
+       return iget_locked(sb, hashval);
+#else
+       return iget5_locked(sb, hashval, erofs_ilookup_test_actor,
+               erofs_iget_set_actor, &nid);
+#endif
+}
+
 struct inode *erofs_iget(struct super_block *sb,
        erofs_nid_t nid, bool isdir)
 {
-       struct inode *inode = iget_locked(sb, nid);
+       struct inode *inode = erofs_iget_locked(sb, nid);
 
        if (unlikely(inode == NULL))
                return ERR_PTR(-ENOMEM);
index 5096b27bcf0d45a2e37f7290452dbc2d20cfac9a..57575c7f56355424a88355297a5d2ed566d1c59f 100644 (file)
@@ -517,6 +517,15 @@ erofs_get_inline_page(struct inode *inode,
 }
 
 /* inode.c */
+static inline unsigned long erofs_inode_hash(erofs_nid_t nid)
+{
+#if BITS_PER_LONG == 32
+       return (nid >> 32) ^ (nid & 0xffffffff);
+#else
+       return nid;
+#endif
+}
+
 extern struct inode *erofs_iget(struct super_block *sb,
        erofs_nid_t nid, bool dir);