Search code examples
clinuxkernelsuperblock

circular dependency between Dentry and superblock structure in linux kernel


I used to do some oo programming. now I am reading linux kernel code in C. I found :

struct super_block {
        ...
        ...
        unsigned long            s_flags;          /* mount flags */
        unsigned long            s_magic;          /* filesystem's magic number */
        struct dentry            *s_root;          /* directory mount point */
        struct rw_semaphore      s_umount;         /* unmount semaphore */
        ...
        ...
        }

struct dentry {
        ...
        ...
        struct dentry_operations *d_op;        /* dentry operations table */
        struct super_block       *d_sb;        /* superblock of file */
        unsigned int             d_flags;      /* dentry flags */
        int                      d_mounted;    /* is this a mount point? */
        void                     *d_fsdata;    /* filesystem-specific data */
        ...
        ...
};

we can see the super_block struct has a struct dentry attribute and struct dentry has a super_block attribute. does it cause circular dependency? thanks a lot

if yes, how does the memory manage work? for example, if a dentry object was deleted, the super_block would point to a invalid location. I meant how to manage their life cycle.


Solution

  • First, there is a circular dependency between the two structures, but -

    There is no problem with that when there is forward declaration (e.g. struct dentry; before the definition of struct super_block and vice versa) as both structs use pointers to the other structs and the size of the pointers is known anyway. Using fields of each structure will require its definition beforehand.