Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


attributes. These can either be specified explicitly viasysfs_dirent->iattrorcanbelefttothedefault
values if the field contains aNULLpointer. In this case, the following auxiliary function is used to set the
default attributes:

fs/sysfs/inode.c
static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
{
inode->i_mode = mode;
inode->i_uid = 0;
inode->i_gid = 0;
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
}

While the access mode of the file can be arbitrarily chosen by the caller, the ownership of the file belongs
toroot.rootin the default case.

Finally, the inode needs to be initialized according to the type of the sysfs entry:

fs/sysfs/inode.c
static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
{
...
/* initialize inode according to type */
switch (sysfs_type(sd)) {
case SYSFS_DIR:
inode->i_op = &sysfs_dir_inode_operations;
inode->i_fop = &sysfs_dir_operations;
inode->i_nlink = sysfs_count_nlink(sd);
break;
case SYSFS_KOBJ_ATTR:
inode->i_size = PAGE_SIZE;
inode->i_fop = &sysfs_file_operations;
break;
case SYSFS_KOBJ_BIN_ATTR:
bin_attr = sd->s_bin_attr.bin_attr;
inode->i_size = bin_attr->size;
inode->i_fop = &bin_fops;
break;
case SYSFS_KOBJ_LINK:
inode->i_op = &sysfs_symlink_inode_operations;
break;
default:
BUG();
}
...

Different types are distinguished by different inode and file operations.

10.3.4 File and Directory Operations


Since sysfs exposes its data structures in a filesystem, most interesting operations can be triggered with
standard filesystem operations. The functions that implement the filesystem operations thus serve as
a glue layer between sysfs and the internal data structures. As for every filesystem, the methods used
Free download pdf