kdev_t.h 1.7 KB

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