Skip to contentSkip to conversation

Unbork fs.h, 1 of 4

RFC In review Daniel Phillips 2 participants 5 messages Jan 6, 2002 – Jan 7, 2002

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

RFC 1/1 Unbork fs.h, 1 of 4

This is the first of a set of 4 patches, totalling 2,142 lines, the end result of which is the removal of four lines from include/linux/fs.h:

291: #include <linux/ext2_fs_i.h>
670: #include <linux/ext2_fs_sb.h>
481: struct ext2_inode_info ext2_i; (union member)
727: struct ext2_sb_info ext2_sb; (union member)

With these four lines gone, several things happen:

- Ext2 in-memory inodes and super_blocks get somewhat smaller
- The kernel build speeds up a little (fewer header files compiled)
- Header dependencies get simpler and more maintainable
- The two excised header files can be merged with ext2_fs.h

How did those four ext2-specific lines come to be part of fs.h in the first place? They're there so that the compiler can compute a size for the inode and super_block structs, and compute offsets to the fields of the unions. Please see my original post, "[RFC] [WIP] Unbork fs.h", for a more extensive discussion of why this is bad. I'll summarize the main points here:

- The size of the struct is governed by the largest member of the union, and since inodes at least are very numerous, a lot of memory is wasted.

- Touching any filesystem's header forces recompilations of any file that includes fs.h.

- All the symbols of every filesystem's header end up in the same namespace, and this namespace is imported by every file that includes fs.h, of which there are many.

- Some filesystem-specific header files include other headers and some of those header files include other headers and so on, so that it is difficult to know which headers are actually being included. Even worse, the includes change over time.

- The complex header includes force the use of various tricks to allow header files to be ignored if they are inadvertently included more than once. This is cruft that should not be needed.

In formulating a strategy to correct these problems, I set out the following goals:

1) Efficiency: no slowdown in any operation

2) Memory usage: no increase in the size of any object

3) Forward compatibility: requires no changes to existing filesystems unless it is desired to take advantage of the new functionality.

4) Incremental approach: no requirement to update all filesystems at the same time.

5) Appropriate Factorization: the vfs supplies as much generic functionality as it can, so that code does not have to be replicated throughout individual filesystems, but at the same time, does not require new elaborate machinery to do so.

6) Robust approach, not prone to subtle execution errors, and likely to survive continued evolution of the code base.

7) Simple approach, easy to understand.

After considering how to tackle this problem for some time (more than a year) I settled on the following approach:

a) Implement vfs support for variable per-filesystem inode and super_block sizes, and for inodes, optional per-filesystem slab caches.

b) Abstract all references to the current inode and super block unions through (type safe) inline functions, so that the access method can be defined at one central point. The inline function uses an explicit cast, eliminating the need for the filesystem-private part of the inode to be embedded in the declaration of the system inode.

c) Apply the above abstraction throughout the Ext2 filesystem, that is, change all references to the structure unions to call the inline functions for inodes and super blocks respectively.

d) At this point, fs.h no longer needs to include ext2's private structure declarations, so these can be removed, as can the ext2-specific members of the unions.

At step (d) the overarching objective has been achieved: individual filesystems will include the system headers and not the other way around.

In my implementation, the filesystem-private structures immediately follow the generic parts of the inode and super_block structs, as they have done traditionally. The thing that changes is, the structures do not have to be declared together. Instead the addressing calculation is performed by the inline access function, and consists simply of adding the size of the generic part of an inode to the input address. Because the function is inline, in many cases this can be optimized by the compiler according to context, which achieves goal (1) above: the new access code is exactly as fast as the old code.

Since inodes and super_blocks become variable sized objects, the vfs needs to have some means of knowing their sizes. My solution here is to encode the size of the variable part of the inode and super_block in the structure that describes general parameters of filesystem, the file_system_type struct. This requires two new integer fields per filesystem type - it is hard to imagine a more economical way to express this.

At filesystem registration time, an inode slab cache is created using the filesystem type's specified size. No such cache is created for super_blocks since they are relatively few in number. Instead, super blocks are kmalloc'ed according to their specified size. (Factoid: kmalloc uses a cascade of binary-sized slab caches internally.) In either case, the minimum possible memory is used to cache the objects - the situation where all inodes in the system had to be the same size as the largest inode is eliminated. This satisfies goal (2) above.

If no size is specified for either the inode or super block private areas then the objects are allocated in the traditional way. This provides complete forward compatibility with existing filesystems and permits an incremental approach to modifying all the filesystems currently in the Linux tree, satisfying goals (3) and (4) above. As well, this allows filesystem maintainers to implement the changes needed to use the new approach at a convenient time and a convenient pace.

