genhd.h 9.9 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13
#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>
 */

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

22
extern const struct device_type disk_type;
23 24 25
extern struct device_type part_type;
extern struct class block_class;

26
#define DISK_MAX_PARTS			256
27
#define DISK_NAME_LEN			32
28

29 30 31 32 33
#include <linux/major.h>
#include <linux/device.h>
#include <linux/smp.h>
#include <linux/string.h>
#include <linux/fs.h>
34
#include <linux/workqueue.h>
35
#include <linux/xarray.h>
36

37
#define PARTITION_META_INFO_VOLNAMELTH	64
38 39 40 41
/*
 * Enough for the string representation of any kind of UUID plus NULL.
 * EFI UUID is 36 characters. MSDOS UUID is 11 characters.
 */
42
#define PARTITION_META_INFO_UUIDLTH	(UUID_STRING_LEN + 1)
43 44

struct partition_meta_info {
45
	char uuid[PARTITION_META_INFO_UUIDLTH];
46 47 48
	u8 volname[PARTITION_META_INFO_VOLNAMELTH];
};

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
/**
 * 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 已提交
107

108 109 110 111 112
enum {
	DISK_EVENT_MEDIA_CHANGE			= 1 << 0, /* media changed */
	DISK_EVENT_EJECT_REQUEST		= 1 << 1, /* eject requested */
};

113 114 115 116 117 118 119
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,
};

120
struct disk_events;
121
struct badblocks;
122

123
struct blk_integrity {
124 125 126 127 128
	const struct blk_integrity_profile	*profile;
	unsigned char				flags;
	unsigned char				tuple_size;
	unsigned char				interval_exp;
	unsigned char				tag_size;
129 130
};

L
Linus Torvalds 已提交
131
struct gendisk {
132 133
	/* major, first_minor and minors are input parameters only,
	 * don't use directly.  Use disk_devt() and disk_max_parts().
134
	 */
L
Linus Torvalds 已提交
135 136 137 138
	int major;			/* major number of driver */
	int first_minor;
	int minors;                     /* maximum number of minors, =1 for
                                         * disks that can't be partitioned. */
139

140
	char disk_name[DISK_NAME_LEN];	/* name of major driver */
141

142 143
	unsigned short events;		/* supported events */
	unsigned short event_flags;	/* flags related to event processing */
144

145
	struct xarray part_tbl;
146
	struct block_device *part0;
147

148
	const struct block_device_operations *fops;
L
Linus Torvalds 已提交
149 150 151 152
	struct request_queue *queue;
	void *private_data;

	int flags;
153 154
	unsigned long state;
#define GD_NEED_PART_SCAN		0
155
#define GD_READ_ONLY			1
156
#define GD_QUEUE_REF			2
157 158

	struct mutex open_mutex;	/* open/close mutex */
159
	unsigned open_partitions;	/* number of open partitions */
160

161
	struct kobject *slave_dir;
L
Linus Torvalds 已提交
162 163 164

	struct timer_rand_state *random;
	atomic_t sync_io;		/* RAID */
165
	struct disk_events *ev;
166
#ifdef  CONFIG_BLK_DEV_INTEGRITY
167
	struct kobject integrity_kobj;
168
#endif	/* CONFIG_BLK_DEV_INTEGRITY */
169 170 171
#if IS_ENABLED(CONFIG_CDROM)
	struct cdrom_device_info *cdi;
#endif
T
Tejun Heo 已提交
172
	int node_id;
173
	struct badblocks *bb;
174
	struct lockdep_map lockdep_map;
L
Linus Torvalds 已提交
175 176
};

177 178 179 180 181 182 183 184 185
/*
 * The gendisk is refcounted by the part0 block_device, and the bd_device
 * therein is also used for device model presentation in sysfs.
 */
#define dev_to_disk(device) \
	(dev_to_bdev(device)->bd_disk)
#define disk_to_dev(disk) \
	(&((disk)->part0->bd_device))

186 187 188 189 190 191
#if IS_REACHABLE(CONFIG_CDROM)
#define disk_to_cdi(disk)	((disk)->cdi)
#else
#define disk_to_cdi(disk)	NULL
#endif

192 193
static inline int disk_max_parts(struct gendisk *disk)
{
194 195 196
	if (disk->flags & GENHD_FL_EXT_DEVT)
		return DISK_MAX_PARTS;
	return disk->minors;
T
Tejun Heo 已提交
197 198
}

T
Tejun Heo 已提交
199
static inline bool disk_part_scan_enabled(struct gendisk *disk)
T
Tejun Heo 已提交
200
{
T
Tejun Heo 已提交
201 202
	return disk_max_parts(disk) > 1 &&
		!(disk->flags & GENHD_FL_NO_PART_SCAN);
203 204 205 206
}

