]>
git.ipfire.org Git - people/jschlag/ipfire-2.x.git/blob - src/nash/name_to_dev_t.c
8 static dev_t
try_name(char *name
, int part
)
17 unsigned int maj
, min
;
19 /* read device number from .../dev */
21 sprintf(path
, "/sys/block/%s/dev", name
);
22 fd
= open(path
, O_RDONLY
);
25 len
= read(fd
, buf
, 32);
27 if (len
<= 0 || len
== 32 || buf
[len
- 1] != '\n')
30 if (sscanf(buf
, "%u:%u", &maj
, &min
) != 2)
32 res
= makedev(maj
, min
);
33 if (maj
!= major(res
) || min
!= minor(res
))
36 /* if it's there and we are not looking for a partition - that's it */
40 /* otherwise read range from .../range */
41 snprintf(path
, 64, "/sys/block/%s/range", name
);
42 fd
= open(path
, O_RDONLY
);
45 len
= read(fd
, buf
, 32);
47 if (len
<= 0 || len
== 32 || buf
[len
- 1] != '\n')
50 range
= strtoul(buf
, &s
, 10);
54 /* if partition is within range - we got it */
62 * Convert a name into device number. We accept the following variants:
64 * 1) device number in hexadecimal represents itself
65 * 2) /dev/nfs represents Root_NFS (0xff)
66 * 3) /dev/<disk_name> represents the device number of disk
67 * 4) /dev/<disk_name><decimal> represents the device number
68 * of partition - device number of disk plus the partition number
69 * 5) /dev/<disk_name>p<decimal> - same as the above, that form is
70 * used when disk name of partitioned disk ends on a digit.
72 * If name doesn't have fall into the categories above, we return 0.
73 * sysfs is used to check if something is a disk name - it has
74 * all known disks under bus/block/devices. If the disk name
75 * contains slashes, name of driverfs node has them replaced with
76 * bangs. try_name() does the actual checks, assuming that sysfs
79 * Note that cases (1) and (2) are already handled by the kernel,
80 * so we can ifdef them out, provided that we check real-root-dev
84 dev_t
name_to_dev_t(char *name
)
91 if (strncmp(name
, "/dev/", 5) != 0) {
92 #if 1 /* kernel used to do this */
95 if (sscanf(name
, "%u:%u", &maj
, &min
) == 2) {
96 res
= makedev(maj
, min
);
97 if (maj
!= major(res
) || min
!= minor(res
))
100 res
= strtoul(name
, &p
, 16);
110 #if 1 /* kernel used to do this */
111 if (strcmp(name
, "nfs") == 0)
112 return makedev(0, 255);
115 if (strlen(name
) > 31)
121 res
= try_name(s
, 0);
125 while (p
> s
&& isdigit(p
[-1]))
127 if (p
== s
|| !*p
|| *p
== '0')
129 part
= strtoul(p
, NULL
, 10);
131 res
= try_name(s
, part
);
135 if (p
< s
+ 2 || !isdigit(p
[-2]) || p
[-1] != 'p')
138 return try_name(s
, part
);