genhd.h 12.5 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12
#ifndef _LINUX_GENHD_H
#define _LINUX_GENHD_H

/*
 * 	genhd.h Copyright (C) 1992 Drew Eckhardt
 *	Generic hard disk header file by  
 * 		Drew Eckhardt
 *
 *		<drew@colorado.edu>
 */

13
#include <linux/kabi.h>
L
Linus Torvalds 已提交
14
#include <linux/types.h>
15
#include <linux/kdev_t.h>
16
#include <linux/rcupdate.h>
17
#include <linux/slab.h>
M
Ming Lei 已提交
18
#include <linux/percpu-refcount.h>
19
#include <linux/uuid.h>
20
#include <linux/blk_types.h>
21
#include <asm/local.h>
L
Linus Torvalds 已提交
22

T
Tejun Heo 已提交
23
#define dev_to_disk(device)	container_of((device), struct gendisk, part0.__dev)
24
#define dev_to_part(device)	container_of((device), struct hd_struct, __dev)
T
Tejun Heo 已提交
25
#define disk_to_dev(disk)	(&(disk)->part0.__dev)
26
#define part_to_dev(part)	(&((part)->__dev))
27

28
extern const struct device_type disk_type;
29 30 31
extern struct device_type part_type;
extern struct class block_class;

32
#define DISK_MAX_PARTS			256
33
#define DISK_NAME_LEN			32
34

35 36 37 38 39
#include <linux/major.h>
#include <linux/device.h>
#include <linux/smp.h>
#include <linux/string.h>
#include <linux/fs.h>
40
#include <linux/workqueue.h>
41

42
#define PARTITION_META_INFO_VOLNAMELTH	64
43 44 45 46
/*
 * Enough for the string representation of any kind of UUID plus NULL.
 * EFI UUID is 36 characters. MSDOS UUID is 11 characters.
 */
47
#define PARTITION_META_INFO_UUIDLTH	(UUID_STRING_LEN + 1)
48 49

struct partition_meta_info {
50
	char uuid[PARTITION_META_INFO_UUIDLTH];
51 52 53
	u8 volname[PARTITION_META_INFO_VOLNAMELTH];
};

L
Linus Torvalds 已提交
54 55
struct hd_struct {
	sector_t start_sect;
56 57 58 59 60
	/*
	 * nr_sects is protected by sequence counter. One might extend a
	 * partition while IO is happening to it and update of nr_sects
	 * can be non-atomic on 32bit machines with 64bit sector_t.
	 */
L
Linus Torvalds 已提交
61
	sector_t nr_sects;
62
#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
63
	seqcount_t nr_sects_seq;
64
#endif
65 66 67 68
	unsigned long stamp;
	struct disk_stats __percpu *dkstats;
	struct percpu_ref ref;

69
	struct device __dev;
70
	struct kobject *holder_dir;
71 72
	bool read_only;
	int partno;
73
	struct partition_meta_info *info;
74 75
#ifdef CONFIG_FAIL_MAKE_REQUEST
	int make_it_fail;
76
#endif
77
	struct rcu_work rcu_work;
78 79 80 81 82

	KABI_RESERVE(1)
	KABI_RESERVE(2)
	KABI_RESERVE(3)
	KABI_RESERVE(4)
L
Linus Torvalds 已提交
83 84
};

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
/**
 * DOC: genhd capability flags
 *
 * ``GENHD_FL_REMOVABLE`` (0x0001): indicates that the block device
 * gives access to removable media.
 * When set, the device remains present even when media is not
 * inserted.
 * Must not be set for devices which are removed entirely when the
 * media is removed.
 *
 * ``GENHD_FL_CD`` (0x0008): the block device is a CD-ROM-style
 * device.
 * Affects responses to the ``CDROM_GET_CAPABILITY`` ioctl.
 *
 * ``GENHD_FL_UP`` (0x0010): indicates that the block device is "up",
 * with a similar meaning to network interfaces.
 *
 * ``GENHD_FL_SUPPRESS_PARTITION_INFO`` (0x0020): don't include
 * partition information in ``/proc/partitions`` or in the output of
 * printk_all_partitions().
 * Used for the null block device and some MMC devices.
 *
 * ``GENHD_FL_EXT_DEVT`` (0x0040): the driver supports extended
 * dynamic ``dev_t``, i.e. it wants extended device numbers
 * (``BLOCK_EXT_MAJOR``).
 * This affects the maximum number of partitions.
 *
 * ``GENHD_FL_NATIVE_CAPACITY`` (0x0080): based on information in the
 * partition table, the device's capacity has been extended to its
 * native capacity; i.e. the device has hidden capacity used by one
 * of the partitions (this is a flag used so that native capacity is
 * only ever unlocked once).
 *
 * ``GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE`` (0x0100): event polling is
 * blocked whenever a writer holds an exclusive lock.
 *
 * ``GENHD_FL_NO_PART_SCAN`` (0x0200): partition scanning is disabled.
 * Used for loop devices in their default settings and some MMC
 * devices.
 *
 * ``GENHD_FL_HIDDEN`` (0x0400): the block device is hidden; it
 * doesn't produce events, doesn't appear in sysfs, and doesn't have
 * an associated ``bdev``.
 * Implies ``GENHD_FL_SUPPRESS_PARTITION_INFO`` and
 * ``GENHD_FL_NO_PART_SCAN``.
 * Used for multipath devices.
 */
