2 * 2007+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/blkdev.h>
19 #include <linux/bio.h>
20 #include <linux/buffer_head.h>
21 #include <linux/connector.h>
22 #include <linux/dst.h>
23 #include <linux/device.h>
24 #include <linux/jhash.h>
25 #include <linux/idr.h>
26 #include <linux/init.h>
27 #include <linux/namei.h>
28 #include <linux/slab.h>
29 #include <linux/socket.h>
32 #include <linux/in6.h>
38 static DEFINE_MUTEX(dst_hash_lock);
39 static struct list_head *dst_hashtable;
40 static unsigned int dst_hashtable_size = 128;
41 module_param(dst_hashtable_size, uint, 0644);
43 static char dst_name[] = "Dementianting goldfish";
45 static DEFINE_IDR(dst_index_idr);
46 static struct cb_id cn_dst_id = { CN_DST_IDX, CN_DST_VAL };
49 * DST sysfs tree for device called 'storage':
51 * /sys/bus/dst/devices/storage/
52 * /sys/bus/dst/devices/storage/type : 192.168.4.80:1025
53 * /sys/bus/dst/devices/storage/size : 800
54 * /sys/bus/dst/devices/storage/name : storage
57 static int dst_dev_match(struct device *dev, struct device_driver *drv)
62 static struct bus_type dst_dev_bus_type = {
64 .match = &dst_dev_match,
67 static void dst_node_release(struct device *dev)
69 struct dst_info *info = container_of(dev, struct dst_info, device);
74 static struct device dst_node_dev = {
75 .bus = &dst_dev_bus_type,
76 .release = &dst_node_release
80 * Setting size of the node after it was changed.
82 static void dst_node_set_size(struct dst_node *n)
84 struct block_device *bdev;
86 set_capacity(n->disk, n->size >> 9);
88 bdev = bdget_disk(n->disk, 0);
90 mutex_lock(&bdev->bd_inode->i_mutex);
91 i_size_write(bdev->bd_inode, n->size);
92 mutex_unlock(&bdev->bd_inode->i_mutex);
98 * Distributed storage request processing function.
100 static int dst_request(struct request_queue *q, struct bio *bio)
102 struct dst_node *n = q->queuedata;
106 return dst_process_bio(n, bio);
110 * Open/close callbacks for appropriate block device.
112 static int dst_bdev_open(struct block_device *bdev, fmode_t mode)
114 struct dst_node *n = bdev->bd_disk->private_data;
120 static int dst_bdev_release(struct gendisk *disk, fmode_t mode)
122 struct dst_node *n = disk->private_data;
128 static struct block_device_operations dst_blk_ops = {
129 .open = dst_bdev_open,
130 .release = dst_bdev_release,
131 .owner = THIS_MODULE,
135 * Block layer binding - disk is created when array is fully configured
136 * by userspace request.
138 static int dst_node_create_disk(struct dst_node *n)
143 n->queue = blk_init_queue(NULL, NULL);
147 n->queue->queuedata = n;
148 blk_queue_make_request(n->queue, dst_request);
149 blk_queue_max_phys_segments(n->queue, n->max_pages);
150 blk_queue_max_hw_segments(n->queue, n->max_pages);
153 n->disk = alloc_disk(1);
155 goto err_out_free_queue;
157 if (!(n->state->permissions & DST_PERM_WRITE)) {
158 printk(KERN_INFO "DST node %s attached read-only.\n", n->name);
159 set_disk_ro(n->disk, 1);
162 if (!idr_pre_get(&dst_index_idr, GFP_KERNEL))
165 mutex_lock(&dst_hash_lock);
166 err = idr_get_new(&dst_index_idr, NULL, &index);
167 mutex_unlock(&dst_hash_lock);
171 n->disk->major = dst_major;
172 n->disk->first_minor = index;
173 n->disk->fops = &dst_blk_ops;
174 n->disk->queue = n->queue;
175 n->disk->private_data = n;
176 snprintf(n->disk->disk_name, sizeof(n->disk->disk_name), "dst-%s", n->name);
183 blk_cleanup_queue(n->queue);
189 * Sysfs machinery: show device's size.
191 static ssize_t dst_show_size(struct device *dev,
192 struct device_attribute *attr, char *buf)
194 struct dst_info *info = container_of(dev, struct dst_info, device);
196 return sprintf(buf, "%llu\n", info->size);
200 * Show local exported device.
202 static ssize_t dst_show_local(struct device *dev,
203 struct device_attribute *attr, char *buf)
205 struct dst_info *info = container_of(dev, struct dst_info, device);
207 return sprintf(buf, "%s\n", info->local);
211 * Shows type of the remote node - device major/minor number
212 * for local nodes and address (af_inet ipv4/ipv6 only) for remote nodes.
214 static ssize_t dst_show_type(struct device *dev,
215 struct device_attribute *attr, char *buf)
217 struct dst_info *info = container_of(dev, struct dst_info, device);
218 int family = info->net.addr.sa_family;
220 if (family == AF_INET) {
221 struct sockaddr_in *sin = (struct sockaddr_in *)&info->net.addr;
222 return sprintf(buf, "%u.%u.%u.%u:%d\n",
223 NIPQUAD(sin->sin_addr.s_addr), ntohs(sin->sin_port));
224 } else if (family == AF_INET6) {
225 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)&info->net.addr;
228 &sin->sin6_addr, ntohs(sin->sin6_port));
230 int i, sz = PAGE_SIZE - 2; /* 0 symbol and '\n' below */
231 int size, addrlen = info->net.addr.sa_data_len;
232 unsigned char *a = (unsigned char *)&info->net.addr.sa_data;
233 char *buf_orig = buf;
235 size = snprintf(buf, sz, "family: %d, addrlen: %u, addr: ",
240 for (i=0; i<addrlen; ++i) {
244 size = snprintf(buf, sz, "%02x ", a[i]);
248 buf += sprintf(buf, "\n");
250 return buf - buf_orig;
255 static struct device_attribute dst_node_attrs[] = {
256 __ATTR(size, 0444, dst_show_size, NULL),
257 __ATTR(type, 0444, dst_show_type, NULL),
258 __ATTR(local, 0444, dst_show_local, NULL),
261 static int dst_create_node_attributes(struct dst_node *n)
265 for (i=0; i<ARRAY_SIZE(dst_node_attrs); ++i) {
266 err = device_create_file(&n->info->device,
269 goto err_out_remove_all;
275 device_remove_file(&n->info->device,
281 static void dst_remove_node_attributes(struct dst_node *n)
285 for (i=0; i<ARRAY_SIZE(dst_node_attrs); ++i)
286 device_remove_file(&n->info->device,
291 * Sysfs cleanup and initialization.
292 * Shows number of useful parameters.
294 static void dst_node_sysfs_exit(struct dst_node *n)
297 dst_remove_node_attributes(n);
298 device_unregister(&n->info->device);
303 static int dst_node_sysfs_init(struct dst_node *n)
307 n->info = kzalloc(sizeof(struct dst_info), GFP_KERNEL);
311 memcpy(&n->info->device, &dst_node_dev, sizeof(struct device));
312 n->info->size = n->size;
314 snprintf(n->info->device.bus_id, sizeof(n->info->device.bus_id), "dst-%s", n->name);
315 err = device_register(&n->info->device);
317 dprintk(KERN_ERR "Failed to register node '%s', err: %d.\n",
322 dst_create_node_attributes(n);
333 * DST node hash tables machinery.
335 static inline unsigned int dst_hash(char *str, unsigned int size)
337 return (jhash(str, size, 0) % dst_hashtable_size);
340 static void dst_node_remove(struct dst_node *n)
342 mutex_lock(&dst_hash_lock);
343 list_del_init(&n->node_entry);
344 mutex_unlock(&dst_hash_lock);
347 static void dst_node_add(struct dst_node *n)
349 unsigned hash = dst_hash(n->name, sizeof(n->name));
351 mutex_lock(&dst_hash_lock);
352 list_add_tail(&n->node_entry, &dst_hashtable[hash]);
353 mutex_unlock(&dst_hash_lock);
357 * Cleaning node when it is about to be freed.
358 * There are still users of the socket though,
359 * so connection cleanup should be protected.
361 static void dst_node_cleanup(struct dst_node *n)
363 struct dst_state *st = n->state;
369 blk_cleanup_queue(n->queue);
371 mutex_lock(&dst_hash_lock);
372 idr_remove(&dst_index_idr, n->disk->first_minor);
373 mutex_unlock(&dst_hash_lock);
379 sync_blockdev(n->bdev);
380 blkdev_put(n->bdev, FMODE_READ|FMODE_WRITE);
385 dst_state_exit_connected(st);
386 dst_state_unlock(st);
388 wake_up(&st->thread_wait);
395 * Free security attributes attached to given node.
397 static void dst_security_exit(struct dst_node *n)
399 struct dst_secure *s, *tmp;
401 list_for_each_entry_safe(s, tmp, &n->security_list, sec_entry) {
402 list_del(&s->sec_entry);
408 * Free node when there are no more users.
409 * Actually node has to be freed on behalf od userspace process,
410 * since there are number of threads, which are embedded in the
411 * node, so they can not exit and free node from there, that is
412 * why there is a wakeup if reference counter is not equal to zero.
414 void dst_node_put(struct dst_node *n)
419 dprintk("%s: n: %p, refcnt: %d.\n",
420 __func__, n, atomic_read(&n->refcnt));
422 if (atomic_dec_and_test(&n->refcnt)) {
424 n->trans_scan_timeout = 0;
426 thread_pool_destroy(n->pool);
427 dst_node_sysfs_exit(n);
428 dst_node_crypto_exit(n);
429 dst_security_exit(n);
430 dst_node_trans_exit(n);
434 dprintk("%s: freed n: %p.\n", __func__, n);
441 * This function finds devices major/minor numbers for given pathname.
443 static int dst_lookup_device(const char *path, dev_t *dev)
449 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
453 inode = nd.path.dentry->d_inode;
459 if (!S_ISBLK(inode->i_mode)) {
464 *dev = inode->i_rdev;
472 * Setting up export device: lookup by the name, get its size
473 * and setup listening socket, which will accept clients, which
474 * will submit IO for given storage.
476 static int dst_setup_export(struct dst_node *n, struct dst_ctl *ctl,
477 struct dst_export_ctl *le)
480 dev_t dev = 0; /* gcc likes to scream here */
482 snprintf(n->info->local, sizeof(n->info->local), "%s", le->device);
484 err = dst_lookup_device(le->device, &dev);
488 n->bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
493 n->size = min_t(loff_t, n->bdev->bd_inode->i_size, n->size);
495 n->size = n->bdev->bd_inode->i_size;
497 n->info->size = n->size;
498 err = dst_node_init_listened(n, le);
500 goto err_out_cleanup;
505 blkdev_put(n->bdev, FMODE_READ|FMODE_WRITE);
511 /* Empty thread pool callbacks for the network processing threads. */
512 static inline void *dst_thread_network_init(void *data)
514 dprintk("%s: data: %p.\n", __func__, data);
518 static inline void dst_thread_network_cleanup(void *data)
520 dprintk("%s: data: %p.\n", __func__, data);
524 * Allocate DST node and initialize some of its parameters.
526 static struct dst_node *dst_alloc_node(struct dst_ctl *ctl,
527 int (*start)(struct dst_node *),
533 n = kzalloc(sizeof(struct dst_node), GFP_KERNEL);
537 INIT_LIST_HEAD(&n->node_entry);
539 INIT_LIST_HEAD(&n->security_list);
540 mutex_init(&n->security_lock);
542 init_waitqueue_head(&n->wait);
544 n->trans_scan_timeout = msecs_to_jiffies(ctl->trans_scan_timeout);
545 if (!n->trans_scan_timeout)
546 n->trans_scan_timeout = HZ;
548 n->trans_max_retries = ctl->trans_max_retries;
549 if (!n->trans_max_retries)
550 n->trans_max_retries = 10;
553 * Pretty much arbitrary default numbers.
554 * 32 matches maximum number of pages in bio originated from ext3 (31).
556 n->max_pages = ctl->max_pages;
560 if (n->max_pages > 1024)
566 atomic_set(&n->refcnt, 1);
567 atomic_long_set(&n->gen, 0);
568 snprintf(n->name, sizeof(n->name), "%s", ctl->name);
570 err = dst_node_sysfs_init(n);
574 n->pool = thread_pool_create(num, n->name, dst_thread_network_init,
575 dst_thread_network_cleanup, n);
576 if (IS_ERR(n->pool)) {
577 err = PTR_ERR(n->pool);
578 goto err_out_sysfs_exit;
581 dprintk("%s: n: %p, name: %s.\n", __func__, n, n->name);
586 dst_node_sysfs_exit(n);
593 * Starting a node, connected to the remote server:
594 * register block device and initialize transaction mechanism.
595 * In revers order though.
597 * It will autonegotiate some parameters with the remote node
598 * and update local if needed.
600 * Transaction initialization should be the last thing before
601 * starting the node, since transaction should include not only
602 * block IO, but also crypto related data (if any), which are
603 * initialized separately.
605 static int dst_start_remote(struct dst_node *n)
609 err = dst_node_trans_init(n, sizeof(struct dst_trans));
613 err = dst_node_create_disk(n);
617 dst_node_set_size(n);
620 dprintk("DST: started remote node '%s', minor: %d.\n", n->name, n->disk->first_minor);
626 * Adding remote node and initialize connection.
628 static int dst_add_remote(struct dst_node *n, struct dst_ctl *ctl,
629 void *data, unsigned int size)
632 struct dst_network_ctl *rctl = data;
637 if (size != sizeof(struct dst_network_ctl))
640 n = dst_alloc_node(ctl, dst_start_remote, 1);
644 memcpy(&n->info->net, rctl, sizeof(struct dst_network_ctl));
645 err = dst_node_init_connected(n, rctl);
659 * Adding export node: initializing block device and listening socket.
661 static int dst_add_export(struct dst_node *n, struct dst_ctl *ctl,
662 void *data, unsigned int size)
665 struct dst_export_ctl *le = data;
670 if (size != sizeof(struct dst_export_ctl))
673 n = dst_alloc_node(ctl, dst_start_export, 2);
677 err = dst_setup_export(n, ctl, le);
690 static int dst_node_remove_unload(struct dst_node *n)
692 printk(KERN_INFO "STOPPED name: '%s', size: %llu.\n",
696 del_gendisk(n->disk);
699 dst_node_sysfs_exit(n);
702 * This is not a hack. Really.
703 * Node's reference counter allows to implement fine grained
704 * node freeing, but since all transactions (which hold node's
705 * reference counter) are processed in the dedicated thread,
706 * it is possible that reference will hit zero in that thread,
707 * so we will not be able to exit thread and cleanup the node.
709 * So, we remove disk, so no new activity is possible, and
710 * wait until all pending transaction are completed (either
711 * in receiving thread or by timeout in workqueue), in this
712 * case reference counter will be less or equal to 2 (once set in
713 * dst_alloc_node() and then in connector message parser;
714 * or when we force module unloading, and connector message
715 * parser does not hold a reference, in this case reference
716 * counter will be equal to 1),
717 * and subsequent dst_node_put() calls will free the node.
719 dprintk("%s: going to sleep with %d refcnt.\n", __func__, atomic_read(&n->refcnt));
720 wait_event(n->wait, atomic_read(&n->refcnt) <= 2);
727 * Remove node from the hash table.
729 static int dst_del_node(struct dst_node *n, struct dst_ctl *ctl,
730 void *data, unsigned int size)
735 return dst_node_remove_unload(n);
739 * Initialize crypto processing for given node.
741 static int dst_crypto_init(struct dst_node *n, struct dst_ctl *ctl,
742 void *data, unsigned int size)
744 struct dst_crypto_ctl *crypto = data;
749 if (size != sizeof(struct dst_crypto_ctl) + crypto->hash_keysize +
750 crypto->cipher_keysize)
756 return dst_node_crypto_init(n, crypto);
760 * Security attributes for given node.
762 static int dst_security_init(struct dst_node *n, struct dst_ctl *ctl,
763 void *data, unsigned int size)
765 struct dst_secure *s;
770 if (size != sizeof(struct dst_secure_user))
773 s = kmalloc(sizeof(struct dst_secure), GFP_KERNEL);
777 memcpy(&s->sec, data, size);
779 mutex_lock(&n->security_lock);
780 list_add_tail(&s->sec_entry, &n->security_list);
781 mutex_unlock(&n->security_lock);
789 static int dst_start_node(struct dst_node *n, struct dst_ctl *ctl,
790 void *data, unsigned int size)
804 printk(KERN_INFO "STARTED name: '%s', size: %llu.\n", n->name, n->size);
808 typedef int (*dst_command_func)(struct dst_node *n, struct dst_ctl *ctl,
809 void *data, unsigned int size);
812 * List of userspace commands.
814 static dst_command_func dst_commands[] = {
815 [DST_ADD_REMOTE] = &dst_add_remote,
816 [DST_ADD_EXPORT] = &dst_add_export,
817 [DST_DEL_NODE] = &dst_del_node,
818 [DST_CRYPTO] = &dst_crypto_init,
819 [DST_SECURITY] = &dst_security_init,
820 [DST_START] = &dst_start_node,
824 * Configuration parser.
826 static void cn_dst_callback(void *data)
829 struct cn_msg *msg = data;
831 struct dst_ctl_ack ack;
832 struct dst_node *n = NULL, *tmp;
835 if (msg->len < sizeof(struct dst_ctl)) {
840 ctl = (struct dst_ctl *)msg->data;
842 if (ctl->cmd >= DST_CMD_MAX) {
846 hash = dst_hash(ctl->name, sizeof(ctl->name));
848 mutex_lock(&dst_hash_lock);
849 list_for_each_entry(tmp, &dst_hashtable[hash], node_entry) {
850 if (!memcmp(tmp->name, ctl->name, sizeof(tmp->name))) {
856 mutex_unlock(&dst_hash_lock);
858 err = dst_commands[ctl->cmd](n, ctl, msg->data + sizeof(struct dst_ctl),
859 msg->len - sizeof(struct dst_ctl));
863 memcpy(&ack.msg, msg, sizeof(struct cn_msg));
865 ack.msg.ack = msg->ack + 1;
866 ack.msg.len = sizeof(struct dst_ctl_ack) - sizeof(struct cn_msg);
870 cn_netlink_send(&ack.msg, 0, GFP_KERNEL);
874 * Global initialization: sysfs, hash table, block device registration,
875 * connector and various caches.
877 static int __init dst_sysfs_init(void)
879 return bus_register(&dst_dev_bus_type);
882 static void dst_sysfs_exit(void)
884 bus_unregister(&dst_dev_bus_type);
887 static int __init dst_hashtable_init(void)
891 dst_hashtable = kcalloc(dst_hashtable_size, sizeof(struct list_head),
896 for (i=0; i<dst_hashtable_size; ++i)
897 INIT_LIST_HEAD(&dst_hashtable[i]);
902 static void dst_hashtable_exit(void)
905 struct dst_node *n, *tmp;
907 for (i=0; i<dst_hashtable_size; ++i) {
908 list_for_each_entry_safe(n, tmp, &dst_hashtable[i], node_entry) {
909 dst_node_remove_unload(n);
913 kfree(dst_hashtable);
916 static int __init dst_sys_init(void)
920 err = dst_hashtable_init();
924 err = dst_export_init();
926 goto err_out_hashtable_exit;
928 err = register_blkdev(dst_major, DST_NAME);
930 goto err_out_export_exit;
934 err = dst_sysfs_init();
936 goto err_out_unregister;
938 err = cn_add_callback(&cn_dst_id, "DST", cn_dst_callback);
940 goto err_out_sysfs_exit;
942 printk(KERN_INFO "Distributed storage, '%s' release.\n", dst_name);
949 unregister_blkdev(dst_major, DST_NAME);
952 err_out_hashtable_exit:
953 dst_hashtable_exit();
958 static void __exit dst_sys_exit(void)
960 cn_del_callback(&cn_dst_id);
961 unregister_blkdev(dst_major, DST_NAME);
962 dst_hashtable_exit();
967 module_init(dst_sys_init);
968 module_exit(dst_sys_exit);
970 MODULE_DESCRIPTION("Distributed storage");
971 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
972 MODULE_LICENSE("GPL");