Files never have a name!

I have just started reading Robert Loves Linux system programming.Guess there cant be a better warm up exercise to the kernel hacking process.An hour in to the book makes me overwhelm all that I have learned (!!!) in this one and half year at ***.

I was agape when I found that the files are never stored by their name internally. Woo wow the reason might be searching a list of strings is a fools tasks.Numbers are always easy to sort and reach.

Every file has been attached to a data structure called inode. It stores all the details owned and controlled by the kernel for the specific file.To your amazement the file name is not integral part of this structure. In uni/linux almost everything is abstracted as a file.

OOPS I hear you shouting how do I reach my file??

Hold on thats achieved with the help of a special file called directory. A directory is nothing but a file containing a table (hash table)

mapping your file nam
es against their inodes.

So whenever you type a file name its inode is got from this file (try finding how its implemented) and once you get you inode thats the key to all the information about your file.

Its pretty much more than what a newbie like me could decipher..So I copy paste it leaving it for you to elucidate it.

struct inode {

struct hlist_node i_hash;
struct list_head i_list;
struct list_head i_dentry; /This is directory entry*/

/* list.h is good place to see how linked lists are manipulated*/
unsigned long i_ino; // This is our key in the world of files.

atomic_t i_count;
umode_t i_mode;
unsigned int i_nlink;
uid_t i_uid;
gid_t i_gid;
dev_t i_rdev;
loff_t i_size;
struct timespec i_atime;
struct timespec i_mtime;
struct timespec i_ctime;
unsigned int i_blkbits;
unsigned long i_blksize;
unsigned long i_version;
unsigned long i_blocks;
unsigned short i_bytes;
unsigned char i_sock;
spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */
struct semaphore i_sem;
struct rw_semaphore i_alloc_sem;
struct inode_operations *i_op;
struct file_operations *i_fop; /* former ->i_op->default_file_ops */
struct super_block *i_sb;
struct file_lock *i_flock;
struct address_space *i_mapping;
struct address_space i_data;
#ifdef CONFIG_QUOTA
struct dquot *i_dquot[MAXQUOTAS];
#endif
/* These three should probably be a union */
struct list_head i_devices;
struct pipe_inode_info *i_pipe;
struct block_device *i_bdev;
struct cdev *i_cdev;
int i_cindex;

__u32 i_generation;

#ifdef CONFIG_DNOTIFY
unsigned long i_dnotify_mask; /* Directory notify events */
struct dnotify_struct *i_dnotify; /* for directory notifications */
#endif

unsigned long i_state;
unsigned long dirtied_when; /* jiffies of first dirtying */

unsigned int i_flags;

atomic_t i_writecount;
void *i_security;
union {
void *generic_ip;
} u;
#ifdef __NEED_I_SIZE_ORDERED
seqcount_t i_size_seqcount;
#endif
};

Well this is the start of my expedition in to the kernel. Thats pretty much to grep the world for.When you pour laurels on google desktop search why cant we try to test and improve this

Now One question to end with…. Whats the maximum number of files a directory can have …?
Is it architecture dependent ??

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: