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#ifdef CONFIG_BLOCK
23
24#define dev_to_disk(device) container_of((device), struct gendisk, part0.__dev)
25#define dev_to_part(device) container_of((device), struct hd_struct, __dev)
26#define disk_to_dev(disk) (&(disk)->part0.__dev)
27#define part_to_dev(part) (&((part)->__dev))
28
29extern struct device_type part_type;
30extern struct kobject *block_depr;
31extern struct class block_class;
32
33enum {
34/* These three have identical behaviour; use the second one if DOS FDISK gets
35 confused about extended/logical partitions starting past cylinder 1023. */
36 DOS_EXTENDED_PARTITION = 5,
37 LINUX_EXTENDED_PARTITION = 0x85,
38 WIN98_EXTENDED_PARTITION = 0x0f,
39
40 SUN_WHOLE_DISK = DOS_EXTENDED_PARTITION,
41
42 LINUX_SWAP_PARTITION = 0x82,
43 LINUX_DATA_PARTITION = 0x83,
44 LINUX_LVM_PARTITION = 0x8e,
45 LINUX_RAID_PARTITION = 0xfd, /* autodetect RAID partition */
46
47 SOLARIS_X86_PARTITION = LINUX_SWAP_PARTITION,
48 NEW_SOLARIS_X86_PARTITION = 0xbf,
49
50 DM6_AUX1PARTITION = 0x51, /* no DDO: use xlated geom */
51 DM6_AUX3PARTITION = 0x53, /* no DDO: use xlated geom */
52 DM6_PARTITION = 0x54, /* has DDO: use xlated geom & offset */
53 EZD_PARTITION = 0x55, /* EZ-DRIVE */
54
55 FREEBSD_PARTITION = 0xa5, /* FreeBSD Partition ID */
56 OPENBSD_PARTITION = 0xa6, /* OpenBSD Partition ID */
57 NETBSD_PARTITION = 0xa9, /* NetBSD Partition ID */
58 BSDI_PARTITION = 0xb7, /* BSDI Partition ID */
59 MINIX_PARTITION = 0x81, /* Minix Partition ID */
60 UNIXWARE_PARTITION = 0x63, /* Same as GNU_HURD and SCO Unix */
61};
62
63#define DISK_MAX_PARTS 256
64#define DISK_NAME_LEN 32
65
66#include <linux/major.h>
67#include <linux/device.h>
68#include <linux/smp.h>
69#include <linux/string.h>
70#include <linux/fs.h>
71#include <linux/workqueue.h>
72
73struct partition {
74 unsigned char boot_ind; /* 0x80 - active */
75 unsigned char head; /* starting head */
76 unsigned char sector; /* starting sector */
77 unsigned char cyl; /* starting cylinder */
78 unsigned char sys_ind; /* What partition type */
79 unsigned char end_head; /* end head */
80 unsigned char end_sector; /* end sector */
81 unsigned char end_cyl; /* end cylinder */
82 __le32 start_sect; /* starting sector counting from 0 */
83 __le32 nr_sects; /* nr of sectors in partition */
84} __attribute__((packed));
85
86struct disk_stats {
87 u64 nsecs[NR_STAT_GROUPS];
88 unsigned long sectors[NR_STAT_GROUPS];
89 unsigned long ios[NR_STAT_GROUPS];
90 unsigned long merges[NR_STAT_GROUPS];
91 unsigned long io_ticks;
92 unsigned long time_in_queue;
93 local_t in_flight[2];
94};
95
96#define PARTITION_META_INFO_VOLNAMELTH 64
97/*
98 * Enough for the string representation of any kind of UUID plus NULL.
99 * EFI UUID is 36 characters. MSDOS UUID is 11 characters.
100 */
101#define PARTITION_META_INFO_UUIDLTH (UUID_STRING_LEN + 1)
102
103struct partition_meta_info {
104 char uuid[PARTITION_META_INFO_UUIDLTH];
105 u8 volname[PARTITION_META_INFO_VOLNAMELTH];
106};
107
108struct hd_struct {
109 sector_t start_sect;
110 /*
111 * nr_sects is protected by sequence counter. One might extend a
112 * partition while IO is happening to it and update of nr_sects
113 * can be non-atomic on 32bit machines with 64bit sector_t.
114 */
115 sector_t nr_sects;
116 seqcount_t nr_sects_seq;
117 sector_t alignment_offset;
118 unsigned int discard_alignment;
119 struct device __dev;
120 struct kobject *holder_dir;
121 int policy, partno;
122 struct partition_meta_info *info;
123#ifdef CONFIG_FAIL_MAKE_REQUEST
124 int make_it_fail;
125#endif
126 unsigned long stamp;
127#ifdef CONFIG_SMP
128 struct disk_stats __percpu *dkstats;
129#else
130 struct disk_stats dkstats;
131#endif
132 struct percpu_ref ref;
133 struct rcu_work rcu_work;
134};
135
136#define GENHD_FL_REMOVABLE 1
137/* 2 is unused */
138#define GENHD_FL_MEDIA_CHANGE_NOTIFY 4
139#define GENHD_FL_CD 8
140#define GENHD_FL_UP 16
141#define GENHD_FL_SUPPRESS_PARTITION_INFO 32
142#define GENHD_FL_EXT_DEVT 64 /* allow extended devt */
143#define GENHD_FL_NATIVE_CAPACITY 128
144#define GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE 256
145#define GENHD_FL_NO_PART_SCAN 512
146#define GENHD_FL_HIDDEN 1024
147
148enum {
149 DISK_EVENT_MEDIA_CHANGE = 1 << 0, /* media changed */
150 DISK_EVENT_EJECT_REQUEST = 1 << 1, /* eject requested */
151};
152
153struct disk_part_tbl {
154 struct rcu_head rcu_head;
155 int len;
156 struct hd_struct __rcu *last_lookup;
157 struct hd_struct __rcu *part[];
158};
159
160struct disk_events;
161struct badblocks;
162
163#if defined(CONFIG_BLK_DEV_INTEGRITY)
164
165struct blk_integrity {
166 const struct blk_integrity_profile *profile;
167 unsigned char flags;
168 unsigned char tuple_size;
169 unsigned char interval_exp;
170 unsigned char tag_size;
171};
172
173#endif /* CONFIG_BLK_DEV_INTEGRITY */
174
175struct gendisk {
176 /* major, first_minor and minors are input parameters only,
177 * don't use directly. Use disk_devt() and disk_max_parts().
178 */
179 int major; /* major number of driver */
180 int first_minor;
181 int minors; /* maximum number of minors, =1 for
182 * disks that can't be partitioned. */
183
184 char disk_name[DISK_NAME_LEN]; /* name of major driver */
185 char *(*devnode)(struct gendisk *gd, umode_t *mode);
186
187 unsigned int events; /* supported events */
188 unsigned int async_events; /* async events, subset of all */
189
190 /* Array of pointers to partitions indexed by partno.
191 * Protected with matching bdev lock but stat and other
192 * non-critical accesses use RCU. Always access through
193 * helpers.
194 */
195 struct disk_part_tbl __rcu *part_tbl;
196 struct hd_struct part0;
197
198 const struct block_device_operations *fops;
199 struct request_queue *queue;
200 void *private_data;
201
202 int flags;
203 struct rw_semaphore lookup_sem;
204 struct kobject *slave_dir;
205
206 struct timer_rand_state *random;
207 atomic_t sync_io; /* RAID */
208 struct disk_events *ev;
209#ifdef CONFIG_BLK_DEV_INTEGRITY
210 struct kobject integrity_kobj;
211#endif /* CONFIG_BLK_DEV_INTEGRITY */
212 int node_id;
213 struct badblocks *bb;
214 struct lockdep_map lockdep_map;
215};
216
217static inline struct gendisk *part_to_disk(struct hd_struct *part)
218{
219 if (likely(part)) {
220 if (part->partno)
221 return dev_to_disk(part_to_dev(part)->parent);
222 else
223 return dev_to_disk(part_to_dev(part));
224 }
225 return NULL;
226}
227
228static inline int disk_max_parts(struct gendisk *disk)
229{
230 if (disk->flags & GENHD_FL_EXT_DEVT)
231 return DISK_MAX_PARTS;
232 return disk->minors;
233}
234
235static inline bool disk_part_scan_enabled(struct gendisk *disk)
236{
237 return disk_max_parts(disk) > 1 &&
238 !(disk->flags & GENHD_FL_NO_PART_SCAN);
239}
240
241static inline dev_t disk_devt(struct gendisk *disk)
242{
243 return MKDEV(disk->major, disk->first_minor);
244}
245
246static inline dev_t part_devt(struct hd_struct *part)
247{
248 return part_to_dev(part)->devt;
249}
250
251extern struct hd_struct *__disk_get_part(struct gendisk *disk, int partno);
252extern struct hd_struct *disk_get_part(struct gendisk *disk, int partno);
253
254static inline void disk_put_part(struct hd_struct *part)
255{
256 if (likely(part))
257 put_device(part_to_dev(part));
258}
259
260/*
261 * Smarter partition iterator without context limits.
262 */
263#define DISK_PITER_REVERSE (1 << 0) /* iterate in the reverse direction */
264#define DISK_PITER_INCL_EMPTY (1 << 1) /* include 0-sized parts */
265#define DISK_PITER_INCL_PART0 (1 << 2) /* include partition 0 */
266#define DISK_PITER_INCL_EMPTY_PART0 (1 << 3) /* include empty partition 0 */
267
268struct disk_part_iter {
269 struct gendisk *disk;
270 struct hd_struct *part;
271 int idx;
272 unsigned int flags;
273};
274
275extern void disk_part_iter_init(struct disk_part_iter *piter,
276 struct gendisk *disk, unsigned int flags);
277extern struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter);
278extern void disk_part_iter_exit(struct disk_part_iter *piter);
279
280extern struct hd_struct *disk_map_sector_rcu(struct gendisk *disk,
281 sector_t sector);
282
283/*
284 * Macros to operate on percpu disk statistics:
285 *
286 * {disk|part|all}_stat_{add|sub|inc|dec}() modify the stat counters
287 * and should be called between disk_stat_lock() and
288 * disk_stat_unlock().
289 *
290 * part_stat_read() can be called at any time.
291 *
292 * part_stat_{add|set_all}() and {init|free}_part_stats are for
293 * internal use only.
294 */
295#ifdef CONFIG_SMP
296#define part_stat_lock() ({ rcu_read_lock(); get_cpu(); })
297#define part_stat_unlock() do { put_cpu(); rcu_read_unlock(); } while (0)
298
299#define part_stat_get_cpu(part, field, cpu) \
300 (per_cpu_ptr((part)->dkstats, (cpu))->field)
301
302#define part_stat_get(part, field) \
303 part_stat_get_cpu(part, field, smp_processor_id())
304
305#define part_stat_read(part, field) \
306({ \
307 typeof((part)->dkstats->field) res = 0; \
308 unsigned int _cpu; \
309 for_each_possible_cpu(_cpu) \
310 res += per_cpu_ptr((part)->dkstats, _cpu)->field; \
311 res; \
312})
313
314static inline void part_stat_set_all(struct hd_struct *part, int value)
315{
316 int i;
317
318 for_each_possible_cpu(i)
319 memset(per_cpu_ptr(part->dkstats, i), value,
320 sizeof(struct disk_stats));
321}
322
323static inline int init_part_stats(struct hd_struct *part)
324{
325 part->dkstats = alloc_percpu(struct disk_stats);
326 if (!part->dkstats)
327 return 0;
328 return 1;
329}
330
331static inline void free_part_stats(struct hd_struct *part)
332{
333 free_percpu(part->dkstats);
334}
335
336#else /* !CONFIG_SMP */
337#define part_stat_lock() ({ rcu_read_lock(); 0; })
338#define part_stat_unlock() rcu_read_unlock()
339
340#define part_stat_get(part, field) ((part)->dkstats.field)
341#define part_stat_get_cpu(part, field, cpu) part_stat_get(part, field)
342#define part_stat_read(part, field) part_stat_get(part, field)
343
344static inline void part_stat_set_all(struct hd_struct *part, int value)
345{
346 memset(&part->dkstats, value, sizeof(struct disk_stats));
347}
348
349static inline int init_part_stats(struct hd_struct *part)
350{
351 return 1;
352}
353
354static inline void free_part_stats(struct hd_struct *part)
355{
356}
357
358#endif /* CONFIG_SMP */
359
360#define part_stat_read_msecs(part, which) \
361 div_u64(part_stat_read(part, nsecs[which]), NSEC_PER_MSEC)
362
363#define part_stat_read_accum(part, field) \
364 (part_stat_read(part, field[STAT_READ]) + \
365 part_stat_read(part, field[STAT_WRITE]) + \
366 part_stat_read(part, field[STAT_DISCARD]))
367
368#define __part_stat_add(part, field, addnd) \
369 (part_stat_get(part, field) += (addnd))
370
371#define part_stat_add(part, field, addnd) do { \
372 __part_stat_add((part), field, addnd); \
373 if ((part)->partno) \
374 __part_stat_add(&part_to_disk((part))->part0, \
375 field, addnd); \
376} while (0)
377
378#define part_stat_dec(gendiskp, field) \
379 part_stat_add(gendiskp, field, -1)
380#define part_stat_inc(gendiskp, field) \
381 part_stat_add(gendiskp, field, 1)
382#define part_stat_sub(gendiskp, field, subnd) \
383 part_stat_add(gendiskp, field, -subnd)
384
385#define part_stat_local_dec(gendiskp, field) \
386 local_dec(&(part_stat_get(gendiskp, field)))
387#define part_stat_local_inc(gendiskp, field) \
388 local_inc(&(part_stat_get(gendiskp, field)))
389#define part_stat_local_read(gendiskp, field) \
390 local_read(&(part_stat_get(gendiskp, field)))
391#define part_stat_local_read_cpu(gendiskp, field, cpu) \
392 local_read(&(part_stat_get_cpu(gendiskp, field, cpu)))
393
394unsigned int part_in_flight(struct request_queue *q, struct hd_struct *part);
395void part_in_flight_rw(struct request_queue *q, struct hd_struct *part,
396 unsigned int inflight[2]);
397void part_dec_in_flight(struct request_queue *q, struct hd_struct *part,
398 int rw);
399void part_inc_in_flight(struct request_queue *q, struct hd_struct *part,
400 int rw);
401
402static inline struct partition_meta_info *alloc_part_info(struct gendisk *disk)
403{
404 if (disk)
405 return kzalloc_node(sizeof(struct partition_meta_info),
406 GFP_KERNEL, disk->node_id);
407 return kzalloc(sizeof(struct partition_meta_info), GFP_KERNEL);
408}
409
410static inline void free_part_info(struct hd_struct *part)
411{
412 kfree(part->info);
413}
414
415void update_io_ticks(struct hd_struct *part, unsigned long now);
416
417/* block/genhd.c */
418extern void device_add_disk(struct device *parent, struct gendisk *disk,
419 const struct attribute_group **groups);
420static inline void add_disk(struct gendisk *disk)
421{
422 device_add_disk(NULL, disk, NULL);
423}
424extern void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk);
425static inline void add_disk_no_queue_reg(struct gendisk *disk)
426{
427 device_add_disk_no_queue_reg(NULL, disk);
428}
429
430extern void del_gendisk(struct gendisk *gp);
431extern struct gendisk *get_gendisk(dev_t dev, int *partno);
432extern struct block_device *bdget_disk(struct gendisk *disk, int partno);
433
434extern void set_device_ro(struct block_device *bdev, int flag);
435extern void set_disk_ro(struct gendisk *disk, int flag);
436
437static inline int get_disk_ro(struct gendisk *disk)
438{
439 return disk->part0.policy;
440}
441
442extern void disk_block_events(struct gendisk *disk);
443extern void disk_unblock_events(struct gendisk *disk);
444extern void disk_flush_events(struct gendisk *disk, unsigned int mask);
445extern unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask);
446
447/* drivers/char/random.c */
448extern void add_disk_randomness(struct gendisk *disk) __latent_entropy;
449extern void rand_initialize_disk(struct gendisk *disk);
450
451static inline sector_t get_start_sect(struct block_device *bdev)
452{
453 return bdev->bd_part->start_sect;
454}
455static inline sector_t get_capacity(struct gendisk *disk)
456{
457 return disk->part0.nr_sects;
458}
459static inline void set_capacity(struct gendisk *disk, sector_t size)
460{
461 disk->part0.nr_sects = size;
462}
463
464#ifdef CONFIG_SOLARIS_X86_PARTITION
465
466#define SOLARIS_X86_NUMSLICE 16
467#define SOLARIS_X86_VTOC_SANE (0x600DDEEEUL)
468
469struct solaris_x86_slice {
470 __le16 s_tag; /* ID tag of partition */
471 __le16 s_flag; /* permission flags */
472 __le32 s_start; /* start sector no of partition */
473 __le32 s_size; /* # of blocks in partition */
474};
475
476struct solaris_x86_vtoc {
477 unsigned int v_bootinfo[3]; /* info needed by mboot (unsupported) */
478 __le32 v_sanity; /* to verify vtoc sanity */
479 __le32 v_version; /* layout version */
480 char v_volume[8]; /* volume name */
481 __le16 v_sectorsz; /* sector size in bytes */
482 __le16 v_nparts; /* number of partitions */
483 unsigned int v_reserved[10]; /* free space */
484 struct solaris_x86_slice
485 v_slice[SOLARIS_X86_NUMSLICE]; /* slice headers */
486 unsigned int timestamp[SOLARIS_X86_NUMSLICE]; /* timestamp (unsupported) */
487 char v_asciilabel[128]; /* for compatibility */
488};
489
490#endif /* CONFIG_SOLARIS_X86_PARTITION */
491
492#ifdef CONFIG_BSD_DISKLABEL
493/*
494 * BSD disklabel support by Yossi Gottlieb <yogo@math.tau.ac.il>
495 * updated by Marc Espie <Marc.Espie@openbsd.org>
496 */
497
498/* check against BSD src/sys/sys/disklabel.h for consistency */
499
500#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
501#define BSD_MAXPARTITIONS 16
502#define OPENBSD_MAXPARTITIONS 16
503#define BSD_FS_UNUSED 0 /* disklabel unused partition entry ID */
504struct bsd_disklabel {
505 __le32 d_magic; /* the magic number */
506 __s16 d_type; /* drive type */
507 __s16 d_subtype; /* controller/d_type specific */
508 char d_typename[16]; /* type name, e.g. "eagle" */
509 char d_packname[16]; /* pack identifier */
510 __u32 d_secsize; /* # of bytes per sector */
511 __u32 d_nsectors; /* # of data sectors per track */
512 __u32 d_ntracks; /* # of tracks per cylinder */
513 __u32 d_ncylinders; /* # of data cylinders per unit */
514 __u32 d_secpercyl; /* # of data sectors per cylinder */
515 __u32 d_secperunit; /* # of data sectors per unit */
516 __u16 d_sparespertrack; /* # of spare sectors per track */
517 __u16 d_sparespercyl; /* # of spare sectors per cylinder */
518 __u32 d_acylinders; /* # of alt. cylinders per unit */
519 __u16 d_rpm; /* rotational speed */
520 __u16 d_interleave; /* hardware sector interleave */
521 __u16 d_trackskew; /* sector 0 skew, per track */
522 __u16 d_cylskew; /* sector 0 skew, per cylinder */
523 __u32 d_headswitch; /* head switch time, usec */
524 __u32 d_trkseek; /* track-to-track seek, usec */
525 __u32 d_flags; /* generic flags */
526#define NDDATA 5
527 __u32 d_drivedata[NDDATA]; /* drive-type specific information */
528#define NSPARE 5
529 __u32 d_spare[NSPARE]; /* reserved for future use */
530 __le32 d_magic2; /* the magic number (again) */
531 __le16 d_checksum; /* xor of data incl. partitions */
532
533 /* filesystem and partition information: */
534 __le16 d_npartitions; /* number of partitions in following */
535 __le32 d_bbsize; /* size of boot area at sn0, bytes */
536 __le32 d_sbsize; /* max size of fs superblock, bytes */
537 struct bsd_partition { /* the partition table */
538 __le32 p_size; /* number of sectors in partition */
539 __le32 p_offset; /* starting sector */
540 __le32 p_fsize; /* filesystem basic fragment size */
541 __u8 p_fstype; /* filesystem type, see below */
542 __u8 p_frag; /* filesystem fragments per block */
543 __le16 p_cpg; /* filesystem cylinders per group */
544 } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
545};
546
547#endif /* CONFIG_BSD_DISKLABEL */
548
549#ifdef CONFIG_UNIXWARE_DISKLABEL
550/*
551 * Unixware slices support by Andrzej Krzysztofowicz <ankry@mif.pg.gda.pl>
552 * and Krzysztof G. Baranowski <kgb@knm.org.pl>
553 */
554
555#define UNIXWARE_DISKMAGIC (0xCA5E600DUL) /* The disk magic number */
556#define UNIXWARE_DISKMAGIC2 (0x600DDEEEUL) /* The slice table magic nr */
557#define UNIXWARE_NUMSLICE 16
558#define UNIXWARE_FS_UNUSED 0 /* Unused slice entry ID */
559
560struct unixware_slice {
561 __le16 s_label; /* label */
562 __le16 s_flags; /* permission flags */
563 __le32 start_sect; /* starting sector */
564 __le32 nr_sects; /* number of sectors in slice */
565};
566
567struct unixware_disklabel {
568 __le32 d_type; /* drive type */
569 __le32 d_magic; /* the magic number */
570 __le32 d_version; /* version number */
571 char d_serial[12]; /* serial number of the device */
572 __le32 d_ncylinders; /* # of data cylinders per device */
573 __le32 d_ntracks; /* # of tracks per cylinder */
574 __le32 d_nsectors; /* # of data sectors per track */
575 __le32 d_secsize; /* # of bytes per sector */
576 __le32 d_part_start; /* # of first sector of this partition */
577 __le32 d_unknown1[12]; /* ? */
578 __le32 d_alt_tbl; /* byte offset of alternate table */
579 __le32 d_alt_len; /* byte length of alternate table */
580 __le32 d_phys_cyl; /* # of physical cylinders per device */
581 __le32 d_phys_trk; /* # of physical tracks per cylinder */
582 __le32 d_phys_sec; /* # of physical sectors per track */
583 __le32 d_phys_bytes; /* # of physical bytes per sector */
584 __le32 d_unknown2; /* ? */
585 __le32 d_unknown3; /* ? */
586 __le32 d_pad[8]; /* pad */
587
588 struct unixware_vtoc {
589 __le32 v_magic; /* the magic number */
590 __le32 v_version; /* version number */
591 char v_name[8]; /* volume name */
592 __le16 v_nslices; /* # of slices */
593 __le16 v_unknown1; /* ? */
594 __le32 v_reserved[10]; /* reserved */
595 struct unixware_slice
596 v_slice[UNIXWARE_NUMSLICE]; /* slice headers */
597 } vtoc;
598
599}; /* 408 */
600
601#endif /* CONFIG_UNIXWARE_DISKLABEL */
602
603#ifdef CONFIG_MINIX_SUBPARTITION
604# define MINIX_NR_SUBPARTITIONS 4
605#endif /* CONFIG_MINIX_SUBPARTITION */
606
607#define ADDPART_FLAG_NONE 0
608#define ADDPART_FLAG_RAID 1
609#define ADDPART_FLAG_WHOLEDISK 2
610
611extern int blk_alloc_devt(struct hd_struct *part, dev_t *devt);
612extern void blk_free_devt(dev_t devt);
613extern dev_t blk_lookup_devt(const char *name, int partno);
614extern char *disk_name (struct gendisk *hd, int partno, char *buf);
615
616extern int disk_expand_part_tbl(struct gendisk *disk, int target);
617extern int rescan_partitions(struct gendisk *disk, struct block_device *bdev);
618extern int invalidate_partitions(struct gendisk *disk, struct block_device *bdev);
619extern struct hd_struct * __must_check add_partition(struct gendisk *disk,
620 int partno, sector_t start,
621 sector_t len, int flags,
622 struct partition_meta_info
623 *info);
624extern void __delete_partition(struct percpu_ref *);
625extern void delete_partition(struct gendisk *, int);
626extern void printk_all_partitions(void);
627
628extern struct gendisk *__alloc_disk_node(int minors, int node_id);
629extern struct kobject *get_disk_and_module(struct gendisk *disk);
630extern void put_disk(struct gendisk *disk);
631extern void put_disk_and_module(struct gendisk *disk);
632extern void blk_register_region(dev_t devt, unsigned long range,
633 struct module *module,
634 struct kobject *(*probe)(dev_t, int *, void *),
635 int (*lock)(dev_t, void *),
636 void *data);
637extern void blk_unregister_region(dev_t devt, unsigned long range);
638
639extern ssize_t part_size_show(struct device *dev,
640 struct device_attribute *attr, char *buf);
641extern ssize_t part_stat_show(struct device *dev,
642 struct device_attribute *attr, char *buf);
643extern ssize_t part_inflight_show(struct device *dev,
644 struct device_attribute *attr, char *buf);
645#ifdef CONFIG_FAIL_MAKE_REQUEST
646extern ssize_t part_fail_show(struct device *dev,
647 struct device_attribute *attr, char *buf);
648extern ssize_t part_fail_store(struct device *dev,
649 struct device_attribute *attr,
650 const char *buf, size_t count);
651#endif /* CONFIG_FAIL_MAKE_REQUEST */
652
653#define alloc_disk_node(minors, node_id) \
654({ \
655 static struct lock_class_key __key; \
656 const char *__name; \
657 struct gendisk *__disk; \
658 \
659 __name = "(gendisk_completion)"#minors"("#node_id")"; \
660 \
661 __disk = __alloc_disk_node(minors, node_id); \
662 \
663 if (__disk) \
664 lockdep_init_map(&__disk->lockdep_map, __name, &__key, 0); \
665 \
666 __disk; \
667})
668
669#define alloc_disk(minors) alloc_disk_node(minors, NUMA_NO_NODE)
670
671static inline int hd_ref_init(struct hd_struct *part)
672{
673 if (percpu_ref_init(&part->ref, __delete_partition, 0,
674 GFP_KERNEL))
675 return -ENOMEM;
676 return 0;
677}
678
679static inline void hd_struct_get(struct hd_struct *part)
680{
681 percpu_ref_get(&part->ref);
682}
683
684static inline int hd_struct_try_get(struct hd_struct *part)
685{
686 return percpu_ref_tryget_live(&part->ref);
687}
688
689static inline void hd_struct_put(struct hd_struct *part)
690{
691 percpu_ref_put(&part->ref);
692}
693
694static inline void hd_struct_kill(struct hd_struct *part)
695{
696 percpu_ref_kill(&part->ref);
697}
698
699static inline void hd_free_part(struct hd_struct *part)
700{
701 free_part_stats(part);
702 free_part_info(part);
703 percpu_ref_exit(&part->ref);
704}
705
706/*
707 * Any access of part->nr_sects which is not protected by partition
708 * bd_mutex or gendisk bdev bd_mutex, should be done using this
709 * accessor function.
710 *
711 * Code written along the lines of i_size_read() and i_size_write().
712 * CONFIG_PREEMPT case optimizes the case of UP kernel with preemption
713 * on.
714 */
715static inline sector_t part_nr_sects_read(struct hd_struct *part)
716{
717#if BITS_PER_LONG==32 && defined(CONFIG_LBDAF) && defined(CONFIG_SMP)
718 sector_t nr_sects;
719 unsigned seq;
720 do {
721 seq = read_seqcount_begin(&part->nr_sects_seq);
722 nr_sects = part->nr_sects;
723 } while (read_seqcount_retry(&part->nr_sects_seq, seq));
724 return nr_sects;
725#elif BITS_PER_LONG==32 && defined(CONFIG_LBDAF) && defined(CONFIG_PREEMPT)
726 sector_t nr_sects;
727
728 preempt_disable();
729 nr_sects = part->nr_sects;
730 preempt_enable();
731 return nr_sects;
732#else
733 return part->nr_sects;
734#endif
735}
736
737/*
738 * Should be called with mutex lock held (typically bd_mutex) of partition
739 * to provide mutual exlusion among writers otherwise seqcount might be
740 * left in wrong state leaving the readers spinning infinitely.
741 */
742static inline void part_nr_sects_write(struct hd_struct *part, sector_t size)
743{
744#if BITS_PER_LONG==32 && defined(CONFIG_LBDAF) && defined(CONFIG_SMP)
745 write_seqcount_begin(&part->nr_sects_seq);
746 part->nr_sects = size;
747 write_seqcount_end(&part->nr_sects_seq);
748#elif BITS_PER_LONG==32 && defined(CONFIG_LBDAF) && defined(CONFIG_PREEMPT)
749 preempt_disable();
750 part->nr_sects = size;
751 preempt_enable();
752#else
753 part->nr_sects = size;
754#endif
755}
756
757#if defined(CONFIG_BLK_DEV_INTEGRITY)
758extern void blk_integrity_add(struct gendisk *);
759extern void blk_integrity_del(struct gendisk *);
760#else /* CONFIG_BLK_DEV_INTEGRITY */
761static inline void blk_integrity_add(struct gendisk *disk) { }
762static inline void blk_integrity_del(struct gendisk *disk) { }
763#endif /* CONFIG_BLK_DEV_INTEGRITY */
764
765#else /* CONFIG_BLOCK */
766
767static inline void printk_all_partitions(void) { }
768
769static inline dev_t blk_lookup_devt(const char *name, int partno)
770{
771 dev_t devt = MKDEV(0, 0);
772 return devt;
773}
774#endif /* CONFIG_BLOCK */
775
776#endif /* _LINUX_GENHD_H */
777