Commit | Line | Data |
---|---|---|
ce0d9d72 EP |
1 | /* |
2 | * 2007+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net> | |
3 | * All rights reserved. | |
4 | * | |
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. | |
9 | * | |
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. | |
14 | */ | |
15 | ||
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> | |
30 | ||
31 | #include <linux/in.h> | |
32 | #include <linux/in6.h> | |
33 | ||
34 | #include <net/sock.h> | |
35 | ||
36 | static int dst_major; | |
37 | ||
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); | |
42 | ||
43 | static char dst_name[] = "Dementianting goldfish"; | |
44 | ||
45 | static DEFINE_IDR(dst_index_idr); | |
46 | static struct cb_id cn_dst_id = { CN_DST_IDX, CN_DST_VAL }; | |
47 | ||
48 | /* | |
49 | * DST sysfs tree for device called 'storage': | |
50 | * | |
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 | |
55 | */ | |
56 | ||
57 | static int dst_dev_match(struct device *dev, struct device_driver *drv) | |
58 | { | |
59 | return 1; | |
60 | } | |
61 | ||
62 | static struct bus_type dst_dev_bus_type = { | |
63 | .name = "dst", | |
64 | .match = &dst_dev_match, | |
65 | }; | |
66 | ||
67 | static void dst_node_release(struct device *dev) | |
68 | { | |
69 | struct dst_info *info = container_of(dev, struct dst_info, device); | |
70 | ||
71 | kfree(info); | |
72 | } | |
73 | ||
74 | static struct device dst_node_dev = { | |
75 | .bus = &dst_dev_bus_type, | |
76 | .release = &dst_node_release | |
77 | }; | |
78 | ||
79 | /* | |
80 | * Setting size of the node after it was changed. | |
81 | */ | |
82 | static void dst_node_set_size(struct dst_node *n) | |
83 | { | |
84 | struct block_device *bdev; | |
85 | ||
86 | set_capacity(n->disk, n->size >> 9); | |
87 | ||
88 | bdev = bdget_disk(n->disk, 0); | |
89 | if (bdev) { | |
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); | |
93 | bdput(bdev); | |
94 | } | |
95 | } | |
96 | ||
97 | /* | |
98 | * Distributed storage request processing function. | |
99 | */ | |
100 | static int dst_request(struct request_queue *q, struct bio *bio) | |
101 | { | |
102 | struct dst_node *n = q->queuedata; | |
30c7c1c6 EP |
103 | int err = -EIO; |
104 | ||
c15227de | 105 | if (bio_empty_barrier(bio) && !blk_queue_discard(q)) { |
30c7c1c6 EP |
106 | /* |
107 | * This is a dirty^Wnice hack, but if we complete this | |
108 | * operation with -EOPNOTSUPP like intended, XFS | |
109 | * will stuck and freeze the machine. This may be | |
110 | * not particulary XFS problem though, but it is the | |
111 | * only FS which sends empty barrier at umount time | |
112 | * I worked with. | |
113 | * | |
114 | * Empty barriers are not allowed anyway, see 51fd77bd9f512 | |
1f98a13f JA |
115 | * for example, although later it was changed to |
116 | * bio_rw_flagged(bio, BIO_RW_DISCARD) only, which does not | |
117 | * work in this case. | |
30c7c1c6 | 118 | */ |
d52ac3f2 | 119 | /* err = -EOPNOTSUPP; */ |
30c7c1c6 EP |
120 | err = 0; |
121 | goto end_io; | |
122 | } | |
ce0d9d72 EP |
123 | |
124 | bio_get(bio); | |
125 | ||
126 | return dst_process_bio(n, bio); | |
30c7c1c6 EP |
127 | |
128 | end_io: | |
129 | bio_endio(bio, err); | |
130 | return err; | |
ce0d9d72 EP |
131 | } |
132 | ||
133 | /* | |
134 | * Open/close callbacks for appropriate block device. | |
135 | */ | |
136 | static int dst_bdev_open(struct block_device *bdev, fmode_t mode) | |
137 | { | |
138 | struct dst_node *n = bdev->bd_disk->private_data; | |
139 | ||
140 | dst_node_get(n); | |
141 | return 0; | |
142 | } | |
143 | ||
144 | static int dst_bdev_release(struct gendisk *disk, fmode_t mode) | |
145 | { | |
146 | struct dst_node *n = disk->private_data; | |
147 | ||
148 | dst_node_put(n); | |
149 | return 0; | |
150 | } | |
151 | ||
152 | static struct block_device_operations dst_blk_ops = { | |
153 | .open = dst_bdev_open, | |
154 | .release = dst_bdev_release, | |
155 | .owner = THIS_MODULE, | |
156 | }; | |
157 | ||
158 | /* | |
159 | * Block layer binding - disk is created when array is fully configured | |
160 | * by userspace request. | |
161 | */ | |
162 | static int dst_node_create_disk(struct dst_node *n) | |
163 | { | |
164 | int err = -ENOMEM; | |
165 | u32 index = 0; | |
166 | ||
167 | n->queue = blk_init_queue(NULL, NULL); | |
168 | if (!n->queue) | |
169 | goto err_out_exit; | |
170 | ||
171 | n->queue->queuedata = n; | |
172 | blk_queue_make_request(n->queue, dst_request); | |
173 | blk_queue_max_phys_segments(n->queue, n->max_pages); | |
174 | blk_queue_max_hw_segments(n->queue, n->max_pages); | |
175 | ||
176 | err = -ENOMEM; | |
177 | n->disk = alloc_disk(1); | |
178 | if (!n->disk) | |
179 | goto err_out_free_queue; | |
180 | ||
181 | if (!(n->state->permissions & DST_PERM_WRITE)) { | |
182 | printk(KERN_INFO "DST node %s attached read-only.\n", n->name); | |
183 | set_disk_ro(n->disk, 1); | |
184 | } | |
185 | ||
186 | if (!idr_pre_get(&dst_index_idr, GFP_KERNEL)) | |
187 | goto err_out_put; | |
188 | ||
189 | mutex_lock(&dst_hash_lock); | |
190 | err = idr_get_new(&dst_index_idr, NULL, &index); | |
191 | mutex_unlock(&dst_hash_lock); | |
192 | if (err) | |
193 | goto err_out_put; | |
194 | ||
195 | n->disk->major = dst_major; | |
196 | n->disk->first_minor = index; | |
197 | n->disk->fops = &dst_blk_ops; | |
198 | n->disk->queue = n->queue; | |
199 | n->disk->private_data = n; | |
d52ac3f2 MZ |
200 | snprintf(n->disk->disk_name, sizeof(n->disk->disk_name), |
201 | "dst-%s", n->name); | |
ce0d9d72 EP |
202 | |
203 | return 0; | |
204 | ||
205 | err_out_put: | |
206 | put_disk(n->disk); | |
207 | err_out_free_queue: | |
208 | blk_cleanup_queue(n->queue); | |
209 | err_out_exit: | |
210 | return err; | |
211 | } | |
212 | ||
213 | /* | |
214 | * Sysfs machinery: show device's size. | |
215 | */ | |
216 | static ssize_t dst_show_size(struct device *dev, | |
217 | struct device_attribute *attr, char *buf) | |
218 | { | |
219 | struct dst_info *info = container_of(dev, struct dst_info, device); | |
220 | ||
221 | return sprintf(buf, "%llu\n", info->size); | |
222 | } | |
223 | ||
224 | /* | |
225 | * Show local exported device. | |
226 | */ | |
227 | static ssize_t dst_show_local(struct device *dev, | |
228 | struct device_attribute *attr, char *buf) | |
229 | { | |
230 | struct dst_info *info = container_of(dev, struct dst_info, device); | |
231 | ||
232 | return sprintf(buf, "%s\n", info->local); | |
233 | } | |
234 | ||
235 | /* | |
236 | * Shows type of the remote node - device major/minor number | |
237 | * for local nodes and address (af_inet ipv4/ipv6 only) for remote nodes. | |
238 | */ | |
239 | static ssize_t dst_show_type(struct device *dev, | |
240 | struct device_attribute *attr, char *buf) | |
241 | { | |
242 | struct dst_info *info = container_of(dev, struct dst_info, device); | |
243 | int family = info->net.addr.sa_family; | |
244 | ||
245 | if (family == AF_INET) { | |
246 | struct sockaddr_in *sin = (struct sockaddr_in *)&info->net.addr; | |
247 | return sprintf(buf, "%u.%u.%u.%u:%d\n", | |
248 | NIPQUAD(sin->sin_addr.s_addr), ntohs(sin->sin_port)); | |
249 | } else if (family == AF_INET6) { | |
d52ac3f2 MZ |
250 | struct sockaddr_in6 *sin = (struct sockaddr_in6 *) |
251 | &info->net.addr; | |
ce0d9d72 EP |
252 | return sprintf(buf, |
253 | "%pi6:%d\n", | |
254 | &sin->sin6_addr, ntohs(sin->sin6_port)); | |
255 | } else { | |
256 | int i, sz = PAGE_SIZE - 2; /* 0 symbol and '\n' below */ | |
257 | int size, addrlen = info->net.addr.sa_data_len; | |
258 | unsigned char *a = (unsigned char *)&info->net.addr.sa_data; | |
259 | char *buf_orig = buf; | |
260 | ||
261 | size = snprintf(buf, sz, "family: %d, addrlen: %u, addr: ", | |
262 | family, addrlen); | |
263 | sz -= size; | |
264 | buf += size; | |
265 | ||
d52ac3f2 | 266 | for (i = 0; i < addrlen; ++i) { |
ce0d9d72 EP |
267 | if (sz < 3) |
268 | break; | |
269 | ||
270 | size = snprintf(buf, sz, "%02x ", a[i]); | |
271 | sz -= size; | |
272 | buf += size; | |
273 | } | |
274 | buf += sprintf(buf, "\n"); | |
275 | ||
276 | return buf - buf_orig; | |
277 | } | |
278 | return 0; | |
279 | } | |
280 | ||
281 | static struct device_attribute dst_node_attrs[] = { | |
282 | __ATTR(size, 0444, dst_show_size, NULL), | |
283 | __ATTR(type, 0444, dst_show_type, NULL), | |
284 | __ATTR(local, 0444, dst_show_local, NULL), | |
285 | }; | |
286 | ||
287 | static int dst_create_node_attributes(struct dst_node *n) | |
288 | { | |
289 | int err, i; | |
290 | ||
d52ac3f2 | 291 | for (i = 0; i < ARRAY_SIZE(dst_node_attrs); ++i) { |
ce0d9d72 EP |
292 | err = device_create_file(&n->info->device, |
293 | &dst_node_attrs[i]); | |
294 | if (err) | |
295 | goto err_out_remove_all; | |
296 | } | |
297 | return 0; | |
298 | ||
299 | err_out_remove_all: | |
300 | while (--i >= 0) | |
301 | device_remove_file(&n->info->device, | |
302 | &dst_node_attrs[i]); | |
303 | ||
304 | return err; | |
305 | } | |
306 | ||
307 | static void dst_remove_node_attributes(struct dst_node *n) | |
308 | { | |
309 | int i; | |
310 | ||
d52ac3f2 | 311 | for (i = 0; i < ARRAY_SIZE(dst_node_attrs); ++i) |
ce0d9d72 EP |
312 | device_remove_file(&n->info->device, |
313 | &dst_node_attrs[i]); | |
314 | } | |
315 | ||
316 | /* | |
317 | * Sysfs cleanup and initialization. | |
318 | * Shows number of useful parameters. | |
319 | */ | |
320 | static void dst_node_sysfs_exit(struct dst_node *n) | |
321 | { | |
322 | if (n->info) { | |
323 | dst_remove_node_attributes(n); | |
324 | device_unregister(&n->info->device); | |
325 | n->info = NULL; | |
326 | } | |
327 | } | |
328 | ||
329 | static int dst_node_sysfs_init(struct dst_node *n) | |
330 | { | |
331 | int err; | |
332 | ||
333 | n->info = kzalloc(sizeof(struct dst_info), GFP_KERNEL); | |
334 | if (!n->info) | |
335 | return -ENOMEM; | |
336 | ||
337 | memcpy(&n->info->device, &dst_node_dev, sizeof(struct device)); | |
338 | n->info->size = n->size; | |
339 | ||
fdc4f2e9 | 340 | dev_set_name(&n->info->device, "dst-%s", n->name); |
ce0d9d72 EP |
341 | err = device_register(&n->info->device); |
342 | if (err) { | |
343 | dprintk(KERN_ERR "Failed to register node '%s', err: %d.\n", | |
344 | n->name, err); | |
345 | goto err_out_exit; | |
346 | } | |
347 | ||
348 | dst_create_node_attributes(n); | |
349 | ||
350 | return 0; | |
351 | ||
352 | err_out_exit: | |
353 | kfree(n->info); | |
354 | n->info = NULL; | |
355 | return err; | |
356 | } | |
357 | ||
358 | /* | |
359 | * DST node hash tables machinery. | |
360 | */ | |
361 | static inline unsigned int dst_hash(char *str, unsigned int size) | |
362 | { | |
d52ac3f2 | 363 | return jhash(str, size, 0) % dst_hashtable_size; |
ce0d9d72 EP |
364 | } |
365 | ||
366 | static void dst_node_remove(struct dst_node *n) | |
367 | { | |
368 | mutex_lock(&dst_hash_lock); | |
369 | list_del_init(&n->node_entry); | |
370 | mutex_unlock(&dst_hash_lock); | |
371 | } | |
372 | ||
373 | static void dst_node_add(struct dst_node *n) | |
374 | { | |
375 | unsigned hash = dst_hash(n->name, sizeof(n->name)); | |
376 | ||
377 | mutex_lock(&dst_hash_lock); | |
378 | list_add_tail(&n->node_entry, &dst_hashtable[hash]); | |
379 | mutex_unlock(&dst_hash_lock); | |
380 | } | |
381 | ||
382 | /* | |
383 | * Cleaning node when it is about to be freed. | |
384 | * There are still users of the socket though, | |
385 | * so connection cleanup should be protected. | |
386 | */ | |
387 | static void dst_node_cleanup(struct dst_node *n) | |
388 | { | |
389 | struct dst_state *st = n->state; | |
390 | ||
391 | if (!st) | |
392 | return; | |
393 | ||
394 | if (n->queue) { | |
395 | blk_cleanup_queue(n->queue); | |
396 | ||
397 | mutex_lock(&dst_hash_lock); | |
398 | idr_remove(&dst_index_idr, n->disk->first_minor); | |
399 | mutex_unlock(&dst_hash_lock); | |
400 | ||
401 | put_disk(n->disk); | |
402 | } | |
403 | ||
404 | if (n->bdev) { | |
405 | sync_blockdev(n->bdev); | |
306bb73d | 406 | close_bdev_exclusive(n->bdev, FMODE_READ|FMODE_WRITE); |
ce0d9d72 EP |
407 | } |
408 | ||
409 | dst_state_lock(st); | |
410 | st->need_exit = 1; | |
411 | dst_state_exit_connected(st); | |
412 | dst_state_unlock(st); | |
413 | ||
414 | wake_up(&st->thread_wait); | |
415 | ||
416 | dst_state_put(st); | |
417 | n->state = NULL; | |
418 | } | |
419 | ||
420 | /* | |
421 | * Free security attributes attached to given node. | |
422 | */ | |
423 | static void dst_security_exit(struct dst_node *n) | |
424 | { | |
425 | struct dst_secure *s, *tmp; | |
426 | ||
427 | list_for_each_entry_safe(s, tmp, &n->security_list, sec_entry) { | |
428 | list_del(&s->sec_entry); | |
429 | kfree(s); | |
430 | } | |
431 | } | |
432 | ||
433 | /* | |
434 | * Free node when there are no more users. | |
435 | * Actually node has to be freed on behalf od userspace process, | |
436 | * since there are number of threads, which are embedded in the | |
437 | * node, so they can not exit and free node from there, that is | |
438 | * why there is a wakeup if reference counter is not equal to zero. | |
439 | */ | |
440 | void dst_node_put(struct dst_node *n) | |
441 | { | |
442 | if (unlikely(!n)) | |
443 | return; | |
444 | ||
445 | dprintk("%s: n: %p, refcnt: %d.\n", | |
446 | __func__, n, atomic_read(&n->refcnt)); | |
447 | ||
448 | if (atomic_dec_and_test(&n->refcnt)) { | |
449 | dst_node_remove(n); | |
450 | n->trans_scan_timeout = 0; | |
451 | dst_node_cleanup(n); | |
452 | thread_pool_destroy(n->pool); | |
453 | dst_node_sysfs_exit(n); | |
454 | dst_node_crypto_exit(n); | |
455 | dst_security_exit(n); | |
456 | dst_node_trans_exit(n); | |
457 | ||
458 | kfree(n); | |
459 | ||
460 | dprintk("%s: freed n: %p.\n", __func__, n); | |
461 | } else { | |
462 | wake_up(&n->wait); | |
463 | } | |
464 | } | |
465 | ||
ce0d9d72 EP |
466 | /* |
467 | * Setting up export device: lookup by the name, get its size | |
468 | * and setup listening socket, which will accept clients, which | |
469 | * will submit IO for given storage. | |
470 | */ | |
471 | static int dst_setup_export(struct dst_node *n, struct dst_ctl *ctl, | |
472 | struct dst_export_ctl *le) | |
473 | { | |
474 | int err; | |
ce0d9d72 EP |
475 | |
476 | snprintf(n->info->local, sizeof(n->info->local), "%s", le->device); | |
477 | ||
306bb73d AV |
478 | n->bdev = open_bdev_exclusive(le->device, FMODE_READ|FMODE_WRITE, NULL); |
479 | if (IS_ERR(n->bdev)) | |
480 | return PTR_ERR(n->bdev); | |
ce0d9d72 EP |
481 | |
482 | if (n->size != 0) | |
483 | n->size = min_t(loff_t, n->bdev->bd_inode->i_size, n->size); | |
484 | else | |
485 | n->size = n->bdev->bd_inode->i_size; | |
486 | ||
487 | n->info->size = n->size; | |
488 | err = dst_node_init_listened(n, le); | |
489 | if (err) | |
490 | goto err_out_cleanup; | |
491 | ||
492 | return 0; | |
493 | ||
494 | err_out_cleanup: | |
306bb73d | 495 | close_bdev_exclusive(n->bdev, FMODE_READ|FMODE_WRITE); |
ce0d9d72 EP |
496 | n->bdev = NULL; |
497 | ||
498 | return err; | |
499 | } | |
500 | ||
501 | /* Empty thread pool callbacks for the network processing threads. */ | |
502 | static inline void *dst_thread_network_init(void *data) | |
503 | { | |
504 | dprintk("%s: data: %p.\n", __func__, data); | |
505 | return data; | |
506 | } | |
507 | ||
508 | static inline void dst_thread_network_cleanup(void *data) | |
509 | { | |
510 | dprintk("%s: data: %p.\n", __func__, data); | |
511 | } | |
512 | ||
513 | /* | |
514 | * Allocate DST node and initialize some of its parameters. | |
515 | */ | |
516 | static struct dst_node *dst_alloc_node(struct dst_ctl *ctl, | |
517 | int (*start)(struct dst_node *), | |
518 | int num) | |
519 | { | |
520 | struct dst_node *n; | |
521 | int err; | |
522 | ||
523 | n = kzalloc(sizeof(struct dst_node), GFP_KERNEL); | |
524 | if (!n) | |
525 | return NULL; | |
526 | ||
527 | INIT_LIST_HEAD(&n->node_entry); | |
528 | ||
529 | INIT_LIST_HEAD(&n->security_list); | |
530 | mutex_init(&n->security_lock); | |
531 | ||
532 | init_waitqueue_head(&n->wait); | |
533 | ||
534 | n->trans_scan_timeout = msecs_to_jiffies(ctl->trans_scan_timeout); | |
535 | if (!n->trans_scan_timeout) | |
536 | n->trans_scan_timeout = HZ; | |
537 | ||
538 | n->trans_max_retries = ctl->trans_max_retries; | |
539 | if (!n->trans_max_retries) | |
540 | n->trans_max_retries = 10; | |
541 | ||
542 | /* | |
543 | * Pretty much arbitrary default numbers. | |
544 | * 32 matches maximum number of pages in bio originated from ext3 (31). | |
545 | */ | |
546 | n->max_pages = ctl->max_pages; | |
547 | if (!n->max_pages) | |
548 | n->max_pages = 32; | |
549 | ||
550 | if (n->max_pages > 1024) | |
551 | n->max_pages = 1024; | |
552 | ||
553 | n->start = start; | |
554 | n->size = ctl->size; | |
555 | ||
556 | atomic_set(&n->refcnt, 1); | |
557 | atomic_long_set(&n->gen, 0); | |
558 | snprintf(n->name, sizeof(n->name), "%s", ctl->name); | |
559 | ||
560 | err = dst_node_sysfs_init(n); | |
561 | if (err) | |
562 | goto err_out_free; | |
563 | ||
564 | n->pool = thread_pool_create(num, n->name, dst_thread_network_init, | |
565 | dst_thread_network_cleanup, n); | |
566 | if (IS_ERR(n->pool)) { | |
567 | err = PTR_ERR(n->pool); | |
568 | goto err_out_sysfs_exit; | |
569 | } | |
570 | ||
571 | dprintk("%s: n: %p, name: %s.\n", __func__, n, n->name); | |
572 | ||
573 | return n; | |
574 | ||
575 | err_out_sysfs_exit: | |
576 | dst_node_sysfs_exit(n); | |
577 | err_out_free: | |
578 | kfree(n); | |
579 | return NULL; | |
580 | } | |
581 | ||
582 | /* | |
583 | * Starting a node, connected to the remote server: | |
584 | * register block device and initialize transaction mechanism. | |
585 | * In revers order though. | |
586 | * | |
587 | * It will autonegotiate some parameters with the remote node | |
588 | * and update local if needed. | |
589 | * | |
590 | * Transaction initialization should be the last thing before | |
591 | * starting the node, since transaction should include not only | |
592 | * block IO, but also crypto related data (if any), which are | |
593 | * initialized separately. | |
594 | */ | |
595 | static int dst_start_remote(struct dst_node *n) | |
596 | { | |
597 | int err; | |
598 | ||
599 | err = dst_node_trans_init(n, sizeof(struct dst_trans)); | |
600 | if (err) | |
601 | return err; | |
602 | ||
603 | err = dst_node_create_disk(n); | |
604 | if (err) | |
605 | return err; | |
606 | ||
607 | dst_node_set_size(n); | |
608 | add_disk(n->disk); | |
609 | ||
d52ac3f2 MZ |
610 | dprintk("DST: started remote node '%s', minor: %d.\n", |
611 | n->name, n->disk->first_minor); | |
ce0d9d72 EP |
612 | |
613 | return 0; | |
614 | } | |
615 | ||
616 | /* | |
617 | * Adding remote node and initialize connection. | |
618 | */ | |
619 | static int dst_add_remote(struct dst_node *n, struct dst_ctl *ctl, | |
620 | void *data, unsigned int size) | |
621 | { | |
622 | int err; | |
623 | struct dst_network_ctl *rctl = data; | |
624 | ||
625 | if (n) | |
626 | return -EEXIST; | |
627 | ||
628 | if (size != sizeof(struct dst_network_ctl)) | |
629 | return -EINVAL; | |
630 | ||
631 | n = dst_alloc_node(ctl, dst_start_remote, 1); | |
632 | if (!n) | |
633 | return -ENOMEM; | |
634 | ||
635 | memcpy(&n->info->net, rctl, sizeof(struct dst_network_ctl)); | |
636 | err = dst_node_init_connected(n, rctl); | |
637 | if (err) | |
638 | goto err_out_free; | |
639 | ||
640 | dst_node_add(n); | |
641 | ||
642 | return 0; | |
643 | ||
644 | err_out_free: | |
645 | dst_node_put(n); | |
646 | return err; | |
647 | } | |
648 | ||
649 | /* | |
650 | * Adding export node: initializing block device and listening socket. | |
651 | */ | |
652 | static int dst_add_export(struct dst_node *n, struct dst_ctl *ctl, | |
653 | void *data, unsigned int size) | |
654 | { | |
655 | int err; | |
656 | struct dst_export_ctl *le = data; | |
657 | ||
658 | if (n) | |
659 | return -EEXIST; | |
660 | ||
661 | if (size != sizeof(struct dst_export_ctl)) | |
662 | return -EINVAL; | |
663 | ||
664 | n = dst_alloc_node(ctl, dst_start_export, 2); | |
665 | if (!n) | |
666 | return -EINVAL; | |
667 | ||
668 | err = dst_setup_export(n, ctl, le); | |
669 | if (err) | |
670 | goto err_out_free; | |
671 | ||
672 | dst_node_add(n); | |
673 | ||
674 | return 0; | |
675 | ||
676 | err_out_free: | |
677 | dst_node_put(n); | |
678 | return err; | |
679 | } | |
680 | ||
681 | static int dst_node_remove_unload(struct dst_node *n) | |
682 | { | |
683 | printk(KERN_INFO "STOPPED name: '%s', size: %llu.\n", | |
684 | n->name, n->size); | |
685 | ||
686 | if (n->disk) | |
687 | del_gendisk(n->disk); | |
688 | ||
689 | dst_node_remove(n); | |
690 | dst_node_sysfs_exit(n); | |
691 | ||
692 | /* | |
693 | * This is not a hack. Really. | |
694 | * Node's reference counter allows to implement fine grained | |
695 | * node freeing, but since all transactions (which hold node's | |
696 | * reference counter) are processed in the dedicated thread, | |
697 | * it is possible that reference will hit zero in that thread, | |
698 | * so we will not be able to exit thread and cleanup the node. | |
699 | * | |
700 | * So, we remove disk, so no new activity is possible, and | |
701 | * wait until all pending transaction are completed (either | |
702 | * in receiving thread or by timeout in workqueue), in this | |
703 | * case reference counter will be less or equal to 2 (once set in | |
704 | * dst_alloc_node() and then in connector message parser; | |
705 | * or when we force module unloading, and connector message | |
706 | * parser does not hold a reference, in this case reference | |
707 | * counter will be equal to 1), | |
708 | * and subsequent dst_node_put() calls will free the node. | |
709 | */ | |
d52ac3f2 MZ |
710 | dprintk("%s: going to sleep with %d refcnt.\n", |
711 | __func__, atomic_read(&n->refcnt)); | |
ce0d9d72 EP |
712 | wait_event(n->wait, atomic_read(&n->refcnt) <= 2); |
713 | ||
714 | dst_node_put(n); | |
715 | return 0; | |
716 | } | |
717 | ||
718 | /* | |
719 | * Remove node from the hash table. | |
720 | */ | |
721 | static int dst_del_node(struct dst_node *n, struct dst_ctl *ctl, | |
722 | void *data, unsigned int size) | |
723 | { | |
724 | if (!n) | |
725 | return -ENODEV; | |
726 | ||
727 | return dst_node_remove_unload(n); | |
728 | } | |
729 | ||
730 | /* | |
731 | * Initialize crypto processing for given node. | |
732 | */ | |
733 | static int dst_crypto_init(struct dst_node *n, struct dst_ctl *ctl, | |
734 | void *data, unsigned int size) | |
735 | { | |
736 | struct dst_crypto_ctl *crypto = data; | |
737 | ||
738 | if (!n) | |
739 | return -ENODEV; | |
740 | ||
741 | if (size != sizeof(struct dst_crypto_ctl) + crypto->hash_keysize + | |
742 | crypto->cipher_keysize) | |
743 | return -EINVAL; | |
744 | ||
745 | if (n->trans_cache) | |
746 | return -EEXIST; | |
747 | ||
748 | return dst_node_crypto_init(n, crypto); | |
749 | } | |
750 | ||
751 | /* | |
752 | * Security attributes for given node. | |
753 | */ | |
754 | static int dst_security_init(struct dst_node *n, struct dst_ctl *ctl, | |
755 | void *data, unsigned int size) | |
756 | { | |
757 | struct dst_secure *s; | |
758 | ||
759 | if (!n) | |
760 | return -ENODEV; | |
761 | ||
762 | if (size != sizeof(struct dst_secure_user)) | |
763 | return -EINVAL; | |
764 | ||
765 | s = kmalloc(sizeof(struct dst_secure), GFP_KERNEL); | |
766 | if (!s) | |
767 | return -ENOMEM; | |
768 | ||
769 | memcpy(&s->sec, data, size); | |
770 | ||
771 | mutex_lock(&n->security_lock); | |
772 | list_add_tail(&s->sec_entry, &n->security_list); | |
773 | mutex_unlock(&n->security_lock); | |
774 | ||
775 | return 0; | |
776 | } | |
777 | ||
778 | /* | |
779 | * Kill'em all! | |
780 | */ | |
781 | static int dst_start_node(struct dst_node *n, struct dst_ctl *ctl, | |
782 | void *data, unsigned int size) | |
783 | { | |
784 | int err; | |
785 | ||
786 | if (!n) | |
787 | return -ENODEV; | |
788 | ||
789 | if (n->trans_cache) | |
790 | return 0; | |
791 | ||
792 | err = n->start(n); | |
793 | if (err) | |
794 | return err; | |
795 | ||
796 | printk(KERN_INFO "STARTED name: '%s', size: %llu.\n", n->name, n->size); | |
797 | return 0; | |
798 | } | |
799 | ||
800 | typedef int (*dst_command_func)(struct dst_node *n, struct dst_ctl *ctl, | |
801 | void *data, unsigned int size); | |
802 | ||
803 | /* | |
804 | * List of userspace commands. | |
805 | */ | |
806 | static dst_command_func dst_commands[] = { | |
807 | [DST_ADD_REMOTE] = &dst_add_remote, | |
808 | [DST_ADD_EXPORT] = &dst_add_export, | |
809 | [DST_DEL_NODE] = &dst_del_node, | |
810 | [DST_CRYPTO] = &dst_crypto_init, | |
811 | [DST_SECURITY] = &dst_security_init, | |
812 | [DST_START] = &dst_start_node, | |
813 | }; | |
814 | ||
815 | /* | |
816 | * Configuration parser. | |
817 | */ | |
7069331d | 818 | static void cn_dst_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp) |
ce0d9d72 EP |
819 | { |
820 | struct dst_ctl *ctl; | |
ce0d9d72 EP |
821 | int err; |
822 | struct dst_ctl_ack ack; | |
823 | struct dst_node *n = NULL, *tmp; | |
824 | unsigned int hash; | |
825 | ||
5788c568 PR |
826 | if (!cap_raised(nsp->eff_cap, CAP_SYS_ADMIN)) { |
827 | err = -EPERM; | |
828 | goto out; | |
829 | } | |
830 | ||
ce0d9d72 EP |
831 | if (msg->len < sizeof(struct dst_ctl)) { |
832 | err = -EBADMSG; | |
833 | goto out; | |
834 | } | |
835 | ||
836 | ctl = (struct dst_ctl *)msg->data; | |
837 | ||
838 | if (ctl->cmd >= DST_CMD_MAX) { | |
839 | err = -EINVAL; | |
840 | goto out; | |
841 | } | |
842 | hash = dst_hash(ctl->name, sizeof(ctl->name)); | |
843 | ||
844 | mutex_lock(&dst_hash_lock); | |
845 | list_for_each_entry(tmp, &dst_hashtable[hash], node_entry) { | |
846 | if (!memcmp(tmp->name, ctl->name, sizeof(tmp->name))) { | |
847 | n = tmp; | |
848 | dst_node_get(n); | |
849 | break; | |
850 | } | |
851 | } | |
852 | mutex_unlock(&dst_hash_lock); | |
853 | ||
854 | err = dst_commands[ctl->cmd](n, ctl, msg->data + sizeof(struct dst_ctl), | |
855 | msg->len - sizeof(struct dst_ctl)); | |
856 | ||
857 | dst_node_put(n); | |
858 | out: | |
859 | memcpy(&ack.msg, msg, sizeof(struct cn_msg)); | |
860 | ||
861 | ack.msg.ack = msg->ack + 1; | |
862 | ack.msg.len = sizeof(struct dst_ctl_ack) - sizeof(struct cn_msg); | |
863 | ||
864 | ack.error = err; | |
865 | ||
866 | cn_netlink_send(&ack.msg, 0, GFP_KERNEL); | |
867 | } | |
868 | ||
869 | /* | |
870 | * Global initialization: sysfs, hash table, block device registration, | |
871 | * connector and various caches. | |
872 | */ | |
873 | static int __init dst_sysfs_init(void) | |
874 | { | |
875 | return bus_register(&dst_dev_bus_type); | |
876 | } | |
877 | ||
878 | static void dst_sysfs_exit(void) | |
879 | { | |
880 | bus_unregister(&dst_dev_bus_type); | |
881 | } | |
882 | ||
883 | static int __init dst_hashtable_init(void) | |
884 | { | |
885 | unsigned int i; | |
886 | ||
887 | dst_hashtable = kcalloc(dst_hashtable_size, sizeof(struct list_head), | |
888 | GFP_KERNEL); | |
889 | if (!dst_hashtable) | |
890 | return -ENOMEM; | |
891 | ||
d52ac3f2 | 892 | for (i = 0; i < dst_hashtable_size; ++i) |
ce0d9d72 EP |
893 | INIT_LIST_HEAD(&dst_hashtable[i]); |
894 | ||
895 | return 0; | |
896 | } | |
897 | ||
898 | static void dst_hashtable_exit(void) | |
899 | { | |
900 | unsigned int i; | |
901 | struct dst_node *n, *tmp; | |
902 | ||
d52ac3f2 | 903 | for (i = 0; i < dst_hashtable_size; ++i) { |
ce0d9d72 EP |
904 | list_for_each_entry_safe(n, tmp, &dst_hashtable[i], node_entry) { |
905 | dst_node_remove_unload(n); | |
906 | } | |
907 | } | |
908 | ||
909 | kfree(dst_hashtable); | |
910 | } | |
911 | ||
912 | static int __init dst_sys_init(void) | |
913 | { | |
914 | int err = -ENOMEM; | |
915 | ||
916 | err = dst_hashtable_init(); | |
917 | if (err) | |
918 | goto err_out_exit; | |
919 | ||
920 | err = dst_export_init(); | |
921 | if (err) | |
922 | goto err_out_hashtable_exit; | |
923 | ||
924 | err = register_blkdev(dst_major, DST_NAME); | |
925 | if (err < 0) | |
926 | goto err_out_export_exit; | |
927 | if (err) | |
928 | dst_major = err; | |
929 | ||
930 | err = dst_sysfs_init(); | |
931 | if (err) | |
932 | goto err_out_unregister; | |
933 | ||
934 | err = cn_add_callback(&cn_dst_id, "DST", cn_dst_callback); | |
935 | if (err) | |
936 | goto err_out_sysfs_exit; | |
937 | ||
938 | printk(KERN_INFO "Distributed storage, '%s' release.\n", dst_name); | |
939 | ||
940 | return 0; | |
941 | ||
942 | err_out_sysfs_exit: | |
943 | dst_sysfs_exit(); | |
944 | err_out_unregister: | |
945 | unregister_blkdev(dst_major, DST_NAME); | |
946 | err_out_export_exit: | |
947 | dst_export_exit(); | |
948 | err_out_hashtable_exit: | |
949 | dst_hashtable_exit(); | |
950 | err_out_exit: | |
951 | return err; | |
952 | } | |
953 | ||
954 | static void __exit dst_sys_exit(void) | |
955 | { | |
956 | cn_del_callback(&cn_dst_id); | |
957 | unregister_blkdev(dst_major, DST_NAME); | |
958 | dst_hashtable_exit(); | |
959 | dst_sysfs_exit(); | |
960 | dst_export_exit(); | |
961 | } | |
962 | ||
963 | module_init(dst_sys_init); | |
964 | module_exit(dst_sys_exit); | |
965 | ||
966 | MODULE_DESCRIPTION("Distributed storage"); | |
967 | MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>"); | |
968 | MODULE_LICENSE("GPL"); |