如何知道分区情况?

发布于 2024-12-16 19:26:17 字数 589 浏览 3 评论 0原文

我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一萌ing 2024-12-23 19:26:17

没有什么像您希望的那么简单。

do_sys_open() 函数中,在 return fd; 之前,struct file *f 指向一个合法的、打开的 struct文件

结构文件包含一个结构路径f_path

struct path 包含一个 struct vfsmount *mntstruct 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 包含一个字段:

    u8 volname[PARTITION_META_INFO_VOLNAMELTH];

该字段是您要查找的设备的名称。

There's nothing as simple as you might hope.

Within the do_sys_open() function, immediately before return fd;, the struct file *f points to a legitimate, opened, struct file.

The struct file contains a struct path f_path.

The struct path contains a struct vfsmount *mnt. struct vfsmount represents every mounted filesystem on the system.

The struct vfsmount contains a struct super_block *mnt_sb.

The struct super_block contains a struct block_device *s_bdev.

The struct block_device contains a struct hd_struct *bd_part.

The struct hd_struct contains a struct device __dev and an int 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 a struct partition_meta_info *info.

The struct partition_meta_info contains a field:

    u8 volname[PARTITION_META_INFO_VOLNAMELTH];

This field is name of the device you're after.

一抹苦笑 2024-12-23 19:26:17

通过 shell,df /some/dir 为您提供所涉及的文件系统。以编程方式,使用 stat 系统调用,您将获得 st_dev 字段。

补充:)我不知道你到底想做什么,但也许使用 来做到这一点FUSE 可以更简单。

Thru a shell, df /some/dir gives you the file-system involved. Programmatically, with stat system call, you get the st_dev field.

(added:) I don't guess what you want to do exactly, but perhaps doing that using FUSE could be simpler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文