一些文件系统内核API (1)

本文详细介绍了Linux系统中文件和inode的权限管理机制,包括文件权限掩码、文件结构体、inode结构体及其相关操作函数等内容。同时,还解释了硬链接与软链接的区别以及inode如何帮助系统管理文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件权限掩码

#define S_IRWXU 00700//所有者

#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IXUSR 00100


#define S_IRWXG 00070//用户组
#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010


#define S_IRWXO 00007//其他所有用户
#define S_IROTH 00004
#define S_IWOTH 00002
#define S_IXOTH 00001


//返回当前文件的权限值掩码
int current_umask(void)
{
return current->fs->umask;
}


//文件结构体
struct file {
union {
struct llist_node fu_llist;
struct rcu_head fu_rcuhead;
} f_u;
struct path f_path;
struct inode *f_inode;
const struct file_operations *f_op; //文件操作函数
spinlock_t f_lock;
atomic_long_t f_count;  //文件对象的引用计数器
unsigned int f_flags;
fmode_t f_mode; //进程访问方式
struct mutex f_pos_lock;
loff_t f_pos;
struct fown_struct f_owner;//通过信号进行IO事件通知的数据
const struct cred *f_cred;
struct file_ra_state f_ra;
u64 f_version; //版本号
#ifdef CONFIG_SECURITY
void *f_security;
#endif
void *private_data;
#ifdef CONFIG_EPOLL
struct list_head f_ep_links;
struct list_head f_tfile_llink;
#endif 
struct address_space *f_mapping;
} __attribute__((aligned(4)));


//通过文件描述符fd,查找到file结构体
struct file *fget(unsigned int fd)
{
return __fget(fd, FMODE_PATH);
}
static struct file *__fget(unsigned int fd, fmode_t mask)
{
struct files_struct *files = current->files;
struct file *file;


rcu_read_lock();
file = fcheck_files(files, fd);
if (file) {
/* File object ref couldn't be taken */
if ((file->f_mode & mask) ||
    !atomic_long_inc_not_zero(&file->f_count))
file = NULL;
}
rcu_read_unlock();


return file;
}


//返回系统能打开的文件最大数
unsigned long get_max_files(void)
{
return files_stat.max_files;
}


//inode详解
硬盘的最小存储单元叫扇区sector,一个sector通常是512byte,一个块block通常是8个扇区,4k大小
文件数据存储在一个又一个的block中。
文件源信息(文件创建者,大小,日期,权限)保存在inode中,索引结点,类似于任务的TCB
每个inode大小是128byte或256byte,它们也占用一定的磁盘空间,这就是为什么1T电脑买回来不足1T
【通过inode号码识别不同的文件,文件名是给用户看的,os不管】
查看inode信息需要目录的x权限


/*硬链接hard link*/
Unix系统允许多个文件对应一个inode,修改文件会影响所有文件,删除文件不影响其他
使用命令:ln 源文件 目标文件


创建目录时,默认会生成两个目录项:"."和".."。前者的inode号码就是当前目录的inode号码,
等同于当前目录的"硬链接";后者的inode号码就是当前目录的父目录的inode号码,
等同于父目录的"硬链接"。所以,任何一个目录的"硬链接"总数,总是等于2


/*软链接soft link*/
A与B的inode完全不一样,但是A的内容是B的路径,打开A最终会打开B,删除B那A就会报错
A不会增加B的链接数


struct inode {
umode_t i_mode;//节点模式
unsigned short i_opflags;
kuid_t i_uid;//user标识
kgid_t i_gid;//group标识
unsigned int i_flags;
#ifdef CONFIG_FS_POSIX_ACL
struct posix_acl *i_acl;
struct posix_acl *i_default_acl;
#endif
const struct inode_operations *i_op;//节点的操作函数
struct super_block *i_sb;
struct address_space *i_mapping;
#ifdef CONFIG_SECURITY
void *i_security;
#endif
unsigned long i_ino;//索引节点号
union {
const unsigned int i_nlink;//硬链接数目
unsigned int __i_nlink;
};
dev_t i_rdev;
loff_t i_size;//文件的字节数
struct timespec i_atime;//上次访问文件的时间
struct timespec i_mtime;//上次写文件的时间
struct timespec i_ctime;//上次修改inode的时间
spinlock_t i_lock;
unsigned short          i_bytes;//文件最后一个块的字节数
unsigned int i_blkbits;//块的位数
blkcnt_t i_blocks;//文件的块数
#ifdef __NEED_I_SIZE_ORDERED
seqcount_t i_size_seqcount;
#endif
unsigned long i_state;
struct mutex i_mutex;//节点信号量
unsigned long dirtied_when;
struct hlist_node i_hash;
struct list_head i_wb_list;
struct list_head i_lru;
struct list_head i_sb_list;
union {
struct hlist_head i_dentry;
struct rcu_head i_rcu;
};
u64 i_version;//版本号
atomic_t i_count;//引用计数器
atomic_t i_dio_count;
atomic_t i_writecount;
#ifdef CONFIG_IMA
atomic_t i_readcount; //读计数器
#endif
const struct file_operations *i_fop;
struct file_lock *i_flock;
struct address_space i_data;
struct list_head i_devices;
union {
struct pipe_inode_info *i_pipe;
struct block_device *i_bdev;
struct cdev *i_cdev;//指向字符设备驱动程序的指针
};
__u32 i_generation;
#ifdef CONFIG_FSNOTIFY
__u32 i_fsnotify_mask; 
struct hlist_head i_fsnotify_marks;
#endif
void *i_private; //文件或设备的私有指针
};


//增加inode的字节数,由bytes决定
void inode_add_bytes(struct inode *inode , loff_t bytes)


//得到整个inode节点的字节数
loff_t inode_get_bytes(struct inode *inode)


//设置整个inode节点的字节数
void inode_set_bytes(struct inode *inode,loff_t bytes)


//减少整个inode节点的字节数,由bytes决定
void inode_sub_bytes(struct inode *inode , loff_t bytes)


//判断是不是存在坏节点
int is_bad_inode(struct inode *inode)


//标记inode为坏节点
void make_bad_inode(struct inode *inode)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值