genhd.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_GENHD_H
  3. #define _LINUX_GENHD_H
  4. /*
  5. * genhd.h Copyright (C) 1992 Drew Eckhardt
  6. * Generic hard disk header file by
  7. * Drew Eckhardt
  8. *
  9. * <drew@colorado.edu>
  10. */
  11. #include <linux/types.h>
  12. #include <linux/kdev_t.h>
  13. #include <linux/rcupdate.h>
  14. #include <linux/slab.h>
  15. #include <linux/percpu-refcount.h>
  16. #include <linux/uuid.h>
  17. #include <linux/blk_types.h>
  18. #include <linux/android_kabi.h>
  19. #include <asm/local.h>
  20. #define dev_to_disk(device) container_of((device), struct gendisk, part0.__dev)
  21. #define dev_to_part(device) container_of((device), struct hd_struct, __dev)
  22. #define disk_to_dev(disk) (&(disk)->part0.__dev)
  23. #define part_to_dev(part) (&((part)->__dev))
  24. extern const struct device_type disk_type;
  25. extern struct device_type part_type;
  26. extern struct class block_class;
  27. #define DISK_MAX_PARTS 256
  28. #define DISK_NAME_LEN 32
  29. #include <linux/major.h>
  30. #include <linux/device.h>
  31. #include <linux/smp.h>
  32. #include <linux/string.h>
  33. #include <linux/fs.h>
  34. #include <linux/workqueue.h>
  35. #define PARTITION_META_INFO_VOLNAMELTH 64
  36. /*
  37. * Enough for the string representation of any kind of UUID plus NULL.
  38. * EFI UUID is 36 characters. MSDOS UUID is 11 characters.
  39. */
  40. #define PARTITION_META_INFO_UUIDLTH (UUID_STRING_LEN + 1)
  41. struct partition_meta_info {
  42. char uuid[PARTITION_META_INFO_UUIDLTH];
  43. u8 volname[PARTITION_META_INFO_VOLNAMELTH];
  44. };
  45. struct hd_struct {
  46. sector_t start_sect;
  47. /*
  48. * nr_sects is protected by sequence counter. One might extend a
  49. * partition while IO is happening to it and update of nr_sects
  50. * can be non-atomic on 32bit machines with 64bit sector_t.
  51. */
  52. sector_t nr_sects;
  53. #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
  54. seqcount_t nr_sects_seq;
  55. #endif
  56. unsigned long stamp;
  57. struct disk_stats __percpu *dkstats;
  58. struct percpu_ref ref;
  59. struct device __dev;
  60. struct kobject *holder_dir;
  61. int policy, partno;
  62. struct partition_meta_info *info;
  63. #ifdef CONFIG_FAIL_MAKE_REQUEST
  64. int make_it_fail;
  65. #endif
  66. struct rcu_work rcu_work;
  67. ANDROID_KABI_RESERVE(1);
  68. ANDROID_KABI_RESERVE(2);
  69. ANDROID_KABI_RESERVE(3);
  70. ANDROID_KABI_RESERVE(4);
  71. };
  72. /**
  73. * DOC: genhd capability flags
  74. *
  75. * ``GENHD_FL_REMOVABLE`` (0x0001): indicates that the block device
  76. * gives access to removable media.
  77. * When set, the device remains present even when media is not
  78. * inserted.
  79. * Must not be set for devices which are removed entirely when the
  80. * media is removed.
  81. *
  82. * ``GENHD_FL_CD`` (0x0008): the block device is a CD-ROM-style
  83. * device.
  84. * Affects responses to the ``CDROM_GET_CAPABILITY`` ioctl.
  85. *
  86. * ``GENHD_FL_UP`` (0x0010): indicates that the block device is "up",
  87. * with a similar meaning to network interfaces.
  88. *
  89. * ``GENHD_FL_SUPPRESS_PARTITION_INFO`` (0x0020): don't include
  90. * partition information in ``/proc/partitions`` or in the output of
  91. * printk_all_partitions().
  92. * Used for the null block device and some MMC devices.
  93. *
  94. * ``GENHD_FL_EXT_DEVT`` (0x0040): the driver supports extended
  95. * dynamic ``dev_t``, i.e. it wants extended device numbers
  96. * (``BLOCK_EXT_MAJOR``).
  97. * This affects the maximum number of partitions.
  98. *
  99. * ``GENHD_FL_NATIVE_CAPACITY`` (0x0080): based on information in the
  100. * partition table, the device's capacity has been extended to its
  101. * native capacity; i.e. the device has hidden capacity used by one
  102. * of the partitions (this is a flag used so that native capacity is
  103. * only ever unlocked once).
  104. *
  105. * ``GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE`` (0x0100): event polling is
  106. * blocked whenever a writer holds an exclusive lock.
  107. *
  108. * ``GENHD_FL_NO_PART_SCAN`` (0x0200): partition scanning is disabled.
  109. * Used for loop devices in their default settings and some MMC
  110. * devices.
  111. *
  112. * ``GENHD_FL_HIDDEN`` (0x0400): the block device is hidden; it
  113. * doesn't produce events, doesn't appear in sysfs, and doesn't have
  114. * an associated ``bdev``.
  115. * Implies ``GENHD_FL_SUPPRESS_PARTITION_INFO`` and
  116. * ``GENHD_FL_NO_PART_SCAN``.
  117. * Used for multipath devices.
  118. */
  119. #define GENHD_FL_REMOVABLE 0x0001
  120. /* 2 is unused (used to be GENHD_FL_DRIVERFS) */
  121. /* 4 is unused (used to be GENHD_FL_MEDIA_CHANGE_NOTIFY) */
  122. #define GENHD_FL_CD 0x0008
  123. #define GENHD_FL_UP 0x0010
  124. #define GENHD_FL_SUPPRESS_PARTITION_INFO 0x0020
  125. #define GENHD_FL_EXT_DEVT 0x0040
  126. #define GENHD_FL_NATIVE_CAPACITY 0x0080
  127. #define GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE 0x0100
  128. #define GENHD_FL_NO_PART_SCAN 0x0200
  129. #define GENHD_FL_HIDDEN 0x0400
  130. enum {
  131. DISK_EVENT_MEDIA_CHANGE = 1 << 0, /* media changed */
  132. DISK_EVENT_EJECT_REQUEST = 1 << 1, /* eject requested */
  133. };
  134. enum {
  135. /* Poll even if events_poll_msecs is unset */
  136. DISK_EVENT_FLAG_POLL = 1 << 0,
  137. /* Forward events to udev */
  138. DISK_EVENT_FLAG_UEVENT = 1 << 1,
  139. };
  140. struct disk_part_tbl {
  141. struct rcu_head rcu_head;
  142. int len;
  143. struct hd_struct __rcu *last_lookup;
  144. struct hd_struct __rcu *part[];
  145. };
  146. struct disk_events;
  147. struct badblocks;
  148. struct blk_integrity {
  149. const struct blk_integrity_profile *profile;
  150. unsigned char flags;
  151. unsigned char tuple_size;
  152. unsigned char interval_exp;
  153. unsigned char tag_size;
  154. ANDROID_KABI_RESERVE(1);
  155. ANDROID_KABI_RESERVE(2);
  156. };
  157. struct gendisk {
  158. /* major, first_minor and minors are input parameters only,
  159. * don't use directly. Use disk_devt() and disk_max_parts().
  160. */
  161. int major; /* major number of driver */
  162. int first_minor;
  163. int minors; /* maximum number of minors, =1 for
  164. * disks that can't be partitioned. */
  165. char disk_name[DISK_NAME_LEN]; /* name of major driver */
  166. unsigned short events; /* supported events */
  167. unsigned short event_flags; /* flags related to event processing */
  168. /* Array of pointers to partitions indexed by partno.
  169. * Protected with matching bdev lock but stat and other
  170. * non-critical accesses use RCU. Always access through
  171. * helpers.
  172. */
  173. struct disk_part_tbl __rcu *part_tbl;
  174. struct hd_struct part0;
  175. const struct block_device_operations *fops;
  176. struct request_queue *queue;
  177. void *private_data;
  178. int flags;
  179. unsigned long state;
  180. #define GD_NEED_PART_SCAN 0
  181. struct rw_semaphore lookup_sem;
  182. struct kobject *slave_dir;
  183. struct timer_rand_state *random;
  184. atomic_t sync_io; /* RAID */
  185. struct disk_events *ev;
  186. #ifdef CONFIG_BLK_DEV_INTEGRITY
  187. struct kobject integrity_kobj;
  188. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  189. #if IS_ENABLED(CONFIG_CDROM)
  190. struct cdrom_device_info *cdi;
  191. #endif
  192. int node_id;
  193. struct badblocks *bb;
  194. struct lockdep_map lockdep_map;
  195. ANDROID_KABI_RESERVE(1);
  196. ANDROID_KABI_RESERVE(2);
  197. ANDROID_KABI_RESERVE(3);
  198. ANDROID_KABI_RESERVE(4);
  199. };
  200. #if IS_REACHABLE(CONFIG_CDROM)
  201. #define disk_to_cdi(disk) ((disk)->cdi)
  202. #else
  203. #define disk_to_cdi(disk) NULL
  204. #endif
  205. static inline struct gendisk *part_to_disk(struct hd_struct *part)
  206. {
  207. if (likely(part)) {
  208. if (part->partno)
  209. return dev_to_disk(part_to_dev(part)->parent);
  210. else
  211. return dev_to_disk(part_to_dev(part));
  212. }
  213. return NULL;
  214. }
  215. static inline int disk_max_parts(struct gendisk *disk)
  216. {
  217. if (disk->flags & GENHD_FL_EXT_DEVT)
  218. return DISK_MAX_PARTS;
  219. return disk->minors;
  220. }
  221. static inline bool disk_part_scan_enabled(struct gendisk *disk)
  222. {
  223. return disk_max_parts(disk) > 1 &&
  224. !(disk->flags & GENHD_FL_NO_PART_SCAN);
  225. }
  226. static inline dev_t disk_devt(struct gendisk *disk)
  227. {
  228. return MKDEV(disk->major, disk->first_minor);
  229. }
  230. static inline dev_t part_devt(struct hd_struct *part)
  231. {
  232. return part_to_dev(part)->devt;
  233. }
  234. extern struct hd_struct *__disk_get_part(struct gendisk *disk, int partno);
  235. extern struct hd_struct *disk_get_part(struct gendisk *disk, int partno);
  236. static inline void disk_put_part(struct hd_struct *part)
  237. {
  238. if (likely(part))
  239. put_device(part_to_dev(part));
  240. }
  241. static inline void hd_sects_seq_init(struct hd_struct *p)
  242. {
  243. #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
  244. seqcount_init(&p->nr_sects_seq);
  245. #endif
  246. }
  247. /*
  248. * Smarter partition iterator without context limits.
  249. */
  250. #define DISK_PITER_REVERSE (1 << 0) /* iterate in the reverse direction */
  251. #define DISK_PITER_INCL_EMPTY (1 << 1) /* include 0-sized parts */
  252. #define DISK_PITER_INCL_PART0 (1 << 2) /* include partition 0 */
  253. #define DISK_PITER_INCL_EMPTY_PART0 (1 << 3) /* include empty partition 0 */
  254. struct disk_part_iter {
  255. struct gendisk *disk;
  256. struct hd_struct *part;
  257. int idx;
  258. unsigned int flags;
  259. };
  260. extern void disk_part_iter_init(struct disk_part_iter *piter,
  261. struct gendisk *disk, unsigned int flags);
  262. extern struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter);
  263. extern void disk_part_iter_exit(struct disk_part_iter *piter);
  264. extern bool disk_has_partitions(struct gendisk *disk);
  265. /* block/genhd.c */
  266. extern void device_add_disk(struct device *parent, struct gendisk *disk,
  267. const struct attribute_group **groups);
  268. static inline void add_disk(struct gendisk *disk)
  269. {
  270. device_add_disk(NULL, disk, NULL);
  271. }
  272. extern void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk);
  273. static inline void add_disk_no_queue_reg(struct gendisk *disk)
  274. {
  275. device_add_disk_no_queue_reg(NULL, disk);
  276. }
  277. extern void del_gendisk(struct gendisk *gp);
  278. extern struct gendisk *get_gendisk(dev_t dev, int *partno);
  279. extern struct block_device *bdget_disk(struct gendisk *disk, int partno);
  280. extern void set_device_ro(struct block_device *bdev, int flag);
  281. extern void set_disk_ro(struct gendisk *disk, int flag);
  282. static inline int get_disk_ro(struct gendisk *disk)
  283. {
  284. return disk->part0.policy;
  285. }
  286. extern void disk_block_events(struct gendisk *disk);
  287. extern void disk_unblock_events(struct gendisk *disk);
  288. extern void disk_flush_events(struct gendisk *disk, unsigned int mask);
  289. bool set_capacity_revalidate_and_notify(struct gendisk *disk, sector_t size,
  290. bool update_bdev);
  291. /* drivers/char/random.c */
  292. extern void add_disk_randomness(struct gendisk *disk) __latent_entropy;
  293. extern void rand_initialize_disk(struct gendisk *disk);
  294. static inline sector_t get_start_sect(struct block_device *bdev)
  295. {
  296. return bdev->bd_part->start_sect;
  297. }
  298. static inline sector_t get_capacity(struct gendisk *disk)
  299. {
  300. return disk->part0.nr_sects;
  301. }
  302. static inline void set_capacity(struct gendisk *disk, sector_t size)
  303. {
  304. disk->part0.nr_sects = size;
  305. }
  306. int bdev_disk_changed(struct block_device *bdev, bool invalidate);
  307. int blk_add_partitions(struct gendisk *disk, struct block_device *bdev);
  308. int blk_drop_partitions(struct block_device *bdev);
  309. extern struct gendisk *__alloc_disk_node(int minors, int node_id);
  310. extern struct kobject *get_disk_and_module(struct gendisk *disk);
  311. extern void put_disk(struct gendisk *disk);
  312. extern void put_disk_and_module(struct gendisk *disk);
  313. extern void blk_register_region(dev_t devt, unsigned long range,
  314. struct module *module,
  315. struct kobject *(*probe)(dev_t, int *, void *),
  316. int (*lock)(dev_t, void *),
  317. void *data);
  318. extern void blk_unregister_region(dev_t devt, unsigned long range);
  319. #define alloc_disk_node(minors, node_id) \
  320. ({ \
  321. static struct lock_class_key __key; \
  322. const char *__name; \
  323. struct gendisk *__disk; \
  324. \
  325. __name = "(gendisk_completion)"#minors"("#node_id")"; \
  326. \
  327. __disk = __alloc_disk_node(minors, node_id); \
  328. \
  329. if (__disk) \
  330. lockdep_init_map(&__disk->lockdep_map, __name, &__key, 0); \
  331. \
  332. __disk; \
  333. })
  334. #define alloc_disk(minors) alloc_disk_node(minors, NUMA_NO_NODE)
  335. int register_blkdev(unsigned int major, const char *name);
  336. void unregister_blkdev(unsigned int major, const char *name);
  337. void revalidate_disk_size(struct gendisk *disk, bool verbose);
  338. bool bdev_check_media_change(struct block_device *bdev);
  339. int __invalidate_device(struct block_device *bdev, bool kill_dirty);
  340. void bd_set_nr_sectors(struct block_device *bdev, sector_t sectors);
  341. /* for drivers/char/raw.c: */
  342. int blkdev_ioctl(struct block_device *, fmode_t, unsigned, unsigned long);
  343. long compat_blkdev_ioctl(struct file *, unsigned, unsigned long);
  344. #ifdef CONFIG_SYSFS
  345. int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk);
  346. void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk);
  347. #else
  348. static inline int bd_link_disk_holder(struct block_device *bdev,
  349. struct gendisk *disk)
  350. {
  351. return 0;
  352. }
  353. static inline void bd_unlink_disk_holder(struct block_device *bdev,
  354. struct gendisk *disk)
  355. {
  356. }
  357. #endif /* CONFIG_SYSFS */
  358. #ifdef CONFIG_BLOCK
  359. void printk_all_partitions(void);
  360. dev_t blk_lookup_devt(const char *name, int partno);
  361. #else /* CONFIG_BLOCK */
  362. static inline void printk_all_partitions(void)
  363. {
  364. }
  365. static inline dev_t blk_lookup_devt(const char *name, int partno)
  366. {
  367. dev_t devt = MKDEV(0, 0);
  368. return devt;
  369. }
  370. #endif /* CONFIG_BLOCK */
  371. #endif /* _LINUX_GENHD_H */