#define GENHD_FL_REMOVABLE			0x0001
/* 2 is unused (used to be GENHD_FL_DRIVERFS) */
/* 4 is unused (used to be GENHD_FL_MEDIA_CHANGE_NOTIFY) */
#define GENHD_FL_CD				0x0008
#define GENHD_FL_UP				0x0010
#define GENHD_FL_SUPPRESS_PARTITION_INFO	0x0020
#define GENHD_FL_EXT_DEVT			0x0040
#define GENHD_FL_NATIVE_CAPACITY		0x0080
#define GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE	0x0100
#define GENHD_FL_NO_PART_SCAN			0x0200
#define GENHD_FL_HIDDEN				0x0400
L
Linus Torvalds 已提交
143

144 145 146 147 148
enum {
	DISK_EVENT_MEDIA_CHANGE			= 1 << 0, /* media changed */
	DISK_EVENT_EJECT_REQUEST		= 1 << 1, /* eject requested */
};

149 150 151 152 153 154 155
enum {
	/* Poll even if events_poll_msecs is unset */
	DISK_EVENT_FLAG_POLL			= 1 << 0,
	/* Forward events to udev */
	DISK_EVENT_FLAG_UEVENT			= 1 << 1,
};

T
Tejun Heo 已提交
156 157 158
struct disk_part_tbl {
	struct rcu_head rcu_head;
	int len;
A
Arnd Bergmann 已提交
159 160
	struct hd_struct __rcu *last_lookup;
	struct hd_struct __rcu *part[];
T
Tejun Heo 已提交
161 162
};

163
struct disk_events;
164
struct badblocks;
165

166
struct blk_integrity {
167 168 169 170 171
	const struct blk_integrity_profile	*profile;
	unsigned char				flags;
	unsigned char				tuple_size;
	unsigned char				interval_exp;
	unsigned char				tag_size;
172 173 174

	KABI_RESERVE(1)
	KABI_RESERVE(2)
175 176
};

L
Linus Torvalds 已提交
177
struct gendisk {
178 179
	/* major, first_minor and minors are input parameters only,
	 * don't use directly.  Use disk_devt() and disk_max_parts().
180
	 */
L
Linus Torvalds 已提交
181 182 183 184
	int major;			/* major number of driver */
	int first_minor;
	int minors;                     /* maximum number of minors, =1 for
                                         * disks that can't be partitioned. */
185

186
	char disk_name[DISK_NAME_LEN];	/* name of major driver */
187

188 189
	unsigned short events;		/* supported events */
	unsigned short event_flags;	/* flags related to event processing */
190

T
Tejun Heo 已提交
191
	/* Array of pointers to partitions indexed by partno.
192 193 194 195
	 * Protected with matching bdev lock but stat and other
	 * non-critical accesses use RCU.  Always access through
	 * helpers.
	 */
A
Arnd Bergmann 已提交
196
	struct disk_part_tbl __rcu *part_tbl;
T
Tejun Heo 已提交
197
	struct hd_struct part0;
198
	DECLARE_BITMAP(user_ro_bitmap, DISK_MAX_PARTS);
199

200
	const struct block_device_operations *fops;
L
Linus Torvalds 已提交
201 202 203 204
	struct request_queue *queue;
	void *private_data;

	int flags;
205 206
	unsigned long state;
#define GD_NEED_PART_SCAN		0
J
Jan Kara 已提交
207
	struct rw_semaphore lookup_sem;
208
	struct kobject *slave_dir;
L
Linus Torvalds 已提交
209 210

