1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_KDEV_T_H
3 #define _LINUX_KDEV_T_H
4 
5 #include <uapi/linux/kdev_t.h>
6 
7 #define MINORBITS	20
8 #define MINORMASK	((1U << MINORBITS) - 1)
9 
10 #define MAJOR(dev)	((unsigned int) ((dev) >> MINORBITS))
11 #define MINOR(dev)	((unsigned int) ((dev) & MINORMASK))
12 #define MKDEV(ma,mi)	(((ma) << MINORBITS) | (mi))
13 
14 #define print_dev_t(buffer, dev)					\
15 	sprintf((buffer), "%u:%u\n", MAJOR(dev), MINOR(dev))
16 
17 #define format_dev_t(buffer, dev)					\
18 	({								\
19 		sprintf(buffer, "%u:%u", MAJOR(dev), MINOR(dev));	\
20 		buffer;							\
21 	})
22 
23 /* acceptable for old filesystems */
old_valid_dev(dev_t dev)24 static __always_inline bool old_valid_dev(dev_t dev)
25 {
26 	return MAJOR(dev) < 256 && MINOR(dev) < 256;
27 }
28 
old_encode_dev(dev_t dev)29 static __always_inline u16 old_encode_dev(dev_t dev)
30 {
31 	return (MAJOR(dev) << 8) | MINOR(dev);
32 }
33 
old_decode_dev(u16 val)34 static __always_inline dev_t old_decode_dev(u16 val)
35 {
36 	return MKDEV((val >> 8) & 255, val & 255);
37 }
38 
new_encode_dev(dev_t dev)39 static __always_inline u32 new_encode_dev(dev_t dev)
40 {
41 	unsigned major = MAJOR(dev);
42 	unsigned minor = MINOR(dev);
43 	return (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
44 }
45 
new_decode_dev(u32 dev)46 static __always_inline dev_t new_decode_dev(u32 dev)
47 {
48 	unsigned major = (dev & 0xfff00) >> 8;
49 	unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
50 	return MKDEV(major, minor);
51 }
52 
huge_encode_dev(dev_t dev)53 static __always_inline u64 huge_encode_dev(dev_t dev)
54 {
55 	return new_encode_dev(dev);
56 }
57 
huge_decode_dev(u64 dev)58 static __always_inline dev_t huge_decode_dev(u64 dev)
59 {
60 	return new_decode_dev(dev);
61 }
62 
sysv_valid_dev(dev_t dev)63 static __always_inline int sysv_valid_dev(dev_t dev)
64 {
65 	return MAJOR(dev) < (1<<14) && MINOR(dev) < (1<<18);
66 }
67 
sysv_encode_dev(dev_t dev)68 static __always_inline u32 sysv_encode_dev(dev_t dev)
69 {
70 	return MINOR(dev) | (MAJOR(dev) << 18);
71 }
72 
sysv_major(u32 dev)73 static __always_inline unsigned sysv_major(u32 dev)
74 {
75 	return (dev >> 18) & 0x3fff;
76 }
77 
sysv_minor(u32 dev)78 static __always_inline unsigned sysv_minor(u32 dev)
79 {
80 	return dev & 0x3ffff;
81 }
82 
83 #endif
84