如何知道分区情况?
我使用的是 Linux(Ubuntu 11.10)。 好吧,当我调用系统调用 open 时,例如在 C 程序中:
size_t filedesc = open("testfile.txt",O_CREAT | O_WRONLY,0640);
如何访问分区,我的意思是有没有办法返回所使用的分区?
系统调用 open 的定义如上:
SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode)
如果我愿意,我可以输入 printk("%s",filename)
并查看路径。但是我如何访问该分区呢?
示例:我在两个不同的分区中有两个存档 example.txt(例如 /dev/sda1 和 /dev/sda2)。
然后我调用系统调用 open: 假设我调用了分区 /dev/sda2 中的 example.txt。 有没有办法使用 open 系统调用来访问分区(例如,printk(KERN_ALERT "%s",partition)
)?
I'm using Linux (Ubuntu 11.10).
Well, when I call the system call open, for example in a C program:
size_t filedesc = open("testfile.txt",O_CREAT | O_WRONLY,0640);
How can I access the partition, I mean is there a way to return the partition used?
The system call open is the defined above:
SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode)
If I want, I can put a printk("%s",filename)
and see the path. But how I can access the partition?
An example: I have two archives example.txt in two different partitions (for example /dev/sda1 and /dev/sda2).
Then I call the system call open: Lets suppose I called the example.txt in the partition /dev/sda2.
Is there a way to acess the partition (for example, printk(KERN_ALERT "%s",partition)
) using the open system call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有什么像您希望的那么简单。
在
do_sys_open()
函数中,在return fd;
之前,struct file *f
指向一个合法的、打开的struct文件
。结构文件
包含一个结构路径f_path
。struct path
包含一个struct vfsmount *mnt
。struct vfsmount
代表系统上每个已挂载的文件系统。struct vfsmount
包含一个struct super_block *mnt_sb
。struct super_block
包含一个struct block_device *s_bdev
。struct block_device
包含一个struct hd_struct *bd_part
。struct hd_struct
包含一个struct device __dev
和一个int partno
。这两者共同定义了您的文件所在的分区。更新
当我找到设备和分区号引用时,我最初停止了查找,因为我认为这就是将人类友好的字符串组合在一起所需的全部内容。但当我再次用新的眼光观察时,我发现还有更多内容:
struct hd_struct 包含一个 structpartition_meta_info *info。
struct partition_meta_info
包含一个字段:该字段是您要查找的设备的名称。
There's nothing as simple as you might hope.
Within the
do_sys_open()
function, immediately beforereturn fd;
, thestruct file *f
points to a legitimate, opened,struct file
.The
struct file
contains astruct path f_path
.The
struct path
contains astruct vfsmount *mnt
.struct vfsmount
represents every mounted filesystem on the system.The
struct vfsmount
contains astruct super_block *mnt_sb
.The
struct super_block
contains astruct block_device *s_bdev
.The
struct block_device
contains astruct hd_struct *bd_part
.The
struct hd_struct
contains astruct device __dev
and anint partno
. Together, these two define which partition your file is located on.Update
I had originally stopped looking when I found the device and partition number references, since I assumed that was all that was required to put together the human-friendly string. But when looking again with fresh eyes, I see there is more:
The
struct hd_struct
contains astruct partition_meta_info *info
.The
struct partition_meta_info
contains a field:This field is name of the device you're after.
通过 shell,
df /some/dir
为您提供所涉及的文件系统。以编程方式,使用stat
系统调用,您将获得st_dev
字段。(补充:)我不知道你到底想做什么,但也许使用 来做到这一点FUSE 可以更简单。
Thru a shell,
df /some/dir
gives you the file-system involved. Programmatically, withstat
system call, you get thest_dev
field.(added:) I don't guess what you want to do exactly, but perhaps doing that using FUSE could be simpler.