Home | History | Annotate | Line # | Download | only in dm
      1  1.56   andvar /*        $NetBSD: dm.h,v 1.56 2021/08/21 22:23:33 andvar Exp $      */
      2   1.2     haad 
      3   1.2     haad /*
      4   1.2     haad  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5   1.2     haad  * All rights reserved.
      6   1.2     haad  *
      7   1.2     haad  * This code is derived from software contributed to The NetBSD Foundation
      8   1.2     haad  * by Adam Hamsik.
      9   1.2     haad  *
     10   1.2     haad  * Redistribution and use in source and binary forms, with or without
     11   1.2     haad  * modification, are permitted provided that the following conditions
     12   1.2     haad  * are met:
     13   1.2     haad  * 1. Redistributions of source code must retain the above copyright
     14   1.2     haad  *    notice, this list of conditions and the following disclaimer.
     15   1.2     haad  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.2     haad  *    notice, this list of conditions and the following disclaimer in the
     17   1.2     haad  *    documentation and/or other materials provided with the distribution.
     18   1.2     haad  *
     19   1.2     haad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.2     haad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.2     haad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.2     haad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.2     haad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.2     haad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.2     haad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.2     haad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.2     haad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.2     haad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.2     haad  * POSSIBILITY OF SUCH DAMAGE.
     30   1.2     haad  */
     31   1.2     haad 
     32  1.48  tkusumi #ifndef _DM_H_
     33  1.48  tkusumi #define _DM_H_
     34   1.2     haad 
     35   1.2     haad #ifdef _KERNEL
     36   1.2     haad 
     37   1.2     haad #include <sys/errno.h>
     38   1.2     haad #include <sys/atomic.h>
     39  1.18     haad #include <sys/fcntl.h>
     40   1.2     haad #include <sys/condvar.h>
     41  1.18     haad #include <sys/kauth.h>
     42   1.2     haad #include <sys/mutex.h>
     43   1.2     haad #include <sys/rwlock.h>
     44   1.2     haad #include <sys/queue.h>
     45  1.30  tkusumi #include <sys/vnode.h>
     46  1.17     haad #include <sys/device.h>
     47  1.21  mlelstv #include <sys/disk.h>
     48  1.11     haad #include <sys/disklabel.h>
     49  1.11     haad 
     50  1.26  hannken #include <miscfs/specfs/specdev.h> /* for v_rdev */
     51  1.26  hannken 
     52  1.12   dyoung #include <prop/proplib.h>
     53  1.12   dyoung 
     54   1.2     haad #define DM_MAX_TYPE_NAME 16
     55   1.2     haad #define DM_NAME_LEN 128
     56   1.2     haad #define DM_UUID_LEN 129
     57   1.2     haad 
     58  1.16     haad #define DM_VERSION_MAJOR	4
     59  1.16     haad #define DM_VERSION_MINOR	16
     60  1.16     haad 
     61   1.2     haad #define DM_VERSION_PATCHLEVEL	0
     62   1.2     haad 
     63   1.2     haad /*** Internal device-mapper structures ***/
     64   1.2     haad 
     65  1.40  tkusumi extern const struct dkdriver dmdkdriver;
     66  1.40  tkusumi extern uint32_t dm_dev_counter;
     67  1.40  tkusumi 
     68  1.50  tkusumi typedef struct dm_mapping {
     69  1.50  tkusumi 	union {
     70  1.50  tkusumi 		struct dm_pdev *pdev;
     71  1.50  tkusumi 	} data;
     72  1.50  tkusumi 	TAILQ_ENTRY(dm_mapping) next;
     73  1.50  tkusumi } dm_mapping_t;
     74  1.50  tkusumi 
     75   1.2     haad /*
     76   1.2     haad  * A device mapper table is a list of physical ranges plus the mapping target
     77   1.2     haad  * applied to them.
     78   1.2     haad  */
     79   1.2     haad typedef struct dm_table_entry {
     80   1.2     haad 	struct dm_dev *dm_dev;		/* backlink */
     81   1.2     haad 	uint64_t start;
     82   1.2     haad 	uint64_t length;
     83   1.2     haad 
     84   1.2     haad 	struct dm_target *target;      /* Link to table target. */
     85   1.2     haad 	void *target_config;           /* Target specific data. */
     86   1.2     haad 	SLIST_ENTRY(dm_table_entry) next;
     87  1.50  tkusumi 
     88  1.50  tkusumi 	TAILQ_HEAD(, dm_mapping) pdev_maps;
     89   1.2     haad } dm_table_entry_t;
     90   1.2     haad 
     91   1.2     haad SLIST_HEAD(dm_table, dm_table_entry);
     92   1.2     haad 
     93   1.2     haad typedef struct dm_table dm_table_t;
     94   1.2     haad 
     95   1.2     haad typedef struct dm_table_head {
     96   1.2     haad 	/* Current active table is selected with this. */
     97  1.23    ahoka 	int cur_active_table;
     98  1.48  tkusumi 	dm_table_t tables[2];
     99   1.2     haad 
    100   1.2     haad 	kmutex_t   table_mtx;
    101  1.48  tkusumi 	kcondvar_t table_cv; /* I/O waiting cv */
    102   1.2     haad 
    103   1.2     haad 	uint32_t io_cnt;
    104   1.2     haad } dm_table_head_t;
    105   1.2     haad 
    106   1.2     haad #define MAX_DEV_NAME 32
    107   1.2     haad 
    108   1.2     haad /*
    109   1.2     haad  * This structure is used to store opened vnodes for disk with name.
    110   1.2     haad  * I need this because devices can be opened only once, but I can
    111  1.25      wiz  * have more than one device on one partition.
    112   1.2     haad  */
    113   1.2     haad typedef struct dm_pdev {
    114   1.2     haad 	char name[MAX_DEV_NAME];
    115  1.49  tkusumi 	char udev_name[MAX_DEV_NAME];
    116   1.2     haad 
    117   1.2     haad 	struct vnode *pdev_vnode;
    118  1.21  mlelstv 	uint64_t pdev_numsec;
    119  1.43  tkusumi 	unsigned int pdev_secsize;
    120  1.56   andvar 	int ref_cnt; /* reference counter for users of this pdev */
    121   1.2     haad 
    122   1.2     haad 	SLIST_ENTRY(dm_pdev) next_pdev;
    123   1.2     haad } dm_pdev_t;
    124   1.2     haad 
    125   1.2     haad /*
    126   1.2     haad  * This structure is called for every device-mapper device.
    127   1.2     haad  * It points to SLIST of device tables and mirrored, snapshoted etc. devices.
    128   1.2     haad  */
    129  1.24     matt TAILQ_HEAD(dm_dev_head, dm_dev);
    130  1.24     matt //extern struct dm_dev_head dm_devs;
    131  1.23    ahoka 
    132   1.2     haad typedef struct dm_dev {
    133   1.2     haad 	char name[DM_NAME_LEN];
    134   1.2     haad 	char uuid[DM_UUID_LEN];
    135   1.2     haad 
    136  1.17     haad 	device_t devt; /* pointer to autoconf device_t structure */
    137  1.20     haad 	uint64_t minor; /* Device minor number */
    138   1.2     haad 	uint32_t flags; /* store communication protocol flags */
    139   1.2     haad 
    140  1.56   andvar 	kmutex_t dev_mtx; /* mutex for general device lock */
    141   1.2     haad 	kcondvar_t dev_cv; /* cv for between ioctl synchronisation */
    142  1.23    ahoka 
    143   1.2     haad 	uint32_t event_nr;
    144   1.2     haad 	uint32_t ref_cnt;
    145   1.2     haad 
    146   1.2     haad 	dm_table_head_t table_head;
    147   1.2     haad 
    148  1.32  tkusumi 	//struct dm_dev_head upcalls;
    149  1.23    ahoka 
    150   1.8     haad 	struct disk *diskp;
    151  1.17     haad 	kmutex_t diskp_mtx;
    152  1.23    ahoka 
    153  1.32  tkusumi 	//TAILQ_ENTRY(dm_dev) next_upcall; /* LIST of mirrored, snapshoted devices. */
    154   1.2     haad 
    155   1.2     haad 	TAILQ_ENTRY(dm_dev) next_devlist; /* Major device list. */
    156   1.2     haad } dm_dev_t;
    157   1.2     haad 
    158  1.48  tkusumi /* For linear target. */
    159   1.2     haad typedef struct target_linear_config {
    160   1.2     haad 	dm_pdev_t *pdev;
    161   1.2     haad 	uint64_t offset;
    162  1.19     haad 	TAILQ_ENTRY(target_linear_config) entries;
    163   1.2     haad } dm_target_linear_config_t;
    164   1.2     haad 
    165  1.19     haad /*
    166  1.19     haad  * Striping devices are stored in a linked list, this might be inefficient
    167  1.19     haad  * for more than 8 striping devices and can be changed to something more
    168  1.19     haad  * scalable.
    169  1.19     haad  * TODO: look for other options than linked list.
    170  1.19     haad  */
    171  1.19     haad TAILQ_HEAD(target_linear_devs, target_linear_config);
    172  1.19     haad 
    173  1.19     haad typedef struct target_linear_devs dm_target_linear_devs_t;
    174  1.19     haad 
    175   1.2     haad /* constant dm_target structures for error, zero, linear, stripes etc. */
    176   1.2     haad typedef struct dm_target {
    177   1.2     haad 	char name[DM_MAX_TYPE_NAME];
    178   1.2     haad 	/* Initialize target_config area */
    179  1.39  tkusumi 	int (*init)(dm_table_entry_t *, int, char **);
    180   1.2     haad 
    181   1.2     haad 	/* Destroy target_config area */
    182   1.2     haad 	int (*destroy)(dm_table_entry_t *);
    183  1.23    ahoka 
    184   1.2     haad 	int (*strategy)(dm_table_entry_t *, struct buf *);
    185  1.55  tkusumi 	//int (*upcall)(dm_table_entry_t *, struct buf *);
    186  1.51  tkusumi 
    187  1.44  tkusumi 	/*
    188  1.44  tkusumi 	 * Optional routines.
    189  1.44  tkusumi 	 */
    190  1.52  tkusumi 	/*
    191  1.52  tkusumi 	 * Info/table routine are called to get params string, which is target
    192  1.52  tkusumi 	 * specific. When dm_table_status_ioctl is called with flag
    193  1.52  tkusumi 	 * DM_STATUS_TABLE_FLAG I have to sent params string back.
    194  1.52  tkusumi 	 */
    195  1.51  tkusumi 	char *(*info)(void *);
    196  1.52  tkusumi 	char *(*table)(void *);
    197  1.45  tkusumi 	int (*sync)(dm_table_entry_t *);
    198  1.43  tkusumi 	int (*secsize)(dm_table_entry_t *, unsigned int *);
    199  1.23    ahoka 
    200   1.2     haad 	uint32_t version[3];
    201  1.27   justin 	uint32_t ref_cnt;
    202  1.39  tkusumi 	int max_argc;
    203  1.23    ahoka 
    204   1.2     haad 	TAILQ_ENTRY(dm_target) dm_target_next;
    205   1.2     haad } dm_target_t;
    206   1.2     haad 
    207   1.2     haad /* device-mapper */
    208  1.14     haad void dmgetproperties(struct disk *, dm_table_head_t *);
    209   1.2     haad 
    210  1.46  tkusumi /* Generic function used to convert char to string */
    211  1.46  tkusumi uint64_t atoi64(const char *);
    212  1.46  tkusumi 
    213   1.2     haad /* dm_ioctl.c */
    214   1.2     haad int dm_dev_create_ioctl(prop_dictionary_t);
    215   1.2     haad int dm_dev_list_ioctl(prop_dictionary_t);
    216   1.2     haad int dm_dev_remove_ioctl(prop_dictionary_t);
    217   1.2     haad int dm_dev_rename_ioctl(prop_dictionary_t);
    218   1.2     haad int dm_dev_resume_ioctl(prop_dictionary_t);
    219   1.2     haad int dm_dev_status_ioctl(prop_dictionary_t);
    220   1.2     haad int dm_dev_suspend_ioctl(prop_dictionary_t);
    221   1.2     haad 
    222   1.2     haad int dm_check_version(prop_dictionary_t);
    223   1.2     haad int dm_list_versions_ioctl(prop_dictionary_t);
    224   1.2     haad 
    225   1.2     haad int dm_table_clear_ioctl(prop_dictionary_t);
    226   1.2     haad int dm_table_deps_ioctl(prop_dictionary_t);
    227   1.2     haad int dm_table_load_ioctl(prop_dictionary_t);
    228   1.2     haad int dm_table_status_ioctl(prop_dictionary_t);
    229   1.2     haad 
    230   1.2     haad /* dm_target.c */
    231   1.4     haad dm_target_t* dm_target_alloc(const char *);
    232   1.9     haad dm_target_t* dm_target_autoload(const char *);
    233   1.2     haad int dm_target_destroy(void);
    234   1.2     haad int dm_target_insert(dm_target_t *);
    235   1.2     haad prop_array_t dm_target_prop_list(void);
    236   1.3     haad dm_target_t* dm_target_lookup(const char *);
    237  1.36  tkusumi int dm_target_rem(const char *);
    238   1.3     haad void dm_target_unbusy(dm_target_t *);
    239   1.3     haad void dm_target_busy(dm_target_t *);
    240   1.2     haad int dm_target_init(void);
    241   1.2     haad 
    242  1.13     haad #define DM_MAX_PARAMS_SIZE 1024
    243  1.13     haad 
    244   1.2     haad /* dm_target_linear.c */
    245  1.39  tkusumi int dm_target_linear_init(dm_table_entry_t *, int, char **);
    246  1.42  tkusumi char *dm_target_linear_table(void *);
    247   1.2     haad int dm_target_linear_strategy(dm_table_entry_t *, struct buf *);
    248  1.18     haad int dm_target_linear_sync(dm_table_entry_t *);
    249   1.2     haad int dm_target_linear_destroy(dm_table_entry_t *);
    250  1.55  tkusumi //int dm_target_linear_upcall(dm_table_entry_t *, struct buf *);
    251  1.43  tkusumi int dm_target_linear_secsize(dm_table_entry_t *, unsigned int *);
    252   1.2     haad 
    253   1.5     haad /* dm_target_stripe.c */
    254  1.39  tkusumi int dm_target_stripe_init(dm_table_entry_t *, int, char **);
    255  1.51  tkusumi char *dm_target_stripe_info(void *);
    256  1.42  tkusumi char *dm_target_stripe_table(void *);
    257   1.5     haad int dm_target_stripe_strategy(dm_table_entry_t *, struct buf *);
    258  1.18     haad int dm_target_stripe_sync(dm_table_entry_t *);
    259   1.5     haad int dm_target_stripe_destroy(dm_table_entry_t *);
    260  1.55  tkusumi //int dm_target_stripe_upcall(dm_table_entry_t *, struct buf *);
    261  1.43  tkusumi int dm_target_stripe_secsize(dm_table_entry_t *, unsigned int *);
    262   1.5     haad 
    263  1.47  tkusumi /* dm_target_error.c */
    264  1.47  tkusumi int dm_target_error_init(dm_table_entry_t*, int, char **);
    265  1.47  tkusumi int dm_target_error_strategy(dm_table_entry_t *, struct buf *);
    266  1.47  tkusumi int dm_target_error_destroy(dm_table_entry_t *);
    267  1.55  tkusumi //int dm_target_error_upcall(dm_table_entry_t *, struct buf *);
    268  1.47  tkusumi 
    269  1.47  tkusumi /* dm_target_zero.c */
    270  1.47  tkusumi int dm_target_zero_init(dm_table_entry_t *, int, char **);
    271  1.47  tkusumi int dm_target_zero_strategy(dm_table_entry_t *, struct buf *);
    272  1.47  tkusumi int dm_target_zero_destroy(dm_table_entry_t *);
    273  1.55  tkusumi //int dm_target_zero_upcall(dm_table_entry_t *, struct buf *);
    274  1.47  tkusumi 
    275  1.53  tkusumi #if 0
    276  1.54  tkusumi /* dm_target_delay.c */
    277  1.54  tkusumi void dm_target_delay_pool_create(void);
    278  1.54  tkusumi void dm_target_delay_pool_destroy(void);
    279  1.54  tkusumi int dm_target_delay_init(dm_table_entry_t *, int, char **);
    280  1.54  tkusumi char *dm_target_delay_info(void *);
    281  1.54  tkusumi char *dm_target_delay_table(void *);
    282  1.54  tkusumi int dm_target_delay_strategy(dm_table_entry_t *, struct buf *);
    283  1.54  tkusumi int dm_target_delay_sync(dm_table_entry_t *);
    284  1.54  tkusumi int dm_target_delay_destroy(dm_table_entry_t *);
    285  1.55  tkusumi //int dm_target_delay_upcall(dm_table_entry_t *, struct buf *);
    286  1.54  tkusumi int dm_target_delay_secsize(dm_table_entry_t *, unsigned int *);
    287  1.54  tkusumi 
    288  1.53  tkusumi /* dm_target_flakey.c */
    289  1.53  tkusumi int dm_target_flakey_init(dm_table_entry_t *, int, char **);
    290  1.53  tkusumi char *dm_target_flakey_table(void *);
    291  1.53  tkusumi int dm_target_flakey_strategy(dm_table_entry_t *, struct buf *);
    292  1.53  tkusumi int dm_target_flakey_sync(dm_table_entry_t *);
    293  1.53  tkusumi int dm_target_flakey_destroy(dm_table_entry_t *);
    294  1.55  tkusumi //int dm_target_flakey_upcall(dm_table_entry_t *, struct buf *);
    295  1.53  tkusumi int dm_target_flakey_secsize(dm_table_entry_t *, unsigned int *);
    296  1.53  tkusumi #endif
    297  1.53  tkusumi 
    298   1.2     haad /* dm_table.c  */
    299   1.2     haad #define DM_TABLE_ACTIVE 0
    300   1.2     haad #define DM_TABLE_INACTIVE 1
    301   1.2     haad 
    302   1.2     haad int dm_table_destroy(dm_table_head_t *, uint8_t);
    303   1.2     haad uint64_t dm_table_size(dm_table_head_t *);
    304  1.23    ahoka uint64_t dm_inactive_table_size(dm_table_head_t *);
    305  1.43  tkusumi void dm_table_disksize(dm_table_head_t *, uint64_t *, unsigned int *);
    306  1.34  tkusumi dm_table_t *dm_table_get_entry(dm_table_head_t *, uint8_t);
    307   1.2     haad int dm_table_get_target_count(dm_table_head_t *, uint8_t);
    308   1.2     haad void dm_table_release(dm_table_head_t *, uint8_t s);
    309   1.2     haad void dm_table_switch_tables(dm_table_head_t *);
    310   1.2     haad void dm_table_head_init(dm_table_head_t *);
    311   1.2     haad void dm_table_head_destroy(dm_table_head_t *);
    312  1.50  tkusumi int dm_table_add_deps(dm_table_entry_t *, dm_pdev_t *);
    313   1.2     haad 
    314   1.2     haad /* dm_dev.c */
    315   1.2     haad dm_dev_t* dm_dev_alloc(void);
    316   1.2     haad void dm_dev_busy(dm_dev_t *);
    317   1.2     haad int dm_dev_destroy(void);
    318  1.17     haad dm_dev_t* dm_dev_detach(device_t);
    319   1.2     haad int dm_dev_free(dm_dev_t *);
    320   1.2     haad int dm_dev_init(void);
    321   1.2     haad int dm_dev_insert(dm_dev_t *);
    322   1.2     haad dm_dev_t* dm_dev_lookup(const char *, const char *, int);
    323   1.2     haad prop_array_t dm_dev_prop_list(void);
    324   1.2     haad dm_dev_t* dm_dev_rem(const char *, const char *, int);
    325   1.2     haad /*int dm_dev_test_minor(int);*/
    326   1.2     haad void dm_dev_unbusy(dm_dev_t *);
    327   1.2     haad 
    328   1.2     haad /* dm_pdev.c */
    329   1.2     haad int dm_pdev_decr(dm_pdev_t *);
    330   1.2     haad int dm_pdev_destroy(void);
    331   1.2     haad int dm_pdev_init(void);
    332   1.2     haad dm_pdev_t* dm_pdev_insert(const char *);
    333   1.2     haad 
    334   1.2     haad #endif /*_KERNEL*/
    335   1.2     haad 
    336  1.48  tkusumi #endif /*_DM_H_*/
    337