Skip to contentSkip to conversation

lazy allocation of struct block_device

RFC [email protected] 4 participants 4 messages Sep 1, 2001

lore
Share & tools
lore
#0 lore raw

From [email protected] Sat Sep 1 18:26:53 2001

On Sat, 1 Sep 2001 [email protected] wrote:

> A kdev_t is a pointer to a struct that has the info now found in > the arrays (and major, minor fields, and a name function..). > This struct is allocated by the driver.

Umm... Apply the arguments from the char_device thread - pointers to unions are rather bad idea. IOW, kdev_t must die - kernel always knows which kind we are dealing with.

I don't mind.
Nothing really changes if you split it into kbdev_t and kcdev_t.

(Splitting is a lot of work, a large source edit,
but basically straightforward. I did similar things a few times, splitting MKDEV into MKBDEV and MKCDEV [and MKXDEV]. However, a union is not so bad. It seems a pity to avoid unions and waste 4 bytes for every inode with separate i_bdev and i_cdev instead of a single i_bcdev.
Anyway, this has nothing to do with the present discussion.)

...

Quoted 5 lines

This "a refcount" is the openct field of the device struct, somewhat like the present bd_openers.

The decrements of the refcount are done in kill_super() for s_dev and at the close/umount corresponding to the open/mount that set it for i_bcdev.

??? So you decrement twice on umount?

Yes, and increment twice on mount.
That may be easiest since s_dev can also come from get_unnamed_dev().

...
How do you tell when inode is not opened anymore?
...
When do you reset it [i_bcdev]?

Now I see what you are asking - at first I thought you wondered about the bookkeeping for the device struct, and there is no problem there, but you ask about the bookkeeping for i_bcdev.

Since the refcount is for the device struct, we cannot do anything until that count hits zero. Then the release method of the device struct is called (which frees it, or decrements a refcount for the module). After this i_bcdev cannot be used anymore.
Since we don't know whether this happened already, i_bcdev must be recomputed on each open or mount.

(One might invent additional data structure to avoid this recomputation, but data structures take memory and add the complication that they must be kept consistent and up-to-date. Since mounting, or opening a block device, are very infrequent operations, it does not matter that we do a possibly superfluous bdopen().)

Andries

#1 lore raw
Quoted 5 lines · [email protected] wrote:

From [email protected] Sat Sep 1 18:26:53 2001
> A kdev_t is a pointer to a struct that has the info now found in > the arrays (and major, minor fields, and a name function..). > This struct is allocated by the driver.

Quoted 7 lines

Umm... Apply the arguments from the char_device thread - pointers to unions are rather bad idea. IOW, kdev_t must die - kernel always knows which kind we are dealing with.
[...]
However, a union is not so bad. It seems a pity to avoid unions and waste 4 bytes for every inode with separate i_bdev and i_cdev instead of a single i_bcdev.

Please, a union of different pointer types is much nicer. You can have i_bdev and i_cdev without wasting any bytes.

This form works with GCC 2.96:

		union {
			struct char_device * i_cdev;
			struct block_device * i_bdev;
		};

If you're using a really old compiler that doesn't support anonymous unions, (GCC 2.95 might be in this category, I'm not sure), then you'll need this:

	#define i_bdev __i_bcdev_union.i_bdev
	#define i_cdev __i_bcdev_union.i_cdev
		union {
			struct char_device * i_cdev;
			struct block_device * i_bdev;
		} __i_bcdev_union;

Either way, you avoid pointers to unions and you also avoid having a named union type which contains pointers.

-- Jamie

#2 lore raw
Quoted 9 lines · Jamie Lokier
Quoted 4 lines · [email protected] wrote:

[...]
However, a union is not so bad. It seems a pity to avoid unions and waste 4 bytes for every inode with separate i_bdev and i_cdev instead of a single i_bcdev.

Please, a union of different pointer types is much nicer. You can have i_bdev and i_cdev without wasting any bytes.

Quoted 6 lines

This form works with GCC 2.96:

                union {
                        struct char_device * i_cdev;
                        struct block_device * i_bdev;
                };

It sure does. One of the nicest new gcc features IMHO!

Quoted 2 lines

If you're using a really old compiler that doesn't support anonymous unions, (GCC 2.95 might be in this category, I'm not sure),

GCC 2.95 does NOT support this. I only found out after I had people complain to me that Linux-NTFS userspace tools would not compile for them and it turned out they were using gcc-2.95... and I 2.96. - I have since then verified this myself:

egcs and gcc up to 2.95 do not support unnamed structs/unions.

gcc-2.96 and gcc-3.0 support them fine.

Quoted 8 lines

then you'll need this:

        #define i_bdev __i_bcdev_union.i_bdev
        #define i_cdev __i_bcdev_union.i_cdev
                union {
                        struct char_device * i_cdev;
                        struct block_device * i_bdev;
                } __i_bcdev_union;

Neat trick! Thanks! I was wondering what to do with NTFS TNG driver (which uses unnamed structs/unions extensively) and this just might solve my problems without having to rewrite half the driver... (-;

Best regards,

Anton

Signature · 5 lines
  "Nothing succeeds like success." - Alexandre Dumas
-- 
Anton Altaparmakov <aia21 at cam.ac.uk> (replace at with @)
Linux NTFS Maintainer / WWW: http://linux-ntfs.sf.net/
ICQ: 8561279 / WWW: http://www-stu.christs.cam.ac.uk/~aia21/
#3 lore raw
In reply to [email protected]
Quoted 11 lines · On Sat, 1 Sep 2001 [email protected] wrote:

How do you tell when inode is not opened anymore?
...
When do you reset it [i_bcdev]?

Now I see what you are asking - at first I thought you wondered about the bookkeeping for the device struct, and there is no problem there, but you ask about the bookkeeping for i_bcdev.

Since the refcount is for the device struct, we cannot do anything until that count hits zero. Then the release method of the device struct is called (which frees it, or decrements a refcount for the module).

Wait a minute. You had suggested (upthread) that these objects are allocated by partition code and contain per-partition data. Freeing them once the device is closed looks very odd.

Quoted 3 lines

After this i_bcdev cannot be used anymore.
Since we don't know whether this happened already, i_bcdev must be recomputed on each open or mount.

... and that means that we can't make devfs-like filesystems just set ->i_bdev (or ->i_cdev) at read_super() (or lookup()) and avoid all mess with major/minor allocation. IMO that's unfortunate, especially since majors allocation is on the permanent freeze.

Quoted 5 lines

(One might invent additional data structure to avoid this recomputation, but data structures take memory and add the complication that they must be kept consistent and up-to-date. Since mounting, or opening a block device, are very infrequent operations, it does not matter that we do a possibly superfluous bdopen().)

Once you look at character devices (they have same set of problems) frequency goes up big way.