As for goals (4), (5) and (6), the extent to which they have been achieved is for you to judge. My opinion is that they have been satisfied.

Where this is going
-------------------
At this point, I will stop work on these patches (except for maintainance) until a concensus is reached on the approach. Assuming that consensus is positive, and assuming Linus blesses it, I will then forward-port the patch set to the 2.5 series. Once integrated, the common vfs mechanism would be available for use by all other filesystems in the tree, which at present number around 30. That's a lot, but fortunately, for each filesystem, only 4 lines need to be changed (deleted) outside the scope of the filesystem proper. In fact, a converted filesystem will function correctly with or without these global changes, further simplifying maintainance issues.

After all filesystem-specific includes have been removed from fs.h, it is possible to make further improvements: the unions themselves can finally be removed, which will require a one-line change to every filesystem and the 'optional' nature of the filesystem size fields can be changed to 'mandatory', slightly improving the efficiency of and simplifying the vfs code. Once in the 2.5 tree, the code will no longer have to account for the possibility that an inode has a null superblock, again slightly simplifying and streamlining it.

It is for consideration whether the generic inode and super_block pointer fields should be kept in the common inode or moved into the scope of the specific filesystems. This is a close call. If the generic pointers do move out of the common structures then some special case allocation for inodes that use only the generic field should be done, since all will have the same size. In any event, switching from one approach or the other will, at that point, have only a small impact on the code base, as opposed to the massive impact of the sum total of the changes to all the filesystems that will have been carried out by then.

The approach I used to modify Ext2 would work well for all the filesystems in the tree. Most of the work consists of a simple global edit along the lines of:

s/u.ext2_i./ext2_i(inode)->/

After doing this for both inode and super block fields, there will certainly be a few odds and ends to clean up, but the total time required for this change is likely to be measured in minutes. Besides that, a pair of inline functions along the lines of:

    static inline struct ext2_sb_info *ext2_s(struct super_block *sb)
    {
           return (struct ext2_sb_info *) &(sb->u);
    }

is required. Note: after all unions have been removed entirely from all the filesystems, the expression &(sb->u) above will be replaced by the more appealing expression (sb + 1) and the union will be removed from the common structures themselves.

Applying and Using the patch
----------------------------
I am running with this patch set applied now. While I will provide no guarantees, it does seems to be working for me. I have not subjected it to stress testing at this point, and I doubt I will have time for it. I hope that someone else will be able to do this and report results. (Hopefully not oopses!)

Note that there is a section of code in fs/super.c bracketed in an #ifdef 1, with the comment 'Manfred thinks...' that the code isn't needed at all, and that the system will run better without it. Since I did not want to be testing that conjecture and the stability of the patch proper at the same time, I conservatively left this code in, as well as my generalization of it. Anybody who is interested in seeing if the system actually does run better without it is invited to try (I have logged a few hours both ways but without benchmarking or stress testing).

If you run the patch, the first thing you may want to do is 'cat /proc/slabinfo'. You will see a slab cache named 'ext2', which holds ext2's inodes. Note the difference in size between that cache and the generic inode_cache, which on my system amounts to 64 bytes (improvement!). This is a good way to confirm that the patch set is actually applied and running.

The Patches
-----------
There are four patches in this set:

1) VFS: changes to support variable sized inodes and super blocks
2) Ext2: remove all but one references to the inode union
3) Ext2: remove all but one references to the super block union
4) Ext2: enable the new functionality and remove the fs.h dependencies

Each patch leaves you with a buildable, working system, but none of the new functionality is enabled until the fourth patch is applied. Patch (3) requires patch (2), and patch (4) requires all the other patches. In other words, patch (2) and (3) and be applied and tested without changing the VFS, if desired.

This patch provides the VFS mechanism for handling filesystem-specific inode and super block sizes.

To apply:

cd /your/2.4.17/tree
cat this/patch | patch -p1

--
Daniel

drivers/block/rd.c +2 -2
@@ -673,7 +673,7 @@
 #endif
 	ram_device = MKDEV(MAJOR_NR, unit);
 
-	if ((inode = get_empty_inode()) == NULL)
+	if ((inode = get_empty_inode(NULL)) == NULL)
 		return;
 	memset(&infile, 0, sizeof(infile));
 	memset(&in_dentry, 0, sizeof(in_dentry));
@@ -683,7 +683,7 @@
 	infile.f_op = &def_blk_fops;
 	init_special_inode(inode, S_IFBLK | S_IRUSR, kdev_t_to_nr(device));
 
-	if ((out_inode = get_empty_inode()) == NULL)
+	if ((out_inode = get_empty_inode(NULL)) == NULL)
 		goto free_inode;
 	memset(&outfile, 0, sizeof(outfile));
 	memset(&out_dentry, 0, sizeof(out_dentry));
fs/inode.c +49 -22

158 lines not on this page · raw message

fs/super.c +34 -26

118 lines not on this page · raw message

include/linux/fs.h +29 -20

91 lines not on this page · raw message

include/linux/slab.h +1 -0
@@ -57,6 +57,7 @@
 extern int kmem_cache_shrink(kmem_cache_t *);
 extern void *kmem_cache_alloc(kmem_cache_t *, int);
 extern void kmem_cache_free(kmem_cache_t *, void *);
+extern void kmem_cache_clear(kmem_cache_t *, void *);
 
 extern void *kmalloc(size_t, int);
 extern void kfree(const void *);
mm/slab.c +10 -0
@@ -1078,6 +1078,16 @@
 	slabp->free = 0;
 }
 
+void kmem_cache_clear(kmem_cache_t *cachep, void *objp)
+{
+	unsigned size = cachep->objsize;
+#if DEBUG
+	if (cachep->flags & SLAB_RED_ZONE)
+		size -= BYTES_PER_WORD*2;
+#endif
+	memset(objp, 0, size);
+}
+
 /*
  * Grow (by 1) the number of slabs within a cache.  This is called by
  * kmem_cache_alloc() when there are no active objs left in a cache.
net/socket.c +1 -2
@@ -438,11 +438,10 @@
 	struct inode * inode;
 	struct socket * sock;
 
-	inode = get_empty_inode();
+	inode = get_empty_inode(sock_mnt->mnt_sb);
 	if (!inode)
 		return NULL;
 
-	inode->i_sb = sock_mnt->mnt_sb;
 	sock = socki_lookup(inode);
 
 	inode->i_mode = S_IFSOCK|S_IRWXUGO;
#1 lore raw

I share the opinion of a couple others, to use generic_ip and generic_sbp. That's what they are there for.

May I suggest the course of action whereby you convert the code to use ext2_sb(sb) and ext2_i(inode) first, without changing fs.h at all. That lays the groundwork for arguing out the final solution, and the code is much cleaner either way.

I have not seen anyone arguing -against- ext2_sb() and ext2_i() cleanups... they seem to make the code obviously more clean.

After those cleanups go in, your further "unbork fs.h" patches will be smaller and only show the core changes you are interested in.

Jeff

Signature · 4 lines
Jeff Garzik      | Alternate titles for LOTR:
Building 1024    | Fast Times at Uruk-Hai
MandrakeSoft     | The Took, the Elf, His Daughter and Her Lover
                 | Samwise Gamgee: International Hobbit of Mystery
#2 lore raw
In reply to Daniel Phillips
Quoted 2 lines · Jeff Garzik

I share the opinion of a couple others, to use generic_ip and generic_sbp. That's what they are there for.

That approach is workable but inferior.

Quoted 10 lines

May I suggest the course of action whereby you convert the code to use ext2_sb(sb) and ext2_i(inode) first, without changing fs.h at all. That lays the groundwork for arguing out the final solution, and the code is much cleaner either way.

I have not seen anyone arguing -against- ext2_sb() and ext2_i() cleanups... they seem to make the code obviously more clean.

After those cleanups go in, your further "unbork fs.h" patches will be smaller and only show the core changes you are interested in.

Patches (2) and (3), which implement the 'ext2_i' and 'ext2_s' changes respectively, apply independently of the patch (1), the VFS changes. Did you read my posting?

--
Daniel

#3 lore raw
In reply to Daniel Phillips
Quoted 5 lines

The approach I used to modify Ext2 would work well for all the filesystems in the tree. Most of the work consists of a simple global edit along the lines of:

   s/u.ext2_i./ext2_i(inode)->/

Whoops, please excuse me:

     s/inode->u.ext2_i./ext2_i(inode)->/

--
Daniel

#4 lore raw
In reply to Daniel Phillips

An update is available, with a number of nits and minor indiscretions picked up by Jeff Garzik fixed. No stoppers so far.

I've spammed the list enough today, so the patch set is here:

nl.linux.org/~phillips/patches/unbork.1-2.4.17
nl.linux.org/~phillips/patches/unbork.2-2.4.17
nl.linux.org/~phillips/patches/unbork.3-2.4.17
nl.linux.org/~phillips/patches/unbork.4-2.4.17

--
Daniel