Skip to contentSkip to conversation

device-mapper: Add userspace target

PATCH New Dan Smith 1 participant 1 message Jul 27, 2006

Subsystems (2)
lore
Share & tools
lore

Dan Smith

author
#0 lore raw

PATCH 1/1 device-mapper: Add userspace target

This patch adds a userspace target to device-mapper. This version includes the following changes as requested:

- Removed debug messages
- Block device targets must be registered at table creation time
- Chardev operations check for root capabilities
- Eliminate the *need* to allocate memory on the critical IO path
- Broke up the interface between the kernel and the userspace library
- Insulated the consumer of libdevmapper from the details of dm-userspace kernel interface, allowing for future expansion without breaking compatibility
- Split dm-userspace.c into separate chunks (along the line between the target-specific and userspace-transport pieces)
- Added synchronous IO completion notification and control to allow userspace to enforce metadata consistency where needed.

A patch to libdevmapper for dm-userspace support will be sent to dm-devel.

drivers/md/dm-user.h +208 -0
diff -Naur linux-2.6.17.6-orig/drivers/md/dm-user.h linux-2.6.17.6-dmu/drivers/md/dm-user.h
@@ -0,0 +1,208 @@
+/*
+ * Copyright (C) International Business Machines Corp., 2006
+ * Author: Dan Smith <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; under version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#ifndef __DM_USER_H
+#define __DM_USER_H
+
+#define DMU_KEY_LEN 256
+
+extern struct target_type userspace_target;
+extern mempool_t *request_pool;
+extern dev_t dmu_dev;
+extern spinlock_t devices_lock;
+extern struct list_head devices;
+
+/*
+ * A hash table of remaps
+ */
+struct hash_table {
+	struct list_head *table;      /* Array of lists (buckets)           */
+	uint64_t size;                /* Number of buckets                  */
+	uint32_t mask;                /* Mask used to determine bucket      */
+	uint64_t count;               /* Number of remaps in entire table   */
+};
+
+/*
+ * A block device that we can send bios to
+ */
+struct target_device {
+	struct list_head list;        /* Our place in the targets list      */
+	struct block_device *bdev;    /* The target block_device            */
+	struct kref users;            /* Self-destructing reference count   */
+};
+
+/*
+ * A dm-userspace device, which consists of multiple targets sharing a
+ * common key
+ */
+struct dmu_device {
+	struct list_head list;        /* Our place in the devices list     */
+
+	spinlock_t lock;              /* Protects all the fields below     */
+
+	struct list_head requests;    /* List of pending requests          */
+	struct list_head target_devs; /* List of devices we can target     */
+	struct list_head in_flight;   /* List of pending remaps            */
+	struct hash_table remaps;     /* Hash table of all our maps        */
+
+	void *transport_private;      /* Private data for userspace comms  */
+
+	char key[DMU_KEY_LEN];        /* Unique name string for device     */
+	struct kref users;            /* Self-destructing reference count  */
+
+	wait_queue_head_t wqueue;     /* To block while waiting for reqs   */
+
+	uint64_t block_size;          /* Block size for this device        */
+	uint64_t block_mask;          /* Mask for offset in block          */
+	unsigned int block_shift;     /* Shift to convert to/from block    */
+
+	struct kcopyd_client *kcopy;  /* Interface to kcopyd               */
+
+	uint32_t id_counter;          /* Used to generate request IDs      */
+};
+
+struct userspace_request {
+	struct list_head list;        /* Our place on the request queue    */
+
+	spinlock_t lock;              /* Protects all the fields below     */
+
+	struct dmu_device *dev;       /* The DMU device that owns us       */
+
+	int type;                     /* Type of request                   */
+	int sent;                     /* Non-zero if we've been sent       */
+	uint32_t flags;               /* Attribute flags                   */
+	uint32_t id;                  /* Unique ID for sync with userspace */
+	union {
+		struct bio_list bios; /* Bios waiting on this request      */
+		uint64_t block;       /* The block in question             */
+	} u;
+	atomic_t refcnt;              /* Reference count                   */
+};
+
+struct dmu_map {
+	struct list_head list;        /* Our place in a remap bucket chain */
+	struct list_head mru_list;    /* Our place on the MRU list         */
+	struct list_head in_flight;   /* Our place on the                  */
+
+	spinlock_t lock;              /* Protects all the fields below     */
+
+	uint64_t org_block;           /* Original block                    */
+	uint64_t new_block;           /* Destination block                 */
+	int64_t offset;               /* Sectors to offset remapped block  */
+	uint32_t flags;               /* Attribute flags                   */
+	uint32_t id;                  /* Unique ID for sync with userspace */
+
+	struct target_device *src;    /* Source blkdev for COPY_FIRST      */
+	struct target_device *dest;   /* Where the remapped block is       */
+
+	struct bio_list bios;         /* Bios queued for remapping         */
+	struct bio_list bios_waiting; /* Bios waiting for endio sync       */
+
+	struct dmu_device *dev;       /* The DMU device that owns us       */
+	struct dmu_map *next;         /* Next remap that depends on us     */
+};
+
+/* Find and grab a reference to a target device */
+struct target_device *find_target(struct dmu_device *dev,
+				  dev_t devno);
+
+/* Object allocation, destruction, and initialization routines */
+void init_remap(struct dmu_device *dev, struct dmu_map *remap);
+void init_request(struct dmu_device *dev,
+		  int type,
+		  struct userspace_request *req);
+void free_remap(struct dmu_map *remap);
+void __free_remap(struct dmu_map *remap);
+struct dmu_map *alloc_remap_atomic(struct dmu_device *dev);
+
+/* Hash table manipulation */
+struct dmu_map *ht_find_map(struct hash_table *ht, uint64_t block);
+void ht_insert_map(struct hash_table *ht, struct dmu_map *map);
+struct dmu_map *ht_find_map_dev(struct dmu_device *dev, uint64_t block);
+void ht_delete_map(struct hash_table *ht, struct dmu_map *map);
+
+/* Character device transport functions */
+int register_chardev_transport(struct dmu_device *dev);
+void unregister_chardev_transport(struct dmu_device *dev);
+int init_chardev_transport(void);
+void cleanup_chardev_transport(void);
+void write_chardev_transport_info(struct dmu_device *dev,
+				  char *buf, unsigned int maxlen);
+
+/* Return the block number for @sector */
+static inline u64 dmu_block(struct dmu_device *dev,
+			    sector_t sector)
+{
+	return sector >> dev->block_shift;
+}
+
+/* Return the sector offset in a block for @sector */
+static inline u64 dmu_sector_offset(struct dmu_device *dev,
+				    sector_t sector)
+{
+	return sector & dev->block_mask;
+}
+
+/* Return the starting sector for @block */
+static inline u64 dmu_sector(struct dmu_device *dev,
+			     uint64_t block)
+{
+	return block << dev->block_shift;
+}
+
+/* Add a request to a device's request queue */
+static void add_request(struct dmu_device *dev,
+			struct userspace_request *req)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&dev->lock, flags);
+	list_add_tail(&req->list, &dev->requests);
+	spin_unlock_irqrestore(&dev->lock, flags);
+
+	wake_up(&dev->wqueue);
+}
+
+/* Remap @bio based on the information in @remap */
+static void __bio_remap(struct bio *bio,
+			struct dmu_map *remap)
+{
+	BUG_ON(remap->dest == NULL);
+
+	bio->bi_sector = dmu_sector(remap->dev, remap->new_block) +
+		dmu_sector_offset(remap->dev, bio->bi_sector) +
+		remap->offset;
+
+	bio->bi_bdev = remap->dest->bdev;
+}
+
+/* Increase the usage count for @dev */
+static inline void get_dev(struct dmu_device *dev)
+{
+	kref_get(&dev->users);
+}
+
+/* Decrease the usage count for @dev */
+void destroy_dmu_device(struct kref *ref);
+static inline void put_dev(struct dmu_device *dev)
+{
+	kref_put(&dev->users, destroy_dmu_device);
+}
+
+#endif
drivers/md/dm-userspace.c +1090 -0

1,090 lines not on this page · raw message

drivers/md/dm-userspace-chardev.c +856 -0

856 lines not on this page · raw message

drivers/md/Kconfig +6 -0
diff -Naur linux-2.6.17.6-orig/drivers/md/Kconfig linux-2.6.17.6-dmu/drivers/md/Kconfig
@@ -237,6 +237,12 @@
        ---help---
          Allow volume managers to take writeable snapshots of a device.
 
+config DM_USERSPACE
+       tristate "Userspace target (EXPERIMENTAL)"
+       depends on BLK_DEV_DM && EXPERIMENTAL
+       ---help---
+	 A target that provides a userspace interface to device-mapper
+
 config DM_MIRROR
        tristate "Mirror target (EXPERIMENTAL)"
        depends on BLK_DEV_DM && EXPERIMENTAL
drivers/md/Makefile +2 -0
diff -Naur linux-2.6.17.6-orig/drivers/md/Makefile linux-2.6.17.6-dmu/drivers/md/Makefile
@@ -14,6 +14,7 @@
 		   raid6altivec1.o raid6altivec2.o raid6altivec4.o \
 		   raid6altivec8.o \
 		   raid6mmx.o raid6sse1.o raid6sse2.o
+dm-user-objs    := dm-userspace.o dm-userspace-chardev.o
 hostprogs-y	:= mktables
 
 # Note: link order is important.  All raid personalities
@@ -37,6 +38,7 @@
 obj-$(CONFIG_DM_SNAPSHOT)	+= dm-snapshot.o
 obj-$(CONFIG_DM_MIRROR)		+= dm-mirror.o
 obj-$(CONFIG_DM_ZERO)		+= dm-zero.o
+obj-$(CONFIG_DM_USERSPACE)      += dm-user.o
 
 quiet_cmd_unroll = UNROLL  $@
       cmd_unroll = $(PERL) $(srctree)/$(src)/unroll.pl $(UNROLL) \
include/linux/dm-userspace.h +147 -0

147 lines not on this page · raw message

Signature · 4 lines
Dan Smith
IBM Linux Technology Center
Open Hypervisor Team
email: [email protected]