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?
There’s nothing as simple as you might hope.
Within the
do_sys_open()function, immediately beforereturn fd;, thestruct file *fpoints to a legitimate, opened,struct file.The
struct filecontains astruct path f_path.The
struct pathcontains astruct vfsmount *mnt.struct vfsmountrepresents every mounted filesystem on the system.The
struct vfsmountcontains astruct super_block *mnt_sb.The
struct super_blockcontains astruct block_device *s_bdev.The
struct block_devicecontains astruct hd_struct *bd_part.The
struct hd_structcontains astruct device __devand 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_structcontains astruct partition_meta_info *info.The
struct partition_meta_infocontains a field:This field is name of the device you’re after.