One more thing: inode can be reused, and I don't see where you handle it. E.g.: shmem_fh_to_dentry / shmem_match uses inode generation to detect it.

On 7/27/20 12:43 PM, Pavel Tikhomirov wrote:


On 7/24/20 6:50 PM, Andrey Zhadchenko wrote:
criu uses fhandle from fdinfo to dump inotify objects. cgroup super block has
no export operations, but .encode_fh and .fh_to_dentry are needed for
inotify_fdinfo function and open_by_handle_at syscall in order to correctly
open files located on cgroupfs by fhandle.
Add hash table as a storage for inodes with exported fhandle.

https://jira.sw.ru/browse/PSBM-105889
Signed-off-by: Andrey Zhadchenko <andrey.zhadche...@virtuozzo.com>
---
  kernel/cgroup.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
  1 file changed, 111 insertions(+), 1 deletion(-)

diff --git a/kernel/cgroup.c b/kernel/cgroup.c
index 9fdba79..a459c56 100644
--- a/kernel/cgroup.c
+++ b/kernel/cgroup.c
@@ -62,6 +62,7 @@
  #include <linux/kthread.h>
  #include <linux/ve.h>
  #include <linux/stacktrace.h>
+#include <linux/exportfs.h>
  #include <linux/atomic.h>
@@ -1390,9 +1391,117 @@ out:
  }
  #endif
+/*
+ * hashtable for inodes that have exported fhandles.
+ * When we export fhandle, we add it's inode into
+ * hashtable so we can find it fast
+ */
+
+#define CGROUP_INODE_HASH_BITS 10
+static DEFINE_HASHTABLE(cgroup_inode_table, CGROUP_INODE_HASH_BITS);
+static DEFINE_SPINLOCK(cgroup_inode_table_lock);
+
+struct cg_inode_hitem {
+    struct inode *inode;
+    struct hlist_node hlist;
+};
+
+static inline unsigned long cgroup_inode_get_hash(unsigned int i_ino)
+{
+    return hash_32(i_ino, CGROUP_INODE_HASH_BITS);
+}
+
+static struct cg_inode_hitem *cgroup_inode_hash_find(unsigned int i_ino)
+{
+    struct cg_inode_hitem *i;
+    struct hlist_head *head = cgroup_inode_table
+        + cgroup_inode_get_hash(i_ino);
+    struct cg_inode_hitem *found = 0;

NULL would look better than 0 for pointer.

+
+    spin_lock(&cgroup_inode_table_lock);
+    hlist_for_each_entry(i, head, hlist) {
+        if (i->inode->i_ino == i_ino) {
+            found = i;
+            break;
+        }
+    }
+    spin_unlock(&cgroup_inode_table_lock);

The cg_inode_hitem can be freed from other thread just after you release the lock. Inode can be evicted and freed just after you release the lock. We should iget a reference on inode before releasing lock and return inode instead of cg_inode_hitem.

+
+    return found;
+}
+
+static struct dentry *cgroup_fh_to_dentry(struct super_block *sb,
+        struct fid *fid, int fh_len, int fh_type)
+{
+    struct cg_inode_hitem *item;
+    struct dentry *dentry = ERR_PTR(-ENOENT);
+
+    if (fh_len < 1)
+        return NULL;
+
+    item = cgroup_inode_hash_find(fid->raw[0]);
+    if (item)
+        dentry = d_find_alias(item->inode);

When you've got dentry you can put the inode reference back. See how it's done in shmem_fh_to_dentry.

+
+    return dentry;
+}
+
+static int cgroup_encode_fh(struct inode *inode, __u32 *fh, int *len,
+                struct inode *parent)
+{
+    struct hlist_head *head = cgroup_inode_table
+        + cgroup_inode_get_hash(inode->i_ino);
+    struct cg_inode_hitem *item;
+
+    if (*len < 1) {
+        *len = 1;
+        return FILEID_INVALID;
+    }
+
+    if (cgroup_inode_hash_find(inode->i_ino) == 0) {

Would look better:
     if (!cgroup_inode_hash_find(inode->i_ino)) {

+        item = kmalloc(sizeof(struct cg_inode_hitem),
+            GFP_KERNEL);
+        /*
+         * encode_fh is expected to return 255 (FILEID_INVALID)
+         * in case of failure. We can't return ENOMEM, so
+         * return FILEID_INVALID at least
+         */
+        if (!item)
+            return FILEID_INVALID;
+        item->inode = inode;
+
+        spin_lock(&cgroup_inode_table_lock);
+        hlist_add_head(&item->hlist, head);
+        spin_unlock(&cgroup_inode_table_lock);
+    }
+
+    fh[0] = inode->i_ino;
+    *len = 1;
+    return 1;
+}
+
+static const struct export_operations cgroup_export_ops = {
+    .encode_fh      = cgroup_encode_fh,
+    .fh_to_dentry    = cgroup_fh_to_dentry,
+};
+
+static int cgroup_delete_inode(struct inode *inode)
+{
+    struct cg_inode_hitem *item = cgroup_inode_hash_find(inode->i_ino);

I don't like this. Lets only merge variable initialization and declaration if You don't expect any fail. And put empty line between declaration and everything else.

+    if (item) {
+        spin_lock(&cgroup_inode_table_lock);
+        hlist_del(&item->hlist);
+        spin_unlock(&cgroup_inode_table_lock);
+
+        kfree(item);
+    }
+
+    return generic_delete_inode(inode);
+}
+
  static const struct super_operations cgroup_ops = {
      .statfs = simple_statfs,
-    .drop_inode = generic_delete_inode,
+    .drop_inode = cgroup_delete_inode,
      .show_options = cgroup_show_options,
  #ifdef CONFIG_VE
      .show_path = cgroup_show_path,
@@ -1539,6 +1648,7 @@ static int cgroup_set_super(struct super_block *sb, void *data)
      sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
      sb->s_magic = CGROUP_SUPER_MAGIC;
      sb->s_op = &cgroup_ops;
+    sb->s_export_op = &cgroup_export_ops;
      return 0;
  }



--
Best regards, Tikhomirov Pavel
Software Developer, Virtuozzo.
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

Reply via email to