f725e3
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f725e3
From: Jan Kara <jack@suse.cz>
f725e3
Date: Mon, 1 Jun 2015 14:28:45 +0200
f725e3
Subject: [PATCH] xfs: Add helpers for inode size
f725e3
f725e3
Add helpers to return size of XFS inode on disk and when loaded in
f725e3
memory.
f725e3
f725e3
Signed-off-by: Jan Kara <jack@suse.cz>
f725e3
---
f725e3
 grub-core/fs/xfs.c | 35 ++++++++++++++++++++++++-----------
f725e3
 1 file changed, 24 insertions(+), 11 deletions(-)
f725e3
f725e3
diff --git a/grub-core/fs/xfs.c b/grub-core/fs/xfs.c
f725e3
index 0d704e9a4d4..26d8147a92e 100644
f725e3
--- a/grub-core/fs/xfs.c
f725e3
+++ b/grub-core/fs/xfs.c
f725e3
@@ -255,6 +255,24 @@ grub_xfs_inode_offset (struct grub_xfs_data *data,
f725e3
 	  data->sblock.log2_inode);
f725e3
 }
f725e3
 
f725e3
+static inline grub_size_t
f725e3
+grub_xfs_inode_size(struct grub_xfs_data *data)
f725e3
+{
f725e3
+  return 1 << data->sblock.log2_inode;
f725e3
+}
f725e3
+
f725e3
+/*
f725e3
+ * Returns size occupied by XFS inode stored in memory - we store struct
f725e3
+ * grub_fshelp_node there but on disk inode size may be actually larger than
f725e3
+ * struct grub_xfs_inode so we need to account for that so that we can read
f725e3
+ * from disk directly into in-memory structure.
f725e3
+ */
f725e3
+static inline grub_size_t
f725e3
+grub_xfs_fshelp_size(struct grub_xfs_data *data)
f725e3
+{
f725e3
+  return sizeof (struct grub_fshelp_node) - sizeof (struct grub_xfs_inode)
f725e3
+	       + grub_xfs_inode_size(data);
f725e3
+}
f725e3
 
f725e3
 static grub_err_t
f725e3
 grub_xfs_read_inode (struct grub_xfs_data *data, grub_uint64_t ino,
f725e3
@@ -264,8 +282,8 @@ grub_xfs_read_inode (struct grub_xfs_data *data, grub_uint64_t ino,
f725e3
   int offset = grub_xfs_inode_offset (data, ino);
f725e3
 
f725e3
   /* Read the inode.  */
f725e3
-  if (grub_disk_read (data->disk, block, offset,
f725e3
-		      1 << data->sblock.log2_inode, inode))
f725e3
+  if (grub_disk_read (data->disk, block, offset, grub_xfs_inode_size(data),
f725e3
+		      inode))
f725e3
     return grub_errno;
f725e3
 
f725e3
   if (grub_strncmp ((char *) inode->magic, "IN", 2))
f725e3
@@ -297,7 +315,7 @@ grub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
f725e3
       if (node->inode.fork_offset)
f725e3
 	recoffset = (node->inode.fork_offset - 1) / 2;
f725e3
       else
f725e3
-	recoffset = ((1 << node->data->sblock.log2_inode)
f725e3
+	recoffset = (grub_xfs_inode_size(node->data)
f725e3
 		     - ((char *) &node->inode.data.btree.keys
f725e3
 			- (char *) &node->inode))
f725e3
 	  / (2 * sizeof (grub_uint64_t));
f725e3
@@ -456,9 +474,7 @@ static int iterate_dir_call_hook (grub_uint64_t ino, const char *filename,
f725e3
   struct grub_fshelp_node *fdiro;
f725e3
   grub_err_t err;
f725e3
 
f725e3
-  fdiro = grub_malloc (sizeof (struct grub_fshelp_node)
f725e3
-		       - sizeof (struct grub_xfs_inode)
f725e3
-		       + (1 << ctx->diro->data->sblock.log2_inode) + 1);
f725e3
+  fdiro = grub_malloc (grub_xfs_fshelp_size(ctx->diro->data) + 1);
f725e3
   if (!fdiro)
f725e3
     {
f725e3
       grub_print_error ();
f725e3
@@ -682,7 +698,7 @@ grub_xfs_mount (grub_disk_t disk)
f725e3
   data = grub_realloc (data,
f725e3
 		       sizeof (struct grub_xfs_data)
f725e3
 		       - sizeof (struct grub_xfs_inode)
f725e3
-		       + (1 << data->sblock.log2_inode) + 1);
f725e3
+		       + grub_xfs_inode_size(data) + 1);
f725e3
 
f725e3
   if (! data)
f725e3
     goto fail;
f725e3
@@ -797,10 +813,7 @@ grub_xfs_open (struct grub_file *file, const char *name)
f725e3
 
f725e3
   if (fdiro != &data->diropen)
f725e3
     {
f725e3
-      grub_memcpy (&data->diropen, fdiro,
f725e3
-		   sizeof (struct grub_fshelp_node)
f725e3
-		   - sizeof (struct grub_xfs_inode)
f725e3
-		   + (1 << data->sblock.log2_inode));
f725e3
+      grub_memcpy (&data->diropen, fdiro, grub_xfs_fshelp_size(data));
f725e3
       grub_free (fdiro);
f725e3
     }
f725e3