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