static inline dev_t disk_devt(struct gendisk *disk)
{
207
	return MKDEV(disk->major, disk->first_minor);
208 209
}

210 211
void disk_uevent(struct gendisk *disk, enum kobject_action action);

212
/* block/genhd.c */
213 214
extern void device_add_disk(struct device *parent, struct gendisk *disk,
			    const struct attribute_group **groups);
215 216
static inline void add_disk(struct gendisk *disk)
{
217
	device_add_disk(NULL, disk, NULL);
218
}
219 220 221 222 223
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);
}
224

L
Linus Torvalds 已提交
225
extern void del_gendisk(struct gendisk *gp);
226
extern struct block_device *bdget_disk(struct gendisk *disk, int partno);
L
Linus Torvalds 已提交
227

228
void set_disk_ro(struct gendisk *disk, bool read_only);
L
Linus Torvalds 已提交
229

T
Tejun Heo 已提交
230 231
static inline int get_disk_ro(struct gendisk *disk)
{
232 233
	return disk->part0->bd_read_only ||
		test_bit(GD_READ_ONLY, &disk->state);
T
Tejun Heo 已提交
234 235
}

236 237
extern void disk_block_events(struct gendisk *disk);
extern void disk_unblock_events(struct gendisk *disk);
238
extern void disk_flush_events(struct gendisk *disk, unsigned int mask);
239
bool set_capacity_and_notify(struct gendisk *disk, sector_t size);
240

L
Linus Torvalds 已提交
241
/* drivers/char/random.c */
242
extern void add_disk_randomness(struct gendisk *disk) __latent_entropy;
L
Linus Torvalds 已提交
243 244 245 246
extern void rand_initialize_disk(struct gendisk *disk);

static inline sector_t get_start_sect(struct block_device *bdev)
{
247
	return bdev->bd_start_sect;
L
Linus Torvalds 已提交
248
}
249 250

static inline sector_t bdev_nr_sectors(struct block_device *bdev)
L
Linus Torvalds 已提交
251
{
252
	return i_size_read(bdev->bd_inode) >> 9;
L
Linus Torvalds 已提交
253
}
254 255

static inline sector_t get_capacity(struct gendisk *disk)
L
Linus Torvalds 已提交
256
{
257
	return bdev_nr_sectors(disk->part0);
L
Linus Torvalds 已提交
258 259
}

260
int bdev_disk_changed(struct block_device *bdev, bool invalidate);
261
int blk_add_partitions(struct gendisk *disk, struct block_device *bdev);
262
void blk_drop_partitions(struct gendisk *disk);
L
Linus Torvalds 已提交
263

264
extern struct gendisk *__alloc_disk_node(int minors, int node_id);
L
Linus Torvalds 已提交
265 266
extern void put_disk(struct gendisk *disk);

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
#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)

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
/**
 * blk_alloc_disk - allocate a gendisk structure
 * @node_id: numa node to allocate on
 *
 * Allocate and pre-initialize a gendisk structure for use with BIO based
 * drivers.
 *
 * Context: can sleep
 */
#define blk_alloc_disk(node_id)						\
({									\
	struct gendisk *__disk = __blk_alloc_disk(node_id);		\
	static struct lock_class_key __key;				\
									\
	if (__disk)							\
		lockdep_init_map(&__disk->lockdep_map,			\
			"(bio completion)", &__key, 0);			\
	__disk;								\
})
struct gendisk *__blk_alloc_disk(int node);
void blk_cleanup_disk(struct gendisk *disk);

307 308 309 310
int __register_blkdev(unsigned int major, const char *name,
		void (*probe)(dev_t devt));
#define register_blkdev(major, name) \
	__register_blkdev(major, name, NULL)
311 312
void unregister_blkdev(unsigned int major, const char *name);

313
bool bdev_check_media_change(struct block_device *bdev);
314
int __invalidate_device(struct block_device *bdev, bool kill_dirty);
315
void set_capacity(struct gendisk *disk, sector_t size);
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

/* 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 */

C
Christoph Hellwig 已提交
336
dev_t blk_lookup_devt(const char *name, int partno);
337
void blk_request_module(dev_t devt);
338 339
#ifdef CONFIG_BLOCK
void printk_all_partitions(void);
340
#else /* CONFIG_BLOCK */
341 342 343
static inline void printk_all_partitions(void)
{
}
344
#endif /* CONFIG_BLOCK */
345

346
#endif /* _LINUX_GENHD_H */