Home | History | Annotate | Line # | Download | only in dm
dm.h revision 1.30
      1 /*        $NetBSD: dm.h,v 1.30 2019/12/03 15:47:38 tkusumi 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 #include <sys/vnode.h>
     48 
     49 #include <sys/device.h>
     50 #include <sys/disk.h>
     51 #include <sys/disklabel.h>
     52 
     53 #include <miscfs/specfs/specdev.h> /* for v_rdev */
     54 
     55 #include <prop/proplib.h>
     56 
     57 #define DM_MAX_TYPE_NAME 16
     58 #define DM_NAME_LEN 128
     59 #define DM_UUID_LEN 129
     60 
     61 #define DM_VERSION_MAJOR	4
     62 #define DM_VERSION_MINOR	16
     63 
     64 #define DM_VERSION_PATCHLEVEL	0
     65 
     66 /*** Internal device-mapper structures ***/
     67 
     68 /*
     69  * A table entry describes a physical range of the logical volume.
     70  */
     71 #define MAX_TARGET_STRING_LEN 32
     72 
     73 /*
     74  * A device mapper table is a list of physical ranges plus the mapping target
     75  * applied to them.
     76  */
     77 
     78 typedef struct dm_table_entry {
     79 	struct dm_dev *dm_dev;		/* backlink */
     80 	uint64_t start;
     81 	uint64_t length;
     82 
     83 	struct dm_target *target;      /* Link to table target. */
     84 	void *target_config;           /* Target specific data. */
     85 	SLIST_ENTRY(dm_table_entry) next;
     86 } dm_table_entry_t;
     87 
     88 SLIST_HEAD(dm_table, dm_table_entry);
     89 
     90 typedef struct dm_table dm_table_t;
     91 
     92 typedef struct dm_table_head {
     93 	/* Current active table is selected with this. */
     94 	int cur_active_table;
     95 	struct dm_table tables[2];
     96 
     97 	kmutex_t   table_mtx;
     98 	kcondvar_t table_cv; /*IO waiting cv */
     99 
    100 	uint32_t io_cnt;
    101 } dm_table_head_t;
    102 
    103 #define MAX_DEV_NAME 32
    104 
    105 /*
    106  * This structure is used to store opened vnodes for disk with name.
    107  * I need this because devices can be opened only once, but I can
    108  * have more than one device on one partition.
    109  */
    110 
    111 typedef struct dm_pdev {
    112 	char name[MAX_DEV_NAME];
    113 
    114 	struct vnode *pdev_vnode;
    115 	uint64_t pdev_numsec;
    116 	unsigned pdev_secsize;
    117 	int ref_cnt; /* reference counter for users ofthis pdev */
    118 
    119 	SLIST_ENTRY(dm_pdev) next_pdev;
    120 } dm_pdev_t;
    121 
    122 /*
    123  * This structure is called for every device-mapper device.
    124  * It points to SLIST of device tables and mirrored, snapshoted etc. devices.
    125  */
    126 TAILQ_HEAD(dm_dev_head, dm_dev);
    127 //extern struct dm_dev_head dm_devs;
    128 
    129 typedef struct dm_dev {
    130 	char name[DM_NAME_LEN];
    131 	char uuid[DM_UUID_LEN];
    132 
    133 	device_t devt; /* pointer to autoconf device_t structure */
    134 	uint64_t minor; /* Device minor number */
    135 	uint32_t flags; /* store communication protocol flags */
    136 
    137 	kmutex_t dev_mtx; /* mutex for generall device lock */
    138 	kcondvar_t dev_cv; /* cv for between ioctl synchronisation */
    139 
    140 	uint32_t event_nr;
    141 	uint32_t ref_cnt;
    142 
    143 	dm_table_head_t table_head;
    144 
    145 	struct dm_dev_head upcalls;
    146 
    147 	struct disk *diskp;
    148 	kmutex_t diskp_mtx;
    149 
    150 	TAILQ_ENTRY(dm_dev) next_upcall; /* LIST of mirrored, snapshoted devices. */
    151 
    152 	TAILQ_ENTRY(dm_dev) next_devlist; /* Major device list. */
    153 } dm_dev_t;
    154 
    155 /* for zero, error : dm_target->target_config == NULL */
    156 
    157 /*
    158  * Target config is initiated with target_init function.
    159  */
    160 
    161 /* for linear : */
    162 typedef struct target_linear_config {
    163 	dm_pdev_t *pdev;
    164 	uint64_t offset;
    165 	TAILQ_ENTRY(target_linear_config) entries;
    166 } dm_target_linear_config_t;
    167 
    168 /*
    169  * Striping devices are stored in a linked list, this might be inefficient
    170  * for more than 8 striping devices and can be changed to something more
    171  * scalable.
    172  * TODO: look for other options than linked list.
    173  */
    174 TAILQ_HEAD(target_linear_devs, target_linear_config);
    175 
    176 typedef struct target_linear_devs dm_target_linear_devs_t;
    177 
    178 /* for stripe : */
    179 typedef struct target_stripe_config {
    180 #define DM_STRIPE_DEV_OFFSET 2
    181 	struct target_linear_devs stripe_devs;
    182 	uint8_t stripe_num;
    183 	uint64_t stripe_chunksize;
    184 	size_t params_len;
    185 } dm_target_stripe_config_t;
    186 
    187 /* for mirror : */
    188 typedef struct target_mirror_config {
    189 #define MAX_MIRROR_COPIES 4
    190 	dm_pdev_t *orig;
    191 	dm_pdev_t *copies[MAX_MIRROR_COPIES];
    192 
    193 	/* copied blocks bitmaps administration etc*/
    194 	dm_pdev_t *log_pdev;	/* for administration */
    195 	uint64_t log_regionsize;	/* blocksize of mirror */
    196 
    197 	/* list of parts that still need copied etc.; run length encoded? */
    198 } dm_target_mirror_config_t;
    199 
    200 
    201 /* for snapshot : */
    202 typedef struct target_snapshot_config {
    203 	dm_pdev_t *tsc_snap_dev;
    204 	/* cow dev is set only for persistent snapshot devices */
    205 	dm_pdev_t *tsc_cow_dev;
    206 
    207 	uint64_t tsc_chunk_size;
    208 	uint32_t tsc_persistent_dev;
    209 } dm_target_snapshot_config_t;
    210 
    211 /* for snapshot-origin devices */
    212 typedef struct target_snapshot_origin_config {
    213 	dm_pdev_t *tsoc_real_dev;
    214 	/* list of snapshots ? */
    215 } dm_target_snapshot_origin_config_t;
    216 
    217 /* constant dm_target structures for error, zero, linear, stripes etc. */
    218 typedef struct dm_target {
    219 	char name[DM_MAX_TYPE_NAME];
    220 	/* Initialize target_config area */
    221 	int (*init)(dm_dev_t *, void **, char *);
    222 
    223 	/* Destroy target_config area */
    224 	int (*destroy)(dm_table_entry_t *);
    225 
    226 	int (*deps) (dm_table_entry_t *, prop_array_t);
    227 	/*
    228 	 * Status routine is called to get params string, which is target
    229 	 * specific. When dm_table_status_ioctl is called with flag
    230 	 * DM_STATUS_TABLE_FLAG I have to sent params string back.
    231 	 */
    232 	char * (*status)(void *);
    233 	int (*strategy)(dm_table_entry_t *, struct buf *);
    234 	int (*sync)(dm_table_entry_t *);
    235 	int (*upcall)(dm_table_entry_t *, struct buf *);
    236 	int (*secsize)(dm_table_entry_t *, unsigned *);
    237 
    238 	uint32_t version[3];
    239 	uint32_t ref_cnt;
    240 
    241 	TAILQ_ENTRY(dm_target) dm_target_next;
    242 } dm_target_t;
    243 
    244 /* Interface structures */
    245 
    246 /* device-mapper */
    247 void dmgetproperties(struct disk *, dm_table_head_t *);
    248 
    249 /* dm_ioctl.c */
    250 int dm_dev_create_ioctl(prop_dictionary_t);
    251 int dm_dev_list_ioctl(prop_dictionary_t);
    252 int dm_dev_remove_ioctl(prop_dictionary_t);
    253 int dm_dev_rename_ioctl(prop_dictionary_t);
    254 int dm_dev_resume_ioctl(prop_dictionary_t);
    255 int dm_dev_status_ioctl(prop_dictionary_t);
    256 int dm_dev_suspend_ioctl(prop_dictionary_t);
    257 
    258 int dm_check_version(prop_dictionary_t);
    259 int dm_get_version_ioctl(prop_dictionary_t);
    260 int dm_list_versions_ioctl(prop_dictionary_t);
    261 
    262 int dm_table_clear_ioctl(prop_dictionary_t);
    263 int dm_table_deps_ioctl(prop_dictionary_t);
    264 int dm_table_load_ioctl(prop_dictionary_t);
    265 int dm_table_status_ioctl(prop_dictionary_t);
    266 
    267 /* dm_target.c */
    268 dm_target_t* dm_target_alloc(const char *);
    269 dm_target_t* dm_target_autoload(const char *);
    270 int dm_target_destroy(void);
    271 int dm_target_insert(dm_target_t *);
    272 prop_array_t dm_target_prop_list(void);
    273 dm_target_t* dm_target_lookup(const char *);
    274 int dm_target_rem(char *);
    275 void dm_target_unbusy(dm_target_t *);
    276 void dm_target_busy(dm_target_t *);
    277 
    278 /* XXX temporally add */
    279 int dm_target_init(void);
    280 
    281 #define DM_MAX_PARAMS_SIZE 1024
    282 
    283 /* dm_target_linear.c */
    284 int dm_target_linear_init(dm_dev_t *, void**, char *);
    285 char * dm_target_linear_status(void *);
    286 int dm_target_linear_strategy(dm_table_entry_t *, struct buf *);
    287 int dm_target_linear_sync(dm_table_entry_t *);
    288 int dm_target_linear_deps(dm_table_entry_t *, prop_array_t);
    289 int dm_target_linear_destroy(dm_table_entry_t *);
    290 int dm_target_linear_upcall(dm_table_entry_t *, struct buf *);
    291 int dm_target_linear_secsize(dm_table_entry_t *, unsigned *);
    292 
    293 /* Generic function used to convert char to string */
    294 uint64_t atoi(const char *);
    295 
    296 /* dm_target_stripe.c */
    297 int dm_target_stripe_init(dm_dev_t *, void**, char *);
    298 char * dm_target_stripe_status(void *);
    299 int dm_target_stripe_strategy(dm_table_entry_t *, struct buf *);
    300 int dm_target_stripe_sync(dm_table_entry_t *);
    301 int dm_target_stripe_deps(dm_table_entry_t *, prop_array_t);
    302 int dm_target_stripe_destroy(dm_table_entry_t *);
    303 int dm_target_stripe_upcall(dm_table_entry_t *, struct buf *);
    304 int dm_target_stripe_secsize(dm_table_entry_t *, unsigned *);
    305 
    306 /* dm_table.c  */
    307 #define DM_TABLE_ACTIVE 0
    308 #define DM_TABLE_INACTIVE 1
    309 
    310 int dm_table_destroy(dm_table_head_t *, uint8_t);
    311 uint64_t dm_table_size(dm_table_head_t *);
    312 uint64_t dm_inactive_table_size(dm_table_head_t *);
    313 void dm_table_disksize(dm_table_head_t *, uint64_t *, unsigned *);
    314 dm_table_t * dm_table_get_entry(dm_table_head_t *, uint8_t);
    315 int dm_table_get_target_count(dm_table_head_t *, uint8_t);
    316 void dm_table_release(dm_table_head_t *, uint8_t s);
    317 void dm_table_switch_tables(dm_table_head_t *);
    318 void dm_table_head_init(dm_table_head_t *);
    319 void dm_table_head_destroy(dm_table_head_t *);
    320 
    321 /* dm_dev.c */
    322 dm_dev_t* dm_dev_alloc(void);
    323 void dm_dev_busy(dm_dev_t *);
    324 int dm_dev_destroy(void);
    325 dm_dev_t* dm_dev_detach(device_t);
    326 int dm_dev_free(dm_dev_t *);
    327 int dm_dev_init(void);
    328 int dm_dev_insert(dm_dev_t *);
    329 dm_dev_t* dm_dev_lookup(const char *, const char *, int);
    330 prop_array_t dm_dev_prop_list(void);
    331 dm_dev_t* dm_dev_rem(const char *, const char *, int);
    332 /*int dm_dev_test_minor(int);*/
    333 void dm_dev_unbusy(dm_dev_t *);
    334 
    335 /* dm_pdev.c */
    336 int dm_pdev_decr(dm_pdev_t *);
    337 int dm_pdev_destroy(void);
    338 int dm_pdev_init(void);
    339 dm_pdev_t* dm_pdev_insert(const char *);
    340 
    341 #endif /*_KERNEL*/
    342 
    343 #endif /*_DM_DEV_H_*/
    344