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 */
24static inline bool old_valid_dev(dev_t dev)
25{
26 return MAJOR(dev) < 256 && MINOR(dev) < 256;
27}
28
29static inline u16 old_encode_dev(dev_t dev)
30{
31 return (MAJOR(dev) << 8) | MINOR(dev);
32}
33
34static inline dev_t old_decode_dev(u16 val)
35{
36 return MKDEV((val >> 8) & 255, val & 255);
37}
38
39static 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
46static 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
53static inline u64 huge_encode_dev(dev_t dev)
54{
55 return new_encode_dev(dev);
56}
57
58static inline dev_t huge_decode_dev(u64 dev)
59{
60 return new_decode_dev(dev);
61}
62
63static inline int sysv_valid_dev(dev_t dev)
64{
65 return MAJOR(dev) < (1<<14) && MINOR(dev) < (1<<18);
66}
67
68static inline u32 sysv_encode_dev(dev_t dev)
69{
70 return MINOR(dev) | (MAJOR(dev) << 18);
71}
72
73static inline unsigned sysv_major(u32 dev)
74{
75 return (dev >> 18) & 0x3fff;
76}
77
78static inline unsigned sysv_minor(u32 dev)
79{
80 return dev & 0x3ffff;
81}
82
83#endif
84