	struct timer_rand_state *random;
211
	atomic64_t sync_io_sectors;     /* RAID */
212
	struct disk_events *ev;
213
#ifdef  CONFIG_BLK_DEV_INTEGRITY
214
	struct kobject integrity_kobj;
215
#endif	/* CONFIG_BLK_DEV_INTEGRITY */
216 217 218
#if IS_ENABLED(CONFIG_CDROM)
	struct cdrom_device_info *cdi;
#endif
T
Tejun Heo 已提交
219
	int node_id;
220
	struct badblocks *bb;
221
	struct lockdep_map lockdep_map;
222 223 224 225 226

	KABI_RESERVE(1)
	KABI_RESERVE(2)
	KABI_RESERVE(3)
	KABI_RESERVE(4)
L
Linus Torvalds 已提交
227 228
};

229 230 231 232 233 234
#if IS_REACHABLE(CONFIG_CDROM)
#define disk_to_cdi(disk)	((disk)->cdi)
#else
#define disk_to_cdi(disk)	NULL
#endif

T
Tejun Heo 已提交
235 236
static inline struct gendisk *part_to_disk(struct hd_struct *part)
{
T
Tejun Heo 已提交
237 238 239 240 241 242
	if (likely(part)) {
		if (part->partno)
			return dev_to_disk(part_to_dev(part)->parent);
		else
			return dev_to_disk(part_to_dev(part));
	}
T
Tejun Heo 已提交
243 244 245
	return NULL;
}

246 247
static inline int disk_max_parts(struct gendisk *disk)
{
248 249 250
	if (disk->flags & GENHD_FL_EXT_DEVT)
		return DISK_MAX_PARTS;
	return disk->minors;
T
Tejun Heo 已提交
251 252
}

T
Tejun Heo 已提交
253
static inline bool disk_part_scan_enabled(struct gendisk *disk)
T
Tejun Heo 已提交
254
{
T
Tejun Heo 已提交
255 256
	return disk_max_parts(disk) > 1 &&
		!(disk->flags & GENHD_FL_NO_PART_SCAN);
257 258 259 260
}

static inline dev_t disk_devt(struct gendisk *disk)
{
261
	return MKDEV(disk->major, disk->first_minor);
262 263 264 265
}

static inline dev_t part_devt(struct hd_struct *part)
{
266
	return part_to_dev(part)->devt;
267 268
}

269
extern struct hd_struct *__disk_get_part(struct gendisk *disk, int partno);
270 271 272 273 274
extern struct hd_struct *disk_get_part(struct gendisk *disk, int partno);

static inline void disk_put_part(struct hd_struct *part)
{
	if (likely(part))
275
		put_device(part_to_dev(part));
276 277
}

278 279 280 281 282 283 284
static inline void hd_sects_seq_init(struct hd_struct *p)
{
#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
	seqcount_init(&p->nr_sects_seq);
#endif
}

285 286 287 288 289
/*
 * Smarter partition iterator without context limits.
 */
#define DISK_PITER_REVERSE	(1 << 0) /* iterate in the reverse direction */
#define DISK_PITER_INCL_EMPTY	(1 << 1) /* include 0-sized parts */
T
Tejun Heo 已提交
290
#define DISK_PITER_INCL_PART0	(1 << 2) /* include partition 0 */
291
#define DISK_PITER_INCL_EMPTY_PART0 (1 << 3) /* include empty partition 0 */
292 293 294 295 296 297 298 299 300 301 302 303

struct disk_part_iter {
	struct gendisk		*disk;
	struct hd_struct	*part;
	int			idx;
	unsigned int		flags;
};

extern void disk_part_iter_init(struct disk_part_iter *piter,
				 struct gendisk *disk, unsigned int flags);
extern struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter);
extern void disk_part_iter_exit(struct disk_part_iter *piter);
304
extern bool disk_has_partitions(struct gendisk *disk);
L
Linus Torvalds 已提交
305

306
/* block/genhd.c */
307 308
extern void device_add_disk(struct device *parent, struct gendisk *disk,
			    const struct attribute_group **groups);
309 310
static inline void add_disk(struct gendisk *disk)
{
311
	device_add_disk(NULL, disk, NULL);
312
}
313 314 315 316 317
extern void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk);
static inline void add_disk_no_queue_reg(struct gendisk *disk)
{
	device_add_disk_no_queue_reg(NULL, disk);
}
318

L
Linus Torvalds 已提交
319
extern void del_gendisk(struct gendisk *gp);
320
extern struct gendisk *get_gendisk(dev_t dev, int *partno);
321
extern struct block_device *bdget_disk(struct gendisk *disk, int partno);
L
Linus Torvalds 已提交
322

323 324 325
extern void set_device_ro(struct block_device *bdev, bool state);
extern void set_disk_ro(struct gendisk *disk, bool state);
extern bool get_user_ro(struct gendisk *disk, unsigned int partno);
L
Linus Torvalds 已提交
326

