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