linux有内核文件操作函数,例filp_open()函数可用于打开文件、vfs_read()函数可用于读取文件、vfs_write()函数可用于写文件、filp_close()函数可用于关闭文件。在vfs_read和vfs_write函数中,其第二个参数指向的用户空间的内存地址,如果直接使用内核空间的指针,则会返回“-EFALUT”。
1.内核空间文件操作
| 功能 | 函数原型 |
|---|---|
| 打开文件 | structfile*filp_open(constchar*filename,intflags,intmode) |
| 读文件 | ssize_tvfs_read(structfile*file,char__user*buf,size_tcount,loff_t*pos) |
| 写文件 | ssize_tvfs_write(structfile*file,constchar__user*buf,size_tcount,loff_t*pos) |
| 关闭文件 | intfilp_close(structfile*filp,fl_owner_tid) |
2.内核空间与用户空间
在vfs_read和vfs_write函数中,其参数buf指向的用户空间的内存地址,如果我们直接使用内核空间的指针,则会返回-EFALUT。这是因为使用的缓冲区超过了用户空间的地址范围。一般系统调用会要求你使用的缓冲区不能在内核区。这个可以用来解决。
#defineMAKE_MM_SEG(s)((mm_segment_t){(s)})
#defineKERNEL_DSMAKE_MM_SEG(0xFFFFFFFF)
#defineUSER_DSMAKE_MM_SEG(PAGE_OFFSET)
#defineget_ds()(KERNEL_DS)
#defineget_fs()(current->addr_limit)
#defineset_fs(x)(current->addr_limit=(x))
如果使用,如下:
set_fs(fs);
详尽解释:系统调用本来是提供给用户空间的程序访问的,所以,对传递给它的参数(比如上面的buf),它默认会认为来自用户空间,在read或write()函数中,为了保护内核空间,一般会用得到的值来和USER_DS进行比较,从而防止用户空间程序“蓄意”破坏内核空间;而现在要在内核空间使用系统调用,此时传递给read或write()的参数地址就是内核空间的地址了,在USER_DS之上(USER_DS~KERNEL_DS),如果不做任何其它处理,在write()函数中,会认为该地址超过了USER_DS范围,所以会认为是用户空间的“蓄意破坏”,从而不允许进一步的执行;为了解决这个问题将其能访问的空间限制扩大到KERNEL_DS,这样就可以在内核顺利使用系统调用了!
3.Linuxstructinode结构
structrw_semaphorei_alloc_sem;
structinode_operations*i_op;
structfile_operations*i_fop;
structaddress_space*i_mapping;
structaddress_spacei_data;
structdquot*i_dquot[MAXQUOTAS];
structlist_headi_devices;
structpipe_inode_info*i_pipe;
structblock_device*i_bdev;
unsignedlongi_dnotify_mask;
structdnotify_struct*i_dnotify;
unsignedlongdirtied_when;
int(*create)(structinode*,structdentry*,int);
structdentry*(*lookup)(structinode*,structdentry*);

int(*unlink)(structinode*,structdentry*);
int(*symlink)(structinode*,structdentry*,constchar*);
int(*mkdir)(structinode*,structdentry*,int);
int(*rmdir)(structinode*,structdentry*);
int(*mknod)(structinode*,structdentry*,int,dev_t);
int(*rename)(structinode*,structdentry*,
structinode*,structdentry*);
int(*readlink)(structdentry*,char*,int);
int(*follow_link)(structdentry*,structnameidata*);
int(*put_link)(structdentry*,structnameidata*);
void(*truncate)(structinode*);
int(*permission)(structinode*,int);
int(*setattr)(structdentry*,structiattr*);
int(*getattr)(structvfsmount*,structdentry*,structkstat*);
int(*setxattr)(structdentry*,constchar*,
ssize_t(*getxattr)(structdentry*,constchar*,void*,size_t);
ssize_t(*listxattr)(structdentry*,char*,size_t);
int(*removexattr)(structdentry*,constchar*);
};
4.Linuxstructfile结构structfile结构体定义在/linux/include/linux/fs.h(Linux2.6.11内核)中,其原型是:
structrcu_headfu_rcuhead;
#definef_dentryf_path.dentry
#definef_vfsmntf_path.mnt
conststructfile_operations*f_op;
structfown_structf_owner;
structlist_headf_ep_links;
structaddress_space*f_mapping;
};
在系统内核空间中,每个打开的文件都有一个对应的structfile。当打开文件并进行操作时,内核会创建一个该文件的描述符,并将其传递给相关的函数。在文件的所有实例都关闭后,内核释放这个数据结构。通常在内核创建和驱动源码中,指向structfile的指针被命名为file或filp。
以上就是linux有没有内核文件操作函数的详细内容,更多请关注主机测评网其它相关文章!
本文来源:国外服务器--linux有没有内核文件操作函数(linux的内核文件在哪里)
本文地址:https://www.idcbaba.com/guowai/5254.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 1919100645@qq.com 举报,一经查实,本站将立刻删除。