T
Tejun Heo 已提交
327 328
static inline int get_disk_ro(struct gendisk *disk)
{
329
	return disk->part0.read_only;
T
Tejun Heo 已提交
330 331
}

332 333
extern void disk_block_events(struct gendisk *disk);
extern void disk_unblock_events(struct gendisk *disk);
334
extern void disk_flush_events(struct gendisk *disk, unsigned int mask);
335
bool set_capacity_revalidate_and_notify(struct gendisk *disk, sector_t size,
336
		bool update_bdev);
337

L
Linus Torvalds 已提交
338
/* drivers/char/random.c */
339
extern void add_disk_randomness(struct gendisk *disk) __latent_entropy;
L
Linus Torvalds 已提交
340 341 342 343
extern void rand_initialize_disk(struct gendisk *disk);

static inline sector_t get_start_sect(struct block_device *bdev)
{
T
Tejun Heo 已提交
344
	return bdev->bd_part->start_sect;
L
Linus Torvalds 已提交
345 346 347
}
static inline sector_t get_capacity(struct gendisk *disk)
{
348
	return disk->part0.nr_sects;
L
Linus Torvalds 已提交
349 350 351
}
static inline void set_capacity(struct gendisk *disk, sector_t size)
{
352
	disk->part0.nr_sects = size;
L
Linus Torvalds 已提交
353 354
}

355
int bdev_disk_changed(struct block_device *bdev, bool invalidate);
356
int blk_add_partitions(struct gendisk *disk, struct block_device *bdev);
357
int blk_drop_partitions(struct block_device *bdev);
L
Linus Torvalds 已提交
358

359
extern struct gendisk *__alloc_disk_node(int minors, int node_id);
360
extern struct kobject *get_disk_and_module(struct gendisk *disk);
L
Linus Torvalds 已提交
361
extern void put_disk(struct gendisk *disk);
362
extern void put_disk_and_module(struct gendisk *disk);
363
extern void blk_register_region(dev_t devt, unsigned long range,
L
Linus Torvalds 已提交
364 365 366 367
			struct module *module,
			struct kobject *(*probe)(dev_t, int *, void *),
			int (*lock)(dev_t, void *),
			void *data);
368
extern void blk_unregister_region(dev_t devt, unsigned long range);
369 370
extern void blk_delete_region(dev_t devt, unsigned long range,
			struct kobject *(*probe)(dev_t, int *, void *));
L
Linus Torvalds 已提交
371

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
#define alloc_disk_node(minors, node_id)				\
({									\
	static struct lock_class_key __key;				\
	const char *__name;						\
	struct gendisk *__disk;						\
									\
	__name = "(gendisk_completion)"#minors"("#node_id")";		\
									\
	__disk = __alloc_disk_node(minors, node_id);			\
									\
	if (__disk)							\
		lockdep_init_map(&__disk->lockdep_map, __name, &__key, 0); \
									\
	__disk;								\
})

#define alloc_disk(minors) alloc_disk_node(minors, NUMA_NO_NODE)

390 391 392
int register_blkdev(unsigned int major, const char *name);
void unregister_blkdev(unsigned int major, const char *name);

393
void revalidate_disk_size(struct gendisk *disk, bool verbose);
394
bool bdev_check_media_change(struct block_device *bdev);
395
int __invalidate_device(struct block_device *bdev, bool kill_dirty);
396
void bd_set_nr_sectors(struct block_device *bdev, sector_t sectors);
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

/* for drivers/char/raw.c: */
int blkdev_ioctl(struct block_device *, fmode_t, unsigned, unsigned long);
long compat_blkdev_ioctl(struct file *, unsigned, unsigned long);

#ifdef CONFIG_SYSFS
int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk);
void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk);
#else
static inline int bd_link_disk_holder(struct block_device *bdev,
				      struct gendisk *disk)
{
	return 0;
}
static inline void bd_unlink_disk_holder(struct block_device *bdev,
					 struct gendisk *disk)
{
}
#endif /* CONFIG_SYSFS */

417 418 419
#ifdef CONFIG_BLOCK
void printk_all_partitions(void);
dev_t blk_lookup_devt(const char *name, int partno);
420
#else /* CONFIG_BLOCK */
421 422 423
static inline void printk_all_partitions(void)
{
}
424
static inline dev_t blk_lookup_devt(const char *name, int partno)
425 426 427 428
{
	dev_t devt = MKDEV(0, 0);
	return devt;
}
429
#endif /* CONFIG_BLOCK */
430

431
#endif /* _LINUX_GENHD_H */