Skip to contentSkip to conversation

cramfs: generate unique inode number for better inode cache usage

PATCH In review [email protected] 3 participants 7 messages Dec 12, 2010 – Dec 14, 2010 Cramfs filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cramfs: generate unique inode number for better inode cache usage

From: Stefani Seibold <[email protected]>

This patch generates a unique inode numbers for any entries in the cram file system. For files which did not contain data's (device nodes, fifos and sockets) the offset of the directory entry inside + 1 will be used as the inode number.

The + 1 for the inode will it make possible to distinguish between a file which contains no data and files which has data, the later one has a inode value where the lower two bits are always 0.

It also reimplement the behavoir to set the size and the number of block to 0 for special file, which is the right value for devices, fifos and sockets.

As a little benefit it will be also more compatible which older mkcramfs, because it will never use the cramfs_inode->offset for creating a inode number for special files.

Please apply it.

Thanks,
Stefani

---
 fs/cramfs/inode.c |   99 +++++++++++++++++++++++++++++-----------------------
 1 files changed, 55 insertions(+), 44 deletions(-)
fs/cramfs/inode.c +55 -44
index 32fd5fe..78ab713 100644
@@ -36,54 +36,61 @@ static DEFINE_MUTEX(read_mutex);
 
 /* These two macros may change in future, to provide better st_ino
    semantics. */
-#define CRAMINO(x)	(((x)->offset && (x)->size)?(x)->offset<<2:1)
 #define OFFSET(x)	((x)->i_ino)
 
-static void setup_inode(struct inode *inode, struct cramfs_inode * cramfs_inode)
+static inline unsigned long cramino(struct cramfs_inode * cino,
+					unsigned int offset)
 {
-	static struct timespec zerotime;
-	inode->i_mode = cramfs_inode->mode;
-	inode->i_uid = cramfs_inode->uid;
-	inode->i_size = cramfs_inode->size;
-	inode->i_blocks = (cramfs_inode->size - 1) / 512 + 1;
-	inode->i_gid = cramfs_inode->gid;
-	/* Struct copy intentional */
-	inode->i_mtime = inode->i_atime = inode->i_ctime = zerotime;
-	/* inode->i_nlink is left 1 - arguably wrong for directories,
-	   but it's the best we can do without reading the directory
-	   contents.  1 yields the right result in GNU find, even
-	   without -noleaf option. */
-	if (S_ISREG(inode->i_mode)) {
-		inode->i_fop = &generic_ro_fops;
-		inode->i_data.a_ops = &cramfs_aops;
-	} else if (S_ISDIR(inode->i_mode)) {
-		inode->i_op = &cramfs_dir_inode_operations;
-		inode->i_fop = &cramfs_directory_operations;
-	} else if (S_ISLNK(inode->i_mode)) {
-		inode->i_op = &page_symlink_inode_operations;
-		inode->i_data.a_ops = &cramfs_aops;
-	} else {
-		init_special_inode(inode, inode->i_mode,
-			old_decode_dev(cramfs_inode->size));
-	}
+	if (!cino->offset)
+		return offset + 1;
+	if (!cino->size)
+		return offset + 1;
+	if (S_ISCHR(cino->mode))
+		return offset + 1;
+	if (S_ISBLK(cino->mode))
+		return offset + 1;
+	if (S_ISFIFO(cino->mode))
+		return offset + 1;
+	if (S_ISSOCK(cino->mode))
+		return offset + 1;
+
+	return cino->offset << 2;
 }
 
 static struct inode *get_cramfs_inode(struct super_block *sb,
-				struct cramfs_inode * cramfs_inode)
+	struct cramfs_inode * cramfs_inode, unsigned int offset)
 {
-	struct inode *inode;
-	if (CRAMINO(cramfs_inode) == 1) {
-		inode = new_inode(sb);
-		if (inode) {
-			inode->i_ino = 1;
-			setup_inode(inode, cramfs_inode);
-		}
-	} else {
-		inode = iget_locked(sb, CRAMINO(cramfs_inode));
-		if (inode && (inode->i_state & I_NEW)) {
-			setup_inode(inode, cramfs_inode);
-			unlock_new_inode(inode);
+	static struct timespec zerotime;
+	struct inode *inode = iget_locked(sb, cramino(cramfs_inode, offset));
+
+	if (inode && (inode->i_state & I_NEW)) {
+		inode->i_mode = cramfs_inode->mode;
+		inode->i_uid = cramfs_inode->uid;
+		inode->i_size = cramfs_inode->size;
+		inode->i_blocks = (cramfs_inode->size - 1) / 512 + 1;
+		inode->i_gid = cramfs_inode->gid;
+		/* Struct copy intentional */
+		inode->i_mtime = inode->i_atime = inode->i_ctime = zerotime;
+		/* inode->i_nlink is left 1 - arguably wrong for directories,
+		   but it's the best we can do without reading the directory
+		   contents.  1 yields the right result in GNU find, even
+		   without -noleaf option. */
+		if (S_ISREG(inode->i_mode)) {
+			inode->i_fop = &generic_ro_fops;
+			inode->i_data.a_ops = &cramfs_aops;
+		} else if (S_ISDIR(inode->i_mode)) {
+			inode->i_op = &cramfs_dir_inode_operations;
+			inode->i_fop = &cramfs_directory_operations;
+		} else if (S_ISLNK(inode->i_mode)) {
+			inode->i_op = &page_symlink_inode_operations;
+			inode->i_data.a_ops = &cramfs_aops;
+		} else {
+			inode->i_size = 0;
+			inode->i_blocks = 0;
+			init_special_inode(inode, inode->i_mode,
+				old_decode_dev(cramfs_inode->size));
 		}
+		unlock_new_inode(inode);
 	}
 	return inode;
 }
@@ -265,6 +272,9 @@ static int cramfs_fill_super(struct super_block *sb, void *data, int silent)
 		printk(KERN_ERR "cramfs: root is not a directory\n");
 		goto out;
 	}
+	/* correct strange, hard-coded permissions of mkcramfs */
+	super.root.mode |= (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
+
 	root_offset = super.root.offset << 2;
 	if (super.flags & CRAMFS_FLAG_FSID_VERSION_2) {
 		sbi->size=super.size;
@@ -289,7 +299,7 @@ static int cramfs_fill_super(struct super_block *sb, void *data, int silent)
 
 	/* Set it all up.. */
 	sb->s_op = &cramfs_ops;
-	root = get_cramfs_inode(sb, &super.root);
+	root = get_cramfs_inode(sb, &super.root, 0);
 	if (!root)
 		goto out;
 	sb->s_root = d_alloc_root(root);
@@ -365,7 +375,7 @@ static int cramfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
 		 */
 		namelen = de->namelen << 2;
 		memcpy(buf, name, namelen);
-		ino = CRAMINO(de);
+		ino = cramino(de, OFFSET(inode) + offset);
 		mode = de->mode;
 		mutex_unlock(&read_mutex);
 		nextoffset = offset + sizeof(*de) + namelen;
@@ -404,8 +414,9 @@ static struct dentry * cramfs_lookup(struct inode *dir, struct dentry *dentry, s
 		struct cramfs_inode *de;
 		char *name;
 		int namelen, retval;
+		int dir_off = OFFSET(dir) + offset;
 
-		de = cramfs_read(dir->i_sb, OFFSET(dir) + offset, sizeof(*de)+CRAMFS_MAXPATHLEN);
+		de = cramfs_read(dir->i_sb, dir_off, sizeof(*de)+CRAMFS_MAXPATHLEN);
 		name = (char *)(de+1);
 
 		/* Try to take advantage of sorted directories */
@@ -436,7 +447,7 @@ static struct dentry * cramfs_lookup(struct inode *dir, struct dentry *dentry, s
 		if (!retval) {
 			struct cramfs_inode entry = *de;
 			mutex_unlock(&read_mutex);
-			d_add(dentry, get_cramfs_inode(dir->i_sb, &entry));
+			d_add(dentry, get_cramfs_inode(dir->i_sb, &entry, dir_off));
 			return NULL;
 		}
 		/* else (retval < 0) */
Signature · 1 line
1.7.3.3
#1 lore raw
Quoted 9 lines · On Sun, 12 Dec 2010 11:48:42 +0100 [email protected] wrote:

This patch generates a unique inode numbers for any entries in the cram file system. For files which did not contain data's (device nodes, fifos and sockets) the offset of the directory entry inside + 1 will be used as the inode number.

The + 1 for the inode will it make possible to distinguish between a file which contains no data and files which has data, the later one has a inode value where the lower two bits are always 0.

Quoted 7 lines

It also reimplement the behavoir to set the size and the number of block to 0 for special file, which is the right value for devices, fifos and sockets.

As a little benefit it will be also more compatible which older mkcramfs, because it will never use the cramfs_inode->offset for creating a inode number for special files.

Did you look at using iunique() to generate cramfs inode numbers?

#2 lore raw

Am Dienstag, den 14.12.2010, 12:51 -0800 schrieb Andrew Morton:

Quoted 16 lines
Quoted 13 lines · On Sun, 12 Dec 2010 11:48:42 +0100 [email protected] wrote:

This patch generates a unique inode numbers for any entries in the cram file system. For files which did not contain data's (device nodes, fifos and sockets) the offset of the directory entry inside + 1 will be used as the inode number.

The + 1 for the inode will it make possible to distinguish between a file which contains no data and files which has data, the later one has a inode value where the lower two bits are always 0.

It also reimplement the behavoir to set the size and the number of block to 0 for special file, which is the right value for devices, fifos and sockets.

Quoted 5 lines
Quoted 3 lines

As a little benefit it will be also more compatible which older mkcramfs, because it will never use the cramfs_inode->offset for creating a inode number for special files.

Did you look at using iunique() to generate cramfs inode numbers?

iunique() will create random inode numbers, depending on the order of the accessed files. My solution returns always the same inode numbers for the files. It is also faster, because there is no need to lookup inode cache for the given super block. And at last the generating of the inode number is very simple.

#3 lore raw
Quoted 17 lines · On Tue, 14 Dec 2010 22:02:30 +0100 Stefani Seibold <[email protected]> wrote:

Am Dienstag, den 14.12.2010, 12:51 -0800 schrieb Andrew Morton:

Quoted 16 lines
Quoted 13 lines · On Sun, 12 Dec 2010 11:48:42 +0100 [email protected] wrote:

This patch generates a unique inode numbers for any entries in the cram file system. For files which did not contain data's (device nodes, fifos and sockets) the offset of the directory entry inside + 1 will be used as the inode number.

The + 1 for the inode will it make possible to distinguish between a file which contains no data and files which has data, the later one has a inode value where the lower two bits are always 0.

It also reimplement the behavoir to set the size and the number of block to 0 for special file, which is the right value for devices, fifos and sockets.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

As a little benefit it will be also more compatible which older mkcramfs, because it will never use the cramfs_inode->offset for creating a inode number for special files.

Did you look at using iunique() to generate cramfs inode numbers?

iunique() will create random inode numbers, depending on the order of the accessed files.

It generates non-unique inode numbers and uses Weird Nonstandard Private Stuff in a place where we could use standard kernel facilities.

#4 lore raw

Am Dienstag, den 14.12.2010, 13:12 -0800 schrieb Andrew Morton:

Quoted 24 lines
Quoted 21 lines · On Tue, 14 Dec 2010 22:02:30 +0100 Stefani Seibold <[email protected]> wrote:

Am Dienstag, den 14.12.2010, 12:51 -0800 schrieb Andrew Morton:

Quoted 20 lines
Quoted 16 lines · On Sun, 12 Dec 2010 11:48:42 +0100 [email protected] wrote:

This patch generates a unique inode numbers for any entries in the cram file system. For files which did not contain data's (device nodes, fifos and sockets) the offset of the directory entry inside + 1 will be used as the inode number.

The + 1 for the inode will it make possible to distinguish between a file which contains no data and files which has data, the later one has a inode value where the lower two bits are always 0.

It also reimplement the behavoir to set the size and the number of block to 0 for special file, which is the right value for devices, fifos and sockets.

As a little benefit it will be also more compatible which older mkcramfs, because it will never use the cramfs_inode->offset for creating a inode number for special files.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Did you look at using iunique() to generate cramfs inode numbers?

iunique() will create random inode numbers, depending on the order of the accessed files.

It generates non-unique inode numbers and uses Weird Nonstandard Private Stuff in a place where we could use standard kernel facilities.

Disagree, it will create uniqe inode. And if the "standard" kernel facilities will not serve right, than it is necessary to do in a better way. iunique() is not the right facility.

Stefani

#5 lore raw
In reply to Andrew Morton
Quoted 2 lines · Andrew Morton

Did you look at using iunique() to generate cramfs inode numbers?

That breaks the cramfs "hardlinking" (which is just files that have the same data pointer), and now a hardlinked file wouldn't have the same inode number any more.

Of course, I'm not sure the hardlinking really matters. cramfs hardlinks aren't really traditional hardlinks anyway - since the permissions etc are in the directory entry, you can have the data hardlinked without having the same permissions, so it's not a "real" hardlink even if the inode number were to be the same.

But this patch seems to roughly approximate the old pseudo-hardlink behavior. It used to be that all non-data files showed up with the same inode number, now they have separate inode numbers.

That said, I hate how it moves that "setup_inode" helper function inline and then does the "if it's a character device" kinds of tests twice. Once for the inode number logic, and once for the inode operations structure assignment.

So I think the approach is fine, but I think the implementation is pretty ugly.

Linus

#6 lore raw

Am Dienstag, den 14.12.2010, 13:08 -0800 schrieb Linus Torvalds:

Quoted 9 lines
Quoted 2 lines · Andrew Morton

Did you look at using iunique() to generate cramfs inode numbers?

That breaks the cramfs "hardlinking" (which is just files that have the same data pointer), and now a hardlinked file wouldn't have the same inode number any more.

Quoted 6 lines

Of course, I'm not sure the hardlinking really matters. cramfs hardlinks aren't really traditional hardlinks anyway - since the permissions etc are in the directory entry, you can have the data hardlinked without having the same permissions, so it's not a "real" hardlink even if the inode number were to be the same.

In my opinion hardlinks doesn't matter, because cramfs has no real hardlinks.

Quoted 11 lines

But this patch seems to roughly approximate the old pseudo-hardlink behavior. It used to be that all non-data files showed up with the same inode number, now they have separate inode numbers.

That said, I hate how it moves that "setup_inode" helper function inline and then does the "if it's a character device" kinds of tests twice. Once for the inode number logic, and once for the inode operations structure assignment.

So I think the approach is fine, but I think the implementation is pretty ugly.

Okay, i will see if i find a better solution. The problem is that the inode number generation CRAMINO will be called from two different functions, get_cramfs_inode() and cramfs_readdir(). So it is much more readable to have one function which creates the inode than an two optimized implementations.

Stefani