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