Skip to contentSkip to conversation

eCryptFS: Use container_of() rather than i_private

PATCH New David Howells 1 participant 1 message Sep 7, 2006 Ecrypt file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eCryptFS: Use container_of() rather than i_private

Use containter_of() rather than i_private to gain access to the eCryptFS private data from the inode as this will result in the assembly code using subtraction or addition to get the address rather than using a LOAD instruction to read i_private. This should result in slightly faster code and possibly the use of one fewer registers in some functions.

Because of this, ecryptfs_set_inode_private() is no longer necessary, and the same goes for any checks that the result of ecryptfs_get_inode_private() is non-NULL (which can be assumed if inode is non-NULL).

Also move the embedded vfs_inode struct to the beginning of ecryptfs_inode_info so that the VFS inode address and the private data address are coincident. This permits the offset to be optimised away.

It is possible that moving vfs_node before wii_inode is not the best thing to do, but if the address of wii_inode is always derived by offset from inode, then it should make no difference as register+offset addressing modes will always have to be used, one way or another.

Since because i_private is made available by the first part of the patch, wii_inode could be dispensed with entirely and i_private used instead, thus reducing the amount of memory used.

---
 fs/ecryptfs/ecryptfs_kernel.h |   16 ++++------------
 fs/ecryptfs/inode.c           |    1 -
 fs/ecryptfs/super.c           |    7 -------
 3 files changed, 4 insertions(+), 20 deletions(-)
fs/ecryptfs/ecryptfs_kernel.h +4 -12
diff -uNrp linux-2.6.18-rc5-mm1-ecryptfs/fs/ecryptfs/ecryptfs_kernel.h linux-2.6.18-rc5-mm1-ecryptfs-ipriv/fs/ecryptfs/ecryptfs_kernel.h
@@ -211,8 +211,8 @@ struct ecryptfs_crypt_stat {
 
 /* inode private data. */
 struct ecryptfs_inode_info {
-	struct inode *wii_inode;
 	struct inode vfs_inode;
+	struct inode *wii_inode;
 	struct ecryptfs_crypt_stat crypt_stat;
 };
 
@@ -289,26 +289,18 @@ ecryptfs_set_file_lower(struct file *fil
 static inline struct ecryptfs_inode_info *
 ecryptfs_inode_to_private(struct inode *inode)
 {
-	return (struct ecryptfs_inode_info *)inode->i_private;
-}
-
-static inline void
-ecryptfs_set_inode_private(struct inode *inode,
-			   struct ecryptfs_inode_info *inode_info)
-{
-	inode->i_private = inode_info;
+	return container_of(inode, struct ecryptfs_inode_info, vfs_inode);
 }
 
 static inline struct inode *ecryptfs_inode_to_lower(struct inode *inode)
 {
-	return ((struct ecryptfs_inode_info *)inode->i_private)->wii_inode;
+	return ecryptfs_inode_to_private(inode)->wii_inode;
 }
 
 static inline void
 ecryptfs_set_inode_lower(struct inode *inode, struct inode *lower_inode)
 {
-	((struct ecryptfs_inode_info *)inode->i_private)->wii_inode =
-		lower_inode;
+	ecryptfs_inode_to_private(inode)->wii_inode = lower_inode;
 }
 
 static inline struct ecryptfs_sb_info *
fs/ecryptfs/inode.c +0 -1
diff -uNrp linux-2.6.18-rc5-mm1-ecryptfs/fs/ecryptfs/inode.c linux-2.6.18-rc5-mm1-ecryptfs-ipriv/fs/ecryptfs/inode.c
@@ -1026,7 +1026,6 @@ out:
 
 int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode)
 {
-	BUG_ON(!ecryptfs_inode_to_private(inode));
 	if ((ecryptfs_inode_to_lower(inode)
 	     == (struct inode *)candidate_lower_inode))
 		return 1;
fs/ecryptfs/super.c +0 -7
diff -uNrp linux-2.6.18-rc5-mm1-ecryptfs/fs/ecryptfs/super.c linux-2.6.18-rc5-mm1-ecryptfs-ipriv/fs/ecryptfs/super.c
@@ -85,13 +85,6 @@ static void ecryptfs_destroy_inode(struc
  */
 void ecryptfs_init_inode(struct inode *inode, struct inode *lower_inode)
 {
-	/* This is where we setup the self-reference in the
-	 * vfs_inode's i_private. That way we don't have to walk the
-	 * list again. */
-	ecryptfs_set_inode_private(inode,
-				   container_of(inode,
-						struct ecryptfs_inode_info,
-						vfs_inode));
 	ecryptfs_set_inode_lower(inode, lower_inode);
 	inode->i_ino = lower_inode->i_ino;
 	inode->i_version++;