Home | History | Annotate | Line # | Download | only in dm
dm.h revision 1.24
      1 /*        $NetBSD: dm.h,v 1.24 2012/07/28 00:43:22 matt Exp $      */
      2 
      3 /*
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Adam Hamsik.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _DM_DEV_H_
     33 #define _DM_DEV_H_
     34 
     35 
     36 #ifdef _KERNEL
     37 
     38 #include <sys/errno.h>
     39 
     40 #include <sys/atomic.h>
     41 #include <sys/fcntl.h>
     42 #include <sys/condvar.h>
     43 #include <sys/kauth.h>
     44 #include <sys/mutex.h>
     45 #include <sys/rwlock.h>
     46 #include <sys/queue.h>
     47 
     48 #include <sys/device.h>
     49 #include <sys/disk.h>
     50 #include <sys/disklabel.h>
     51 
     52 #include <prop/proplib.h>
     53 
     54 #define DM_MAX_TYPE_NAME 16
     55 #define DM_NAME_LEN 128
     56 #define DM_UUID_LEN 129
     57 
     58 #define DM_VERSION_MAJOR	4
     59 #define DM_VERSION_MINOR	16
     60 
     61 #define DM_VERSION_PATCHLEVEL	0
     62 
     63 /*** Internal device-mapper structures ***/
     64 
     65 /*
     66  * A table entry describes a physical range of the logical volume.
     67  */
     68 #define MAX_TARGET_STRING_LEN 32
     69 
     70 /*
     71  * A device mapper table is a list of physical ranges plus the mapping target
     72  * applied to them.
     73  */
     74 
     75 typedef struct dm_table_entry {
     76 	struct dm_dev *dm_dev;		/* backlink */
     77 	uint64_t start;
     78 	uint64_t length;
     79 
     80 	struct dm_target *target;      /* Link to table target. */
     81 	void *target_config;           /* Target specific data. */
     82 	SLIST_ENTRY(dm_table_entry) next;
     83 } dm_table_entry_t;
     84 
     85 SLIST_HEAD(dm_table, dm_table_entry);
     86 
     87 typedef struct dm_table dm_table_t;
     88 
     89 typedef struct dm_table_head {
     90 	/* Current active table is selected with this. */
     91 	int cur_active_table;
     92 	struct dm_table tables[2];
     93 
     94 	kmutex_t   table_mtx;
     95 	kcondvar_t table_cv; /*IO waiting cv */
     96 
     97 	uint32_t io_cnt;
     98 } dm_table_head_t;
     99 
    100 #define MAX_DEV_NAME 32
    101 
    102 /*
    103  * This structure is used to store opened vnodes for disk with name.
    104  * I need this because devices can be opened only once, but I can
    105  * have more then one device on one partition.
    106  */
    107 
    108 typedef struct dm_pdev {
    109 	char name[MAX_DEV_NAME];
    110 
    111 	struct vnode *pdev_vnode;
    112 	uint64_t pdev_numsec;
    113 	unsigned pdev_secsize;
    114 	int ref_cnt; /* reference counter for users ofthis pdev */
    115 
    116 	SLIST_ENTRY(dm_pdev) next_pdev;
    117 } dm_pdev_t;
    118 
    119 /*
    120  * This structure is called for every device-mapper device.
    121  * It points to SLIST of device tables and mirrored, snapshoted etc. devices.
    122  */
    123 TAILQ_HEAD(dm_dev_head, dm_dev);
    124 //extern struct dm_dev_head dm_devs;
    125 
    126 typedef struct dm_dev {
    127 	char name[DM_NAME_LEN];
    128 	char uuid[DM_UUID_LEN];
    129 
    130 	device_t devt; /* pointer to autoconf device_t structure */
    131 	uint64_t minor; /* Device minor number */
    132 	uint32_t flags; /* store communication protocol flags */
    133 
    134 	kmutex_t dev_mtx; /* mutex for generall device lock */
    135 	kcondvar_t dev_cv; /* cv for between ioctl synchronisation */
    136 
    137 	uint32_t event_nr;
    138 	uint32_t ref_cnt;
    139 
    140 	uint32_t dev_type;
    141 
    142 	dm_table_head_t table_head;
    143 
    144 	struct dm_dev_head upcalls;
    145 
    146 	struct disk *diskp;
    147 	kmutex_t diskp_mtx;
    148 
    149 	TAILQ_ENTRY(dm_dev) next_upcall; /* LIST of mirrored, snapshoted devices. */
    150 
    151 	TAILQ_ENTRY(dm_dev) next_devlist; /* Major device list. */
    152 } dm_dev_t;
    153 
    154 /* Device types used for upcalls */
    155 #define DM_ZERO_DEV            (1 << 0)
    156 #define DM_ERROR_DEV           (1 << 1)
    157 #define DM_LINEAR_DEV          (1 << 2)
    158 #define DM_MIRROR_DEV          (1 << 3)
    159 #define DM_STRIPE_DEV          (1 << 4)
    160 #define DM_SNAPSHOT_DEV        (1 << 5)
    161 #define DM_SNAPSHOT_ORIG_DEV   (1 << 6)
    162 #define DM_SPARE_DEV           (1 << 7)
    163 /* Set this device type only during dev remove ioctl. */
    164 #define DM_DELETING_DEV        (1 << 8)
    165 
    166 
    167 /* for zero, error : dm_target->target_config == NULL */
    168 
    169 /*
    170  * Target config is initiated with target_init function.
    171  */
    172 
    173 /* for linear : */
    174 typedef struct target_linear_config {
    175 	dm_pdev_t *pdev;
    176 	uint64_t offset;
    177 	TAILQ_ENTRY(target_linear_config) entries;
    178 } dm_target_linear_config_t;
    179 
    180 /*
    181  * Striping devices are stored in a linked list, this might be inefficient
    182  * for more than 8 striping devices and can be changed to something more
    183  * scalable.
    184  * TODO: look for other options than linked list.
    185  */
    186 TAILQ_HEAD(target_linear_devs, target_linear_config);
    187 
    188 typedef struct target_linear_devs dm_target_linear_devs_t;
    189 
    190 /* for stripe : */
    191 typedef struct target_stripe_config {
    192 #define DM_STRIPE_DEV_OFFSET 2
    193 	struct target_linear_devs stripe_devs;
    194 	uint8_t stripe_num;
    195 	uint64_t stripe_chunksize;
    196 	size_t params_len;
    197 } dm_target_stripe_config_t;
    198 
    199 /* for mirror : */
    200 typedef struct target_mirror_config {
    201 #define MAX_MIRROR_COPIES 4
    202 	dm_pdev_t *orig;
    203 	dm_pdev_t *copies[MAX_MIRROR_COPIES];
    204 
    205 	/* copied blocks bitmaps administration etc*/
    206 	dm_pdev_t *log_pdev;	/* for administration */
    207 	uint64_t log_regionsize;	/* blocksize of mirror */
    208 
    209 	/* list of parts that still need copied etc.; run length encoded? */
    210 } dm_target_mirror_config_t;
    211 
    212 
    213 /* for snapshot : */
    214 typedef struct target_snapshot_config {
    215 	dm_pdev_t *tsc_snap_dev;
    216 	/* cow dev is set only for persistent snapshot devices */
    217 	dm_pdev_t *tsc_cow_dev;
    218 
    219 	uint64_t tsc_chunk_size;
    220 	uint32_t tsc_persistent_dev;
    221 } dm_target_snapshot_config_t;
    222 
    223 /* for snapshot-origin devices */
    224 typedef struct target_snapshot_origin_config {
    225 	dm_pdev_t *tsoc_real_dev;
    226 	/* list of snapshots ? */
    227 } dm_target_snapshot_origin_config_t;
    228 
    229 /* constant dm_target structures for error, zero, linear, stripes etc. */
    230 typedef struct dm_target {
    231 	char name[DM_MAX_TYPE_NAME];
    232 	/* Initialize target_config area */
    233 	int (*init)(dm_dev_t *, void **, char *);
    234 
    235 	/* Destroy target_config area */
    236 	int (*destroy)(dm_table_entry_t *);
    237 
    238 	int (*deps) (dm_table_entry_t *, prop_array_t);
    239 	/*
    240 	 * Status routine is called to get params string, which is target
    241 	 * specific. When dm_table_status_ioctl is called with flag
    242 	 * DM_STATUS_TABLE_FLAG I have to sent params string back.
    243 	 */
    244 	char * (*status)(void *);
    245 	int (*strategy)(dm_table_entry_t *, struct buf *);
    246 	int (*sync)(dm_table_entry_t *);
    247 	int (*upcall)(dm_table_entry_t *, struct buf *);
    248 	int (*secsize)(dm_table_entry_t *, unsigned *);
    249 
    250 	uint32_t version[3];
    251 	int ref_cnt;
    252 
    253 	TAILQ_ENTRY(dm_target) dm_target_next;
    254 } dm_target_t;
    255 
    256 /* Interface structures */
    257 
    258 /*
    259  * This structure is used to translate command sent to kernel driver in
    260  * <key>command</key>
    261  * <value></value>
    262  * to function which I can call, and if the command is allowed for
    263  * non-superusers.
    264  */
    265 struct cmd_function {
    266 	const char *cmd;
    267 	int  (*fn)(prop_dictionary_t);
    268 	int  allowed;
    269 };
    270 
    271 /* device-mapper */
    272 void dmgetproperties(struct disk *, dm_table_head_t *);
    273 
    274 /* dm_ioctl.c */
    275 int dm_dev_create_ioctl(prop_dictionary_t);
    276 int dm_dev_list_ioctl(prop_dictionary_t);
    277 int dm_dev_remove_ioctl(prop_dictionary_t);
    278 int dm_dev_rename_ioctl(prop_dictionary_t);
    279 int dm_dev_resume_ioctl(prop_dictionary_t);
    280 int dm_dev_status_ioctl(prop_dictionary_t);
    281 int dm_dev_suspend_ioctl(prop_dictionary_t);
    282 
    283 int dm_check_version(prop_dictionary_t);
    284 int dm_get_version_ioctl(prop_dictionary_t);
    285 int dm_list_versions_ioctl(prop_dictionary_t);
    286 
    287 int dm_table_clear_ioctl(prop_dictionary_t);
    288 int dm_table_deps_ioctl(prop_dictionary_t);
    289 int dm_table_load_ioctl(prop_dictionary_t);
    290 int dm_table_status_ioctl(prop_dictionary_t);
    291 
    292 /* dm_target.c */
    293 dm_target_t* dm_target_alloc(const char *);
    294 dm_target_t* dm_target_autoload(const char *);
    295 int dm_target_destroy(void);
    296 int dm_target_insert(dm_target_t *);
    297 prop_array_t dm_target_prop_list(void);
    298 dm_target_t* dm_target_lookup(const char *);
    299 int dm_target_rem(char *);
    300 void dm_target_unbusy(dm_target_t *);
    301 void dm_target_busy(dm_target_t *);
    302 
    303 /* XXX temporally add */
    304 int dm_target_init(void);
    305 
    306 #define DM_MAX_PARAMS_SIZE 1024
    307 
    308 /* dm_target_linear.c */
    309 int dm_target_linear_init(dm_dev_t *, void**, char *);
    310 char * dm_target_linear_status(void *);
    311 int dm_target_linear_strategy(dm_table_entry_t *, struct buf *);
    312 int dm_target_linear_sync(dm_table_entry_t *);
    313 int dm_target_linear_deps(dm_table_entry_t *, prop_array_t);
    314 int dm_target_linear_destroy(dm_table_entry_t *);
    315 int dm_target_linear_upcall(dm_table_entry_t *, struct buf *);
    316 int dm_target_linear_secsize(dm_table_entry_t *, unsigned *);
    317 
    318 /* Generic function used to convert char to string */
    319 uint64_t atoi(const char *);
    320 
    321 /* dm_target_stripe.c */
    322 int dm_target_stripe_init(dm_dev_t *, void**, char *);
    323 char * dm_target_stripe_status(void *);
    324 int dm_target_stripe_strategy(dm_table_entry_t *, struct buf *);
    325 int dm_target_stripe_sync(dm_table_entry_t *);
    326 int dm_target_stripe_deps(dm_table_entry_t *, prop_array_t);
    327 int dm_target_stripe_destroy(dm_table_entry_t *);
    328 int dm_target_stripe_upcall(dm_table_entry_t *, struct buf *);
    329 int dm_target_stripe_secsize(dm_table_entry_t *, unsigned *);
    330 
    331 /* dm_table.c  */
    332 #define DM_TABLE_ACTIVE 0
    333 #define DM_TABLE_INACTIVE 1
    334 
    335 int dm_table_destroy(dm_table_head_t *, uint8_t);
    336 uint64_t dm_table_size(dm_table_head_t *);
    337 uint64_t dm_inactive_table_size(dm_table_head_t *);
    338 void dm_table_disksize(dm_table_head_t *, uint64_t *, unsigned *);
    339 dm_table_t * dm_table_get_entry(dm_table_head_t *, uint8_t);
    340 int dm_table_get_target_count(dm_table_head_t *, uint8_t);
    341 void dm_table_release(dm_table_head_t *, uint8_t s);
    342 void dm_table_switch_tables(dm_table_head_t *);
    343 void dm_table_head_init(dm_table_head_t *);
    344 void dm_table_head_destroy(dm_table_head_t *);
    345 
    346 /* dm_dev.c */
    347 dm_dev_t* dm_dev_alloc(void);
    348 void dm_dev_busy(dm_dev_t *);
    349 int dm_dev_destroy(void);
    350 dm_dev_t* dm_dev_detach(device_t);
    351 int dm_dev_free(dm_dev_t *);
    352 int dm_dev_init(void);
    353 int dm_dev_insert(dm_dev_t *);
    354 dm_dev_t* dm_dev_lookup(const char *, const char *, int);
    355 prop_array_t dm_dev_prop_list(void);
    356 dm_dev_t* dm_dev_rem(const char *, const char *, int);
    357 /*int dm_dev_test_minor(int);*/
    358 void dm_dev_unbusy(dm_dev_t *);
    359 
    360 /* dm_pdev.c */
    361 int dm_pdev_decr(dm_pdev_t *);
    362 int dm_pdev_destroy(void);
    363 int dm_pdev_init(void);
    364 dm_pdev_t* dm_pdev_insert(const char *);
    365 
    366 #endif /*_KERNEL*/
    367 
    368 #endif /*_DM_DEV_H_*/
    369