In Posix how is type dev_t getting used?
Asked Answered
F

4

8

What I am after is the meaning of such type and what interface can use it.

It is explained in Posix spec that dev_t is used for device IDs. However, what device id means for any object described by a path, which can be a file, a directy, a fifo or a physical device?

For example, calling stat() shall give you a struct including a member of such type; and you can stat any kinds of object in your file system. The device id should have different meanings for different file types then.

Fluxmeter answered 9/3, 2012 at 14:30 Comment(0)
A
10

The only use of dev_t in the vast majority of programs (ones which are portable and not connected to a single OS) is to determine that two file names or file descriptors refer to the same underlying file. This is true if and only if the st_ino and st_dev entries for the two files' stat structures match one another.

Basically, st_dev tells which "device" (e.g. mounted partition, network share, etc.) the file resides on, and st_ino is a unique identifier of the file within the context of a single device.

Aliment answered 9/3, 2012 at 17:8 Comment(4)
Can't you have multiple file systems on one device?Hesperidin
@user129393192: No, each partition or other virtualized (e.g. loopback) block device is its own device (its own dev_t value).Aliment
You mean any file system or other "device" has a dev_t? So it's not based on partition?Hesperidin
@user129393192: Every thing which is mountable has a unique dev_t.Aliment
H
7

Actually, there are two dev_t-typed fields in struct stat:

  • st_dev is the "[d]evice ID of device containing file", so if two files have the same st_dev, they're on the same filesystem.
  • st_rdev is the device ID of the device denoted by a character or block special file, i.e. the files commonly encountered in /dev. It has no meaning for other types of files.
Harbard answered 9/3, 2012 at 14:53 Comment(2)
But you did not explain what the device id is. For a normal file, there is no device associated, then what does the id mean here?Fluxmeter
@MengfeiMurphy: every file exists on some device, be it a disk partition or a network share. That's what st_dev designates.Harbard
C
6

Within the kernel, the dev_t type who is defined in is used to hold device numbers (major/minor). dev_t is a 32-bit quantity with 12 bits set aside for the major number and 20 for the minor number.

Cartridge answered 25/1, 2014 at 12:57 Comment(1)
Yes, my /dev/sda1 st_rdev gives 2049, which is true because it is in major 8 min 1Casanova
E
1

NOT an answer to the question, just update some info about st_dev

dev_t in current glibc (2.35) is 64-bit, with 32-bit major and minor numbers. glibc's default encoding is MMMM Mmmm mmmM MMmm, where M is a hex digit of the major number and m is a hex digit of the minor number. This is backward compatible with legacy systems where dev_t is 16 bits wide, encoded as MMmm. It is also backward compatible with the Linux kernel, which for some architectures uses 32-bit dev_t, encoded as mmmM MMmm.

One can use major(3) and minor(3) to decompose a dev_t:

$ cat main.c
#include <assert.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <stdio.h>

int main(void)
{
        struct stat buf;
        assert(0 == stat(".", &buf));

        printf("Major device ID: %d\n", major(buf.st_dev));
        printf("Minor device ID: %d\n", minor(buf.st_dev));
}

$ gccs main.c && ./a.out
Major device ID: 0
Minor device ID: 39
Extrabold answered 2/10, 2022 at 4:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.