block/genhd.c: cleanups
[pv_ops_mirror.git] / include / linux / genhd.h
blobcd048e3cc96d939fd948d7b2f387ff9a2233aecf
1 #ifndef _LINUX_GENHD_H
2 #define _LINUX_GENHD_H
4 /*
5 * genhd.h Copyright (C) 1992 Drew Eckhardt
6 * Generic hard disk header file by
7 * Drew Eckhardt
9 * <drew@colorado.edu>
12 #include <linux/types.h>
13 #include <linux/kdev_t.h>
15 #ifdef CONFIG_BLOCK
17 #define kobj_to_dev(k) container_of(k, struct device, kobj)
18 #define dev_to_disk(device) container_of(device, struct gendisk, dev)
19 #define dev_to_part(device) container_of(device, struct hd_struct, dev)
21 extern struct device_type part_type;
22 extern struct kobject *block_depr;
23 extern struct class block_class;
25 enum {
26 /* These three have identical behaviour; use the second one if DOS FDISK gets
27 confused about extended/logical partitions starting past cylinder 1023. */
28 DOS_EXTENDED_PARTITION = 5,
29 LINUX_EXTENDED_PARTITION = 0x85,
30 WIN98_EXTENDED_PARTITION = 0x0f,
32 SUN_WHOLE_DISK = DOS_EXTENDED_PARTITION,
34 LINUX_SWAP_PARTITION = 0x82,
35 LINUX_DATA_PARTITION = 0x83,
36 LINUX_LVM_PARTITION = 0x8e,
37 LINUX_RAID_PARTITION = 0xfd, /* autodetect RAID partition */
39 SOLARIS_X86_PARTITION = LINUX_SWAP_PARTITION,
40 NEW_SOLARIS_X86_PARTITION = 0xbf,
42 DM6_AUX1PARTITION = 0x51, /* no DDO: use xlated geom */
43 DM6_AUX3PARTITION = 0x53, /* no DDO: use xlated geom */
44 DM6_PARTITION = 0x54, /* has DDO: use xlated geom & offset */
45 EZD_PARTITION = 0x55, /* EZ-DRIVE */
47 FREEBSD_PARTITION = 0xa5, /* FreeBSD Partition ID */
48 OPENBSD_PARTITION = 0xa6, /* OpenBSD Partition ID */
49 NETBSD_PARTITION = 0xa9, /* NetBSD Partition ID */
50 BSDI_PARTITION = 0xb7, /* BSDI Partition ID */
51 MINIX_PARTITION = 0x81, /* Minix Partition ID */
52 UNIXWARE_PARTITION = 0x63, /* Same as GNU_HURD and SCO Unix */
55 #ifndef __KERNEL__
57 struct partition {
58 unsigned char boot_ind; /* 0x80 - active */
59 unsigned char head; /* starting head */
60 unsigned char sector; /* starting sector */
61 unsigned char cyl; /* starting cylinder */
62 unsigned char sys_ind; /* What partition type */
63 unsigned char end_head; /* end head */
64 unsigned char end_sector; /* end sector */
65 unsigned char end_cyl; /* end cylinder */
66 unsigned int start_sect; /* starting sector counting from 0 */
67 unsigned int nr_sects; /* nr of sectors in partition */
68 } __attribute__((packed));
70 #endif
72 #ifdef __KERNEL__
73 #include <linux/major.h>
74 #include <linux/device.h>
75 #include <linux/smp.h>
76 #include <linux/string.h>
77 #include <linux/fs.h>
78 #include <linux/workqueue.h>
80 struct partition {
81 unsigned char boot_ind; /* 0x80 - active */
82 unsigned char head; /* starting head */
83 unsigned char sector; /* starting sector */
84 unsigned char cyl; /* starting cylinder */
85 unsigned char sys_ind; /* What partition type */
86 unsigned char end_head; /* end head */
87 unsigned char end_sector; /* end sector */
88 unsigned char end_cyl; /* end cylinder */
89 __le32 start_sect; /* starting sector counting from 0 */
90 __le32 nr_sects; /* nr of sectors in partition */
91 } __attribute__((packed));
93 struct disk_stats {
94 unsigned long sectors[2]; /* READs and WRITEs */
95 unsigned long ios[2];
96 unsigned long merges[2];
97 unsigned long ticks[2];
98 unsigned long io_ticks;
99 unsigned long time_in_queue;
102 struct hd_struct {
103 sector_t start_sect;
104 sector_t nr_sects;
105 struct device dev;
106 struct kobject *holder_dir;
107 int policy, partno;
108 #ifdef CONFIG_FAIL_MAKE_REQUEST
109 int make_it_fail;
110 #endif
111 unsigned long stamp;
112 int in_flight;
113 #ifdef CONFIG_SMP
114 struct disk_stats *dkstats;
115 #else
116 struct disk_stats dkstats;
117 #endif
120 #define GENHD_FL_REMOVABLE 1
121 #define GENHD_FL_DRIVERFS 2
122 #define GENHD_FL_MEDIA_CHANGE_NOTIFY 4
123 #define GENHD_FL_CD 8
124 #define GENHD_FL_UP 16
125 #define GENHD_FL_SUPPRESS_PARTITION_INFO 32
126 #define GENHD_FL_FAIL 64
129 struct gendisk {
130 int major; /* major number of driver */
131 int first_minor;
132 int minors; /* maximum number of minors, =1 for
133 * disks that can't be partitioned. */
134 char disk_name[32]; /* name of major driver */
135 struct hd_struct **part; /* [indexed by minor] */
136 struct block_device_operations *fops;
137 struct request_queue *queue;
138 void *private_data;
139 sector_t capacity;
141 int flags;
142 struct device *driverfs_dev; // FIXME: remove
143 struct device dev;
144 struct kobject *holder_dir;
145 struct kobject *slave_dir;
147 struct timer_rand_state *random;
148 int policy;
150 atomic_t sync_io; /* RAID */
151 unsigned long stamp;
152 int in_flight;
153 #ifdef CONFIG_SMP
154 struct disk_stats *dkstats;
155 #else
156 struct disk_stats dkstats;
157 #endif
158 struct work_struct async_notify;
162 * Macros to operate on percpu disk statistics:
164 * The __ variants should only be called in critical sections. The full
165 * variants disable/enable preemption.
167 static inline struct hd_struct *get_part(struct gendisk *gendiskp,
168 sector_t sector)
170 struct hd_struct *part;
171 int i;
172 for (i = 0; i < gendiskp->minors - 1; i++) {
173 part = gendiskp->part[i];
174 if (part && part->start_sect <= sector
175 && sector < part->start_sect + part->nr_sects)
176 return part;
178 return NULL;
181 #ifdef CONFIG_SMP
182 #define __disk_stat_add(gendiskp, field, addnd) \
183 (per_cpu_ptr(gendiskp->dkstats, smp_processor_id())->field += addnd)
185 #define disk_stat_read(gendiskp, field) \
186 ({ \
187 typeof(gendiskp->dkstats->field) res = 0; \
188 int i; \
189 for_each_possible_cpu(i) \
190 res += per_cpu_ptr(gendiskp->dkstats, i)->field; \
191 res; \
194 static inline void disk_stat_set_all(struct gendisk *gendiskp, int value) {
195 int i;
196 for_each_possible_cpu(i)
197 memset(per_cpu_ptr(gendiskp->dkstats, i), value,
198 sizeof (struct disk_stats));
201 #define __part_stat_add(part, field, addnd) \
202 (per_cpu_ptr(part->dkstats, smp_processor_id())->field += addnd)
204 #define __all_stat_add(gendiskp, field, addnd, sector) \
205 ({ \
206 struct hd_struct *part = get_part(gendiskp, sector); \
207 if (part) \
208 __part_stat_add(part, field, addnd); \
209 __disk_stat_add(gendiskp, field, addnd); \
212 #define part_stat_read(part, field) \
213 ({ \
214 typeof(part->dkstats->field) res = 0; \
215 int i; \
216 for_each_possible_cpu(i) \
217 res += per_cpu_ptr(part->dkstats, i)->field; \
218 res; \
221 static inline void part_stat_set_all(struct hd_struct *part, int value) {
222 int i;
223 for_each_possible_cpu(i)
224 memset(per_cpu_ptr(part->dkstats, i), value,
225 sizeof(struct disk_stats));
228 #else
229 #define __disk_stat_add(gendiskp, field, addnd) \
230 (gendiskp->dkstats.field += addnd)
231 #define disk_stat_read(gendiskp, field) (gendiskp->dkstats.field)
233 static inline void disk_stat_set_all(struct gendisk *gendiskp, int value)
235 memset(&gendiskp->dkstats, value, sizeof (struct disk_stats));
238 #define __part_stat_add(part, field, addnd) \
239 (part->dkstats.field += addnd)
241 #define __all_stat_add(gendiskp, field, addnd, sector) \
242 ({ \
243 struct hd_struct *part = get_part(gendiskp, sector); \
244 if (part) \
245 part->dkstats.field += addnd; \
246 __disk_stat_add(gendiskp, field, addnd); \
249 #define part_stat_read(part, field) (part->dkstats.field)
251 static inline void part_stat_set_all(struct hd_struct *part, int value)
253 memset(&part->dkstats, value, sizeof(struct disk_stats));
256 #endif
258 #define disk_stat_add(gendiskp, field, addnd) \
259 do { \
260 preempt_disable(); \
261 __disk_stat_add(gendiskp, field, addnd); \
262 preempt_enable(); \
263 } while (0)
265 #define __disk_stat_dec(gendiskp, field) __disk_stat_add(gendiskp, field, -1)
266 #define disk_stat_dec(gendiskp, field) disk_stat_add(gendiskp, field, -1)
268 #define __disk_stat_inc(gendiskp, field) __disk_stat_add(gendiskp, field, 1)
269 #define disk_stat_inc(gendiskp, field) disk_stat_add(gendiskp, field, 1)
271 #define __disk_stat_sub(gendiskp, field, subnd) \
272 __disk_stat_add(gendiskp, field, -subnd)
273 #define disk_stat_sub(gendiskp, field, subnd) \
274 disk_stat_add(gendiskp, field, -subnd)
276 #define part_stat_add(gendiskp, field, addnd) \
277 do { \
278 preempt_disable(); \
279 __part_stat_add(gendiskp, field, addnd);\
280 preempt_enable(); \
281 } while (0)
283 #define __part_stat_dec(gendiskp, field) __part_stat_add(gendiskp, field, -1)
284 #define part_stat_dec(gendiskp, field) part_stat_add(gendiskp, field, -1)
286 #define __part_stat_inc(gendiskp, field) __part_stat_add(gendiskp, field, 1)
287 #define part_stat_inc(gendiskp, field) part_stat_add(gendiskp, field, 1)
289 #define __part_stat_sub(gendiskp, field, subnd) \
290 __part_stat_add(gendiskp, field, -subnd)
291 #define part_stat_sub(gendiskp, field, subnd) \
292 part_stat_add(gendiskp, field, -subnd)
294 #define all_stat_add(gendiskp, field, addnd, sector) \
295 do { \
296 preempt_disable(); \
297 __all_stat_add(gendiskp, field, addnd, sector); \
298 preempt_enable(); \
299 } while (0)
301 #define __all_stat_dec(gendiskp, field, sector) \
302 __all_stat_add(gendiskp, field, -1, sector)
303 #define all_stat_dec(gendiskp, field, sector) \
304 all_stat_add(gendiskp, field, -1, sector)
306 #define __all_stat_inc(gendiskp, field, sector) \
307 __all_stat_add(gendiskp, field, 1, sector)
308 #define all_stat_inc(gendiskp, field, sector) \
309 all_stat_add(gendiskp, field, 1, sector)
311 #define __all_stat_sub(gendiskp, field, subnd, sector) \
312 __all_stat_add(gendiskp, field, -subnd, sector)
313 #define all_stat_sub(gendiskp, field, subnd, sector) \
314 all_stat_add(gendiskp, field, -subnd, sector)
316 /* Inlines to alloc and free disk stats in struct gendisk */
317 #ifdef CONFIG_SMP
318 static inline int init_disk_stats(struct gendisk *disk)
320 disk->dkstats = alloc_percpu(struct disk_stats);
321 if (!disk->dkstats)
322 return 0;
323 return 1;
326 static inline void free_disk_stats(struct gendisk *disk)
328 free_percpu(disk->dkstats);
331 static inline int init_part_stats(struct hd_struct *part)
333 part->dkstats = alloc_percpu(struct disk_stats);
334 if (!part->dkstats)
335 return 0;
336 return 1;
339 static inline void free_part_stats(struct hd_struct *part)
341 free_percpu(part->dkstats);
344 #else /* CONFIG_SMP */
345 static inline int init_disk_stats(struct gendisk *disk)
347 return 1;
350 static inline void free_disk_stats(struct gendisk *disk)
354 static inline int init_part_stats(struct hd_struct *part)
356 return 1;
359 static inline void free_part_stats(struct hd_struct *part)
362 #endif /* CONFIG_SMP */
364 /* drivers/block/ll_rw_blk.c */
365 extern void disk_round_stats(struct gendisk *disk);
366 extern void part_round_stats(struct hd_struct *part);
368 /* drivers/block/genhd.c */
369 extern int get_blkdev_list(char *, int);
370 extern void add_disk(struct gendisk *disk);
371 extern void del_gendisk(struct gendisk *gp);
372 extern void unlink_gendisk(struct gendisk *gp);
373 extern struct gendisk *get_gendisk(dev_t dev, int *part);
375 extern void set_device_ro(struct block_device *bdev, int flag);
376 extern void set_disk_ro(struct gendisk *disk, int flag);
378 /* drivers/char/random.c */
379 extern void add_disk_randomness(struct gendisk *disk);
380 extern void rand_initialize_disk(struct gendisk *disk);
382 static inline sector_t get_start_sect(struct block_device *bdev)
384 return bdev->bd_contains == bdev ? 0 : bdev->bd_part->start_sect;
386 static inline sector_t get_capacity(struct gendisk *disk)
388 return disk->capacity;
390 static inline void set_capacity(struct gendisk *disk, sector_t size)
392 disk->capacity = size;
395 #endif /* __KERNEL__ */
397 #ifdef CONFIG_SOLARIS_X86_PARTITION
399 #define SOLARIS_X86_NUMSLICE 16
400 #define SOLARIS_X86_VTOC_SANE (0x600DDEEEUL)
402 struct solaris_x86_slice {
403 __le16 s_tag; /* ID tag of partition */
404 __le16 s_flag; /* permission flags */
405 __le32 s_start; /* start sector no of partition */
406 __le32 s_size; /* # of blocks in partition */
409 struct solaris_x86_vtoc {
410 unsigned int v_bootinfo[3]; /* info needed by mboot (unsupported) */
411 __le32 v_sanity; /* to verify vtoc sanity */
412 __le32 v_version; /* layout version */
413 char v_volume[8]; /* volume name */
414 __le16 v_sectorsz; /* sector size in bytes */
415 __le16 v_nparts; /* number of partitions */
416 unsigned int v_reserved[10]; /* free space */
417 struct solaris_x86_slice
418 v_slice[SOLARIS_X86_NUMSLICE]; /* slice headers */
419 unsigned int timestamp[SOLARIS_X86_NUMSLICE]; /* timestamp (unsupported) */
420 char v_asciilabel[128]; /* for compatibility */
423 #endif /* CONFIG_SOLARIS_X86_PARTITION */
425 #ifdef CONFIG_BSD_DISKLABEL
427 * BSD disklabel support by Yossi Gottlieb <yogo@math.tau.ac.il>
428 * updated by Marc Espie <Marc.Espie@openbsd.org>
431 /* check against BSD src/sys/sys/disklabel.h for consistency */
433 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
434 #define BSD_MAXPARTITIONS 16
435 #define OPENBSD_MAXPARTITIONS 16
436 #define BSD_FS_UNUSED 0 /* disklabel unused partition entry ID */
437 struct bsd_disklabel {
438 __le32 d_magic; /* the magic number */
439 __s16 d_type; /* drive type */
440 __s16 d_subtype; /* controller/d_type specific */
441 char d_typename[16]; /* type name, e.g. "eagle" */
442 char d_packname[16]; /* pack identifier */
443 __u32 d_secsize; /* # of bytes per sector */
444 __u32 d_nsectors; /* # of data sectors per track */
445 __u32 d_ntracks; /* # of tracks per cylinder */
446 __u32 d_ncylinders; /* # of data cylinders per unit */
447 __u32 d_secpercyl; /* # of data sectors per cylinder */
448 __u32 d_secperunit; /* # of data sectors per unit */
449 __u16 d_sparespertrack; /* # of spare sectors per track */
450 __u16 d_sparespercyl; /* # of spare sectors per cylinder */
451 __u32 d_acylinders; /* # of alt. cylinders per unit */
452 __u16 d_rpm; /* rotational speed */
453 __u16 d_interleave; /* hardware sector interleave */
454 __u16 d_trackskew; /* sector 0 skew, per track */
455 __u16 d_cylskew; /* sector 0 skew, per cylinder */
456 __u32 d_headswitch; /* head switch time, usec */
457 __u32 d_trkseek; /* track-to-track seek, usec */
458 __u32 d_flags; /* generic flags */
459 #define NDDATA 5
460 __u32 d_drivedata[NDDATA]; /* drive-type specific information */
461 #define NSPARE 5
462 __u32 d_spare[NSPARE]; /* reserved for future use */
463 __le32 d_magic2; /* the magic number (again) */
464 __le16 d_checksum; /* xor of data incl. partitions */
466 /* filesystem and partition information: */
467 __le16 d_npartitions; /* number of partitions in following */
468 __le32 d_bbsize; /* size of boot area at sn0, bytes */
469 __le32 d_sbsize; /* max size of fs superblock, bytes */
470 struct bsd_partition { /* the partition table */
471 __le32 p_size; /* number of sectors in partition */
472 __le32 p_offset; /* starting sector */
473 __le32 p_fsize; /* filesystem basic fragment size */
474 __u8 p_fstype; /* filesystem type, see below */
475 __u8 p_frag; /* filesystem fragments per block */
476 __le16 p_cpg; /* filesystem cylinders per group */
477 } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
480 #endif /* CONFIG_BSD_DISKLABEL */
482 #ifdef CONFIG_UNIXWARE_DISKLABEL
484 * Unixware slices support by Andrzej Krzysztofowicz <ankry@mif.pg.gda.pl>
485 * and Krzysztof G. Baranowski <kgb@knm.org.pl>
488 #define UNIXWARE_DISKMAGIC (0xCA5E600DUL) /* The disk magic number */
489 #define UNIXWARE_DISKMAGIC2 (0x600DDEEEUL) /* The slice table magic nr */
490 #define UNIXWARE_NUMSLICE 16
491 #define UNIXWARE_FS_UNUSED 0 /* Unused slice entry ID */
493 struct unixware_slice {
494 __le16 s_label; /* label */
495 __le16 s_flags; /* permission flags */
496 __le32 start_sect; /* starting sector */
497 __le32 nr_sects; /* number of sectors in slice */
500 struct unixware_disklabel {
501 __le32 d_type; /* drive type */
502 __le32 d_magic; /* the magic number */
503 __le32 d_version; /* version number */
504 char d_serial[12]; /* serial number of the device */
505 __le32 d_ncylinders; /* # of data cylinders per device */
506 __le32 d_ntracks; /* # of tracks per cylinder */
507 __le32 d_nsectors; /* # of data sectors per track */
508 __le32 d_secsize; /* # of bytes per sector */
509 __le32 d_part_start; /* # of first sector of this partition */
510 __le32 d_unknown1[12]; /* ? */
511 __le32 d_alt_tbl; /* byte offset of alternate table */
512 __le32 d_alt_len; /* byte length of alternate table */
513 __le32 d_phys_cyl; /* # of physical cylinders per device */
514 __le32 d_phys_trk; /* # of physical tracks per cylinder */
515 __le32 d_phys_sec; /* # of physical sectors per track */
516 __le32 d_phys_bytes; /* # of physical bytes per sector */
517 __le32 d_unknown2; /* ? */
518 __le32 d_unknown3; /* ? */
519 __le32 d_pad[8]; /* pad */
521 struct unixware_vtoc {
522 __le32 v_magic; /* the magic number */
523 __le32 v_version; /* version number */
524 char v_name[8]; /* volume name */
525 __le16 v_nslices; /* # of slices */
526 __le16 v_unknown1; /* ? */
527 __le32 v_reserved[10]; /* reserved */
528 struct unixware_slice
529 v_slice[UNIXWARE_NUMSLICE]; /* slice headers */
530 } vtoc;
532 }; /* 408 */
534 #endif /* CONFIG_UNIXWARE_DISKLABEL */
536 #ifdef CONFIG_MINIX_SUBPARTITION
537 # define MINIX_NR_SUBPARTITIONS 4
538 #endif /* CONFIG_MINIX_SUBPARTITION */
540 #ifdef __KERNEL__
542 #define ADDPART_FLAG_NONE 0
543 #define ADDPART_FLAG_RAID 1
544 #define ADDPART_FLAG_WHOLEDISK 2
546 extern dev_t blk_lookup_devt(const char *name);
547 extern char *disk_name (struct gendisk *hd, int part, char *buf);
549 extern int rescan_partitions(struct gendisk *disk, struct block_device *bdev);
550 extern void add_partition(struct gendisk *, int, sector_t, sector_t, int);
551 extern void delete_partition(struct gendisk *, int);
552 extern void printk_all_partitions(void);
554 extern struct gendisk *alloc_disk_node(int minors, int node_id);
555 extern struct gendisk *alloc_disk(int minors);
556 extern struct kobject *get_disk(struct gendisk *disk);
557 extern void put_disk(struct gendisk *disk);
558 extern void blk_register_region(dev_t devt, unsigned long range,
559 struct module *module,
560 struct kobject *(*probe)(dev_t, int *, void *),
561 int (*lock)(dev_t, void *),
562 void *data);
563 extern void blk_unregister_region(dev_t devt, unsigned long range);
565 static inline struct block_device *bdget_disk(struct gendisk *disk, int index)
567 return bdget(MKDEV(disk->major, disk->first_minor) + index);
570 #endif
572 #else /* CONFIG_BLOCK */
574 static inline void printk_all_partitions(void) { }
576 static inline dev_t blk_lookup_devt(const char *name)
578 dev_t devt = MKDEV(0, 0);
579 return devt;
582 #endif /* CONFIG_BLOCK */
584 #endif