xref: /linux/drivers/s390/block/dcssblk.c (revision e5e1bdf0bca8cd16ad39ed2febf6f689d9c07586)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * dcssblk.c -- the S/390 block driver for dcss memory
4  *
5  * Authors: Carsten Otte, Stefan Weinhuber, Gerald Schaefer
6  */
7 
8 #define KMSG_COMPONENT "dcssblk"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10 
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/ctype.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/blkdev.h>
18 #include <linux/completion.h>
19 #include <linux/interrupt.h>
20 #include <linux/pfn_t.h>
21 #include <linux/uio.h>
22 #include <linux/dax.h>
23 #include <asm/extmem.h>
24 #include <asm/io.h>
25 
26 #define DCSSBLK_NAME "dcssblk"
27 #define DCSSBLK_MINORS_PER_DISK 1
28 #define DCSSBLK_PARM_LEN 400
29 #define DCSS_BUS_ID_SIZE 20
30 
31 static int dcssblk_open(struct block_device *bdev, fmode_t mode);
32 static void dcssblk_release(struct gendisk *disk, fmode_t mode);
33 static blk_qc_t dcssblk_submit_bio(struct bio *bio);
34 static long dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
35 		long nr_pages, void **kaddr, pfn_t *pfn);
36 
37 static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0";
38 
39 static int dcssblk_major;
40 static const struct block_device_operations dcssblk_devops = {
41 	.owner   	= THIS_MODULE,
42 	.submit_bio	= dcssblk_submit_bio,
43 	.open    	= dcssblk_open,
44 	.release 	= dcssblk_release,
45 };
46 
47 static size_t dcssblk_dax_copy_from_iter(struct dax_device *dax_dev,
48 		pgoff_t pgoff, void *addr, size_t bytes, struct iov_iter *i)
49 {
50 	return copy_from_iter(addr, bytes, i);
51 }
52 
53 static size_t dcssblk_dax_copy_to_iter(struct dax_device *dax_dev,
54 		pgoff_t pgoff, void *addr, size_t bytes, struct iov_iter *i)
55 {
56 	return copy_to_iter(addr, bytes, i);
57 }
58 
59 static int dcssblk_dax_zero_page_range(struct dax_device *dax_dev,
60 				       pgoff_t pgoff, size_t nr_pages)
61 {
62 	long rc;
63 	void *kaddr;
64 
65 	rc = dax_direct_access(dax_dev, pgoff, nr_pages, &kaddr, NULL);
66 	if (rc < 0)
67 		return rc;
68 	memset(kaddr, 0, nr_pages << PAGE_SHIFT);
69 	dax_flush(dax_dev, kaddr, nr_pages << PAGE_SHIFT);
70 	return 0;
71 }
72 
73 static const struct dax_operations dcssblk_dax_ops = {
74 	.direct_access = dcssblk_dax_direct_access,
75 	.dax_supported = generic_fsdax_supported,
76 	.copy_from_iter = dcssblk_dax_copy_from_iter,
77 	.copy_to_iter = dcssblk_dax_copy_to_iter,
78 	.zero_page_range = dcssblk_dax_zero_page_range,
79 };
80 
81 struct dcssblk_dev_info {
82 	struct list_head lh;
83 	struct device dev;
84 	char segment_name[DCSS_BUS_ID_SIZE];
85 	atomic_t use_count;
86 	struct gendisk *gd;
87 	unsigned long start;
88 	unsigned long end;
89 	int segment_type;
90 	unsigned char save_pending;
91 	unsigned char is_shared;
92 	struct request_queue *dcssblk_queue;
93 	int num_of_segments;
94 	struct list_head seg_list;
95 	struct dax_device *dax_dev;
96 };
97 
98 struct segment_info {
99 	struct list_head lh;
100 	char segment_name[DCSS_BUS_ID_SIZE];
101 	unsigned long start;
102 	unsigned long end;
103 	int segment_type;
104 };
105 
106 static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
107 				  size_t count);
108 static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
109 				  size_t count);
110 
111 static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
112 static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
113 
114 static struct device *dcssblk_root_dev;
115 
116 static LIST_HEAD(dcssblk_devices);
117 static struct rw_semaphore dcssblk_devices_sem;
118 
119 /*
120  * release function for segment device.
121  */
122 static void
123 dcssblk_release_segment(struct device *dev)
124 {
125 	struct dcssblk_dev_info *dev_info;
126 	struct segment_info *entry, *temp;
127 
128 	dev_info = container_of(dev, struct dcssblk_dev_info, dev);
129 	list_for_each_entry_safe(entry, temp, &dev_info->seg_list, lh) {
130 		list_del(&entry->lh);
131 		kfree(entry);
132 	}
133 	kfree(dev_info);
134 	module_put(THIS_MODULE);
135 }
136 
137 /*
138  * get a minor number. needs to be called with
139  * down_write(&dcssblk_devices_sem) and the
140  * device needs to be enqueued before the semaphore is
141  * freed.
142  */
143 static int
144 dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info)
145 {
146 	int minor, found;
147 	struct dcssblk_dev_info *entry;
148 
149 	if (dev_info == NULL)
150 		return -EINVAL;
151 	for (minor = 0; minor < (1<<MINORBITS); minor++) {
152 		found = 0;
153 		// test if minor available
154 		list_for_each_entry(entry, &dcssblk_devices, lh)
155 			if (minor == entry->gd->first_minor)
156 				found++;
157 		if (!found) break; // got unused minor
158 	}
159 	if (found)
160 		return -EBUSY;
161 	dev_info->gd->first_minor = minor;
162 	return 0;
163 }
164 
165 /*
166  * get the struct dcssblk_dev_info from dcssblk_devices
167  * for the given name.
168  * down_read(&dcssblk_devices_sem) must be held.
169  */
170 static struct dcssblk_dev_info *
171 dcssblk_get_device_by_name(char *name)
172 {
173 	struct dcssblk_dev_info *entry;
174 
175 	list_for_each_entry(entry, &dcssblk_devices, lh) {
176 		if (!strcmp(name, entry->segment_name)) {
177 			return entry;
178 		}
179 	}
180 	return NULL;
181 }
182 
183 /*
184  * get the struct segment_info from seg_list
185  * for the given name.
186  * down_read(&dcssblk_devices_sem) must be held.
187  */
188 static struct segment_info *
189 dcssblk_get_segment_by_name(char *name)
190 {
191 	struct dcssblk_dev_info *dev_info;
192 	struct segment_info *entry;
193 
194 	list_for_each_entry(dev_info, &dcssblk_devices, lh) {
195 		list_for_each_entry(entry, &dev_info->seg_list, lh) {
196 			if (!strcmp(name, entry->segment_name))
197 				return entry;
198 		}
199 	}
200 	return NULL;
201 }
202 
203 /*
204  * get the highest address of the multi-segment block.
205  */
206 static unsigned long
207 dcssblk_find_highest_addr(struct dcssblk_dev_info *dev_info)
208 {
209 	unsigned long highest_addr;
210 	struct segment_info *entry;
211 
212 	highest_addr = 0;
213 	list_for_each_entry(entry, &dev_info->seg_list, lh) {
214 		if (highest_addr < entry->end)
215 			highest_addr = entry->end;
216 	}
217 	return highest_addr;
218 }
219 
220 /*
221  * get the lowest address of the multi-segment block.
222  */
223 static unsigned long
224 dcssblk_find_lowest_addr(struct dcssblk_dev_info *dev_info)
225 {
226 	int set_first;
227 	unsigned long lowest_addr;
228 	struct segment_info *entry;
229 
230 	set_first = 0;
231 	lowest_addr = 0;
232 	list_for_each_entry(entry, &dev_info->seg_list, lh) {
233 		if (set_first == 0) {
234 			lowest_addr = entry->start;
235 			set_first = 1;
236 		} else {
237 			if (lowest_addr > entry->start)
238 				lowest_addr = entry->start;
239 		}
240 	}
241 	return lowest_addr;
242 }
243 
244 /*
245  * Check continuity of segments.
246  */
247 static int
248 dcssblk_is_continuous(struct dcssblk_dev_info *dev_info)
249 {
250 	int i, j, rc;
251 	struct segment_info *sort_list, *entry, temp;
252 
253 	if (dev_info->num_of_segments <= 1)
254 		return 0;
255 
256 	sort_list = kcalloc(dev_info->num_of_segments,
257 			    sizeof(struct segment_info),
258 			    GFP_KERNEL);
259 	if (sort_list == NULL)
260 		return -ENOMEM;
261 	i = 0;
262 	list_for_each_entry(entry, &dev_info->seg_list, lh) {
263 		memcpy(&sort_list[i], entry, sizeof(struct segment_info));
264 		i++;
265 	}
266 
267 	/* sort segments */
268 	for (i = 0; i < dev_info->num_of_segments; i++)
269 		for (j = 0; j < dev_info->num_of_segments; j++)
270 			if (sort_list[j].start > sort_list[i].start) {
271 				memcpy(&temp, &sort_list[i],
272 					sizeof(struct segment_info));
273 				memcpy(&sort_list[i], &sort_list[j],
274 					sizeof(struct segment_info));
275 				memcpy(&sort_list[j], &temp,
276 					sizeof(struct segment_info));
277 			}
278 
279 	/* check continuity */
280 	for (i = 0; i < dev_info->num_of_segments - 1; i++) {
281 		if ((sort_list[i].end + 1) != sort_list[i+1].start) {
282 			pr_err("Adjacent DCSSs %s and %s are not "
283 			       "contiguous\n", sort_list[i].segment_name,
284 			       sort_list[i+1].segment_name);
285 			rc = -EINVAL;
286 			goto out;
287 		}
288 		/* EN and EW are allowed in a block device */
289 		if (sort_list[i].segment_type != sort_list[i+1].segment_type) {
290 			if (!(sort_list[i].segment_type & SEGMENT_EXCLUSIVE) ||
291 				(sort_list[i].segment_type == SEG_TYPE_ER) ||
292 				!(sort_list[i+1].segment_type &
293 				SEGMENT_EXCLUSIVE) ||
294 				(sort_list[i+1].segment_type == SEG_TYPE_ER)) {
295 				pr_err("DCSS %s and DCSS %s have "
296 				       "incompatible types\n",
297 				       sort_list[i].segment_name,
298 				       sort_list[i+1].segment_name);
299 				rc = -EINVAL;
300 				goto out;
301 			}
302 		}
303 	}
304 	rc = 0;
305 out:
306 	kfree(sort_list);
307 	return rc;
308 }
309 
310 /*
311  * Load a segment
312  */
313 static int
314 dcssblk_load_segment(char *name, struct segment_info **seg_info)
315 {
316 	int rc;
317 
318 	/* already loaded? */
319 	down_read(&dcssblk_devices_sem);
320 	*seg_info = dcssblk_get_segment_by_name(name);
321 	up_read(&dcssblk_devices_sem);
322 	if (*seg_info != NULL)
323 		return -EEXIST;
324 
325 	/* get a struct segment_info */
326 	*seg_info = kzalloc(sizeof(struct segment_info), GFP_KERNEL);
327 	if (*seg_info == NULL)
328 		return -ENOMEM;
329 
330 	strcpy((*seg_info)->segment_name, name);
331 
332 	/* load the segment */
333 	rc = segment_load(name, SEGMENT_SHARED,
334 			&(*seg_info)->start, &(*seg_info)->end);
335 	if (rc < 0) {
336 		segment_warning(rc, (*seg_info)->segment_name);
337 		kfree(*seg_info);
338 	} else {
339 		INIT_LIST_HEAD(&(*seg_info)->lh);
340 		(*seg_info)->segment_type = rc;
341 	}
342 	return rc;
343 }
344 
345 /*
346  * device attribute for switching shared/nonshared (exclusive)
347  * operation (show + store)
348  */
349 static ssize_t
350 dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
351 {
352 	struct dcssblk_dev_info *dev_info;
353 
354 	dev_info = container_of(dev, struct dcssblk_dev_info, dev);
355 	return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
356 }
357 
358 static ssize_t
359 dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
360 {
361 	struct dcssblk_dev_info *dev_info;
362 	struct segment_info *entry, *temp;
363 	int rc;
364 
365 	if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
366 		return -EINVAL;
367 	down_write(&dcssblk_devices_sem);
368 	dev_info = container_of(dev, struct dcssblk_dev_info, dev);
369 	if (atomic_read(&dev_info->use_count)) {
370 		rc = -EBUSY;
371 		goto out;
372 	}
373 	if (inbuf[0] == '1') {
374 		/* reload segments in shared mode */
375 		list_for_each_entry(entry, &dev_info->seg_list, lh) {
376 			rc = segment_modify_shared(entry->segment_name,
377 						SEGMENT_SHARED);
378 			if (rc < 0) {
379 				BUG_ON(rc == -EINVAL);
380 				if (rc != -EAGAIN)
381 					goto removeseg;
382 			}
383 		}
384 		dev_info->is_shared = 1;
385 		switch (dev_info->segment_type) {
386 		case SEG_TYPE_SR:
387 		case SEG_TYPE_ER:
388 		case SEG_TYPE_SC:
389 			set_disk_ro(dev_info->gd, 1);
390 		}
391 	} else if (inbuf[0] == '0') {
392 		/* reload segments in exclusive mode */
393 		if (dev_info->segment_type == SEG_TYPE_SC) {
394 			pr_err("DCSS %s is of type SC and cannot be "
395 			       "loaded as exclusive-writable\n",
396 			       dev_info->segment_name);
397 			rc = -EINVAL;
398 			goto out;
399 		}
400 		list_for_each_entry(entry, &dev_info->seg_list, lh) {
401 			rc = segment_modify_shared(entry->segment_name,
402 						   SEGMENT_EXCLUSIVE);
403 			if (rc < 0) {
404 				BUG_ON(rc == -EINVAL);
405 				if (rc != -EAGAIN)
406 					goto removeseg;
407 			}
408 		}
409 		dev_info->is_shared = 0;
410 		set_disk_ro(dev_info->gd, 0);
411 	} else {
412 		rc = -EINVAL;
413 		goto out;
414 	}
415 	rc = count;
416 	goto out;
417 
418 removeseg:
419 	pr_err("DCSS device %s is removed after a failed access mode "
420 	       "change\n", dev_info->segment_name);
421 	temp = entry;
422 	list_for_each_entry(entry, &dev_info->seg_list, lh) {
423 		if (entry != temp)
424 			segment_unload(entry->segment_name);
425 	}
426 	list_del(&dev_info->lh);
427 
428 	kill_dax(dev_info->dax_dev);
429 	put_dax(dev_info->dax_dev);
430 	del_gendisk(dev_info->gd);
431 	blk_cleanup_queue(dev_info->dcssblk_queue);
432 	dev_info->gd->queue = NULL;
433 	put_disk(dev_info->gd);
434 	up_write(&dcssblk_devices_sem);
435 
436 	if (device_remove_file_self(dev, attr)) {
437 		device_unregister(dev);
438 		put_device(dev);
439 	}
440 	return rc;
441 out:
442 	up_write(&dcssblk_devices_sem);
443 	return rc;
444 }
445 static DEVICE_ATTR(shared, S_IWUSR | S_IRUSR, dcssblk_shared_show,
446 		   dcssblk_shared_store);
447 
448 /*
449  * device attribute for save operation on current copy
450  * of the segment. If the segment is busy, saving will
451  * become pending until it gets released, which can be
452  * undone by storing a non-true value to this entry.
453  * (show + store)
454  */
455 static ssize_t
456 dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
457 {
458 	struct dcssblk_dev_info *dev_info;
459 
460 	dev_info = container_of(dev, struct dcssblk_dev_info, dev);
461 	return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
462 }
463 
464 static ssize_t
465 dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
466 {
467 	struct dcssblk_dev_info *dev_info;
468 	struct segment_info *entry;
469 
470 	if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
471 		return -EINVAL;
472 	dev_info = container_of(dev, struct dcssblk_dev_info, dev);
473 
474 	down_write(&dcssblk_devices_sem);
475 	if (inbuf[0] == '1') {
476 		if (atomic_read(&dev_info->use_count) == 0) {
477 			// device is idle => we save immediately
478 			pr_info("All DCSSs that map to device %s are "
479 				"saved\n", dev_info->segment_name);
480 			list_for_each_entry(entry, &dev_info->seg_list, lh) {
481 				if (entry->segment_type == SEG_TYPE_EN ||
482 				    entry->segment_type == SEG_TYPE_SN)
483 					pr_warn("DCSS %s is of type SN or EN"
484 						" and cannot be saved\n",
485 						entry->segment_name);
486 				else
487 					segment_save(entry->segment_name);
488 			}
489 		}  else {
490 			// device is busy => we save it when it becomes
491 			// idle in dcssblk_release
492 			pr_info("Device %s is in use, its DCSSs will be "
493 				"saved when it becomes idle\n",
494 				dev_info->segment_name);
495 			dev_info->save_pending = 1;
496 		}
497 	} else if (inbuf[0] == '0') {
498 		if (dev_info->save_pending) {
499 			// device is busy & the user wants to undo his save
500 			// request
501 			dev_info->save_pending = 0;
502 			pr_info("A pending save request for device %s "
503 				"has been canceled\n",
504 				dev_info->segment_name);
505 		}
506 	} else {
507 		up_write(&dcssblk_devices_sem);
508 		return -EINVAL;
509 	}
510 	up_write(&dcssblk_devices_sem);
511 	return count;
512 }
513 static DEVICE_ATTR(save, S_IWUSR | S_IRUSR, dcssblk_save_show,
514 		   dcssblk_save_store);
515 
516 /*
517  * device attribute for showing all segments in a device
518  */
519 static ssize_t
520 dcssblk_seglist_show(struct device *dev, struct device_attribute *attr,
521 		char *buf)
522 {
523 	int i;
524 
525 	struct dcssblk_dev_info *dev_info;
526 	struct segment_info *entry;
527 
528 	down_read(&dcssblk_devices_sem);
529 	dev_info = container_of(dev, struct dcssblk_dev_info, dev);
530 	i = 0;
531 	buf[0] = '\0';
532 	list_for_each_entry(entry, &dev_info->seg_list, lh) {
533 		strcpy(&buf[i], entry->segment_name);
534 		i += strlen(entry->segment_name);
535 		buf[i] = '\n';
536 		i++;
537 	}
538 	up_read(&dcssblk_devices_sem);
539 	return i;
540 }
541 static DEVICE_ATTR(seglist, S_IRUSR, dcssblk_seglist_show, NULL);
542 
543 static struct attribute *dcssblk_dev_attrs[] = {
544 	&dev_attr_shared.attr,
545 	&dev_attr_save.attr,
546 	&dev_attr_seglist.attr,
547 	NULL,
548 };
549 static struct attribute_group dcssblk_dev_attr_group = {
550 	.attrs = dcssblk_dev_attrs,
551 };
552 static const struct attribute_group *dcssblk_dev_attr_groups[] = {
553 	&dcssblk_dev_attr_group,
554 	NULL,
555 };
556 
557 /*
558  * device attribute for adding devices
559  */
560 static ssize_t
561 dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
562 {
563 	int rc, i, j, num_of_segments;
564 	struct dcssblk_dev_info *dev_info;
565 	struct segment_info *seg_info, *temp;
566 	char *local_buf;
567 	unsigned long seg_byte_size;
568 
569 	dev_info = NULL;
570 	seg_info = NULL;
571 	if (dev != dcssblk_root_dev) {
572 		rc = -EINVAL;
573 		goto out_nobuf;
574 	}
575 	if ((count < 1) || (buf[0] == '\0') || (buf[0] == '\n')) {
576 		rc = -ENAMETOOLONG;
577 		goto out_nobuf;
578 	}
579 
580 	local_buf = kmalloc(count + 1, GFP_KERNEL);
581 	if (local_buf == NULL) {
582 		rc = -ENOMEM;
583 		goto out_nobuf;
584 	}
585 
586 	/*
587 	 * parse input
588 	 */
589 	num_of_segments = 0;
590 	for (i = 0; (i < count && (buf[i] != '\0') && (buf[i] != '\n')); i++) {
591 		for (j = i; j < count &&
592 			(buf[j] != ':') &&
593 			(buf[j] != '\0') &&
594 			(buf[j] != '\n'); j++) {
595 			local_buf[j-i] = toupper(buf[j]);
596 		}
597 		local_buf[j-i] = '\0';
598 		if (((j - i) == 0) || ((j - i) > 8)) {
599 			rc = -ENAMETOOLONG;
600 			goto seg_list_del;
601 		}
602 
603 		rc = dcssblk_load_segment(local_buf, &seg_info);
604 		if (rc < 0)
605 			goto seg_list_del;
606 		/*
607 		 * get a struct dcssblk_dev_info
608 		 */
609 		if (num_of_segments == 0) {
610 			dev_info = kzalloc(sizeof(struct dcssblk_dev_info),
611 					GFP_KERNEL);
612 			if (dev_info == NULL) {
613 				rc = -ENOMEM;
614 				goto out;
615 			}
616 			strcpy(dev_info->segment_name, local_buf);
617 			dev_info->segment_type = seg_info->segment_type;
618 			INIT_LIST_HEAD(&dev_info->seg_list);
619 		}
620 		list_add_tail(&seg_info->lh, &dev_info->seg_list);
621 		num_of_segments++;
622 		i = j;
623 
624 		if ((buf[j] == '\0') || (buf[j] == '\n'))
625 			break;
626 	}
627 
628 	/* no trailing colon at the end of the input */
629 	if ((i > 0) && (buf[i-1] == ':')) {
630 		rc = -ENAMETOOLONG;
631 		goto seg_list_del;
632 	}
633 	strlcpy(local_buf, buf, i + 1);
634 	dev_info->num_of_segments = num_of_segments;
635 	rc = dcssblk_is_continuous(dev_info);
636 	if (rc < 0)
637 		goto seg_list_del;
638 
639 	dev_info->start = dcssblk_find_lowest_addr(dev_info);
640 	dev_info->end = dcssblk_find_highest_addr(dev_info);
641 
642 	dev_set_name(&dev_info->dev, "%s", dev_info->segment_name);
643 	dev_info->dev.release = dcssblk_release_segment;
644 	dev_info->dev.groups = dcssblk_dev_attr_groups;
645 	INIT_LIST_HEAD(&dev_info->lh);
646 	dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
647 	if (dev_info->gd == NULL) {
648 		rc = -ENOMEM;
649 		goto seg_list_del;
650 	}
651 	dev_info->gd->major = dcssblk_major;
652 	dev_info->gd->fops = &dcssblk_devops;
653 	dev_info->dcssblk_queue = blk_alloc_queue(NUMA_NO_NODE);
654 	dev_info->gd->queue = dev_info->dcssblk_queue;
655 	dev_info->gd->private_data = dev_info;
656 	blk_queue_logical_block_size(dev_info->dcssblk_queue, 4096);
657 	blk_queue_flag_set(QUEUE_FLAG_DAX, dev_info->dcssblk_queue);
658 
659 	seg_byte_size = (dev_info->end - dev_info->start + 1);
660 	set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
661 	pr_info("Loaded %s with total size %lu bytes and capacity %lu "
662 		"sectors\n", local_buf, seg_byte_size, seg_byte_size >> 9);
663 
664 	dev_info->save_pending = 0;
665 	dev_info->is_shared = 1;
666 	dev_info->dev.parent = dcssblk_root_dev;
667 
668 	/*
669 	 *get minor, add to list
670 	 */
671 	down_write(&dcssblk_devices_sem);
672 	if (dcssblk_get_segment_by_name(local_buf)) {
673 		rc = -EEXIST;
674 		goto release_gd;
675 	}
676 	rc = dcssblk_assign_free_minor(dev_info);
677 	if (rc)
678 		goto release_gd;
679 	sprintf(dev_info->gd->disk_name, "dcssblk%d",
680 		dev_info->gd->first_minor);
681 	list_add_tail(&dev_info->lh, &dcssblk_devices);
682 
683 	if (!try_module_get(THIS_MODULE)) {
684 		rc = -ENODEV;
685 		goto dev_list_del;
686 	}
687 	/*
688 	 * register the device
689 	 */
690 	rc = device_register(&dev_info->dev);
691 	if (rc)
692 		goto put_dev;
693 
694 	dev_info->dax_dev = alloc_dax(dev_info, dev_info->gd->disk_name,
695 			&dcssblk_dax_ops, DAXDEV_F_SYNC);
696 	if (IS_ERR(dev_info->dax_dev)) {
697 		rc = PTR_ERR(dev_info->dax_dev);
698 		dev_info->dax_dev = NULL;
699 		goto put_dev;
700 	}
701 
702 	get_device(&dev_info->dev);
703 	device_add_disk(&dev_info->dev, dev_info->gd, NULL);
704 
705 	switch (dev_info->segment_type) {
706 		case SEG_TYPE_SR:
707 		case SEG_TYPE_ER:
708 		case SEG_TYPE_SC:
709 			set_disk_ro(dev_info->gd,1);
710 			break;
711 		default:
712 			set_disk_ro(dev_info->gd,0);
713 			break;
714 	}
715 	up_write(&dcssblk_devices_sem);
716 	rc = count;
717 	goto out;
718 
719 put_dev:
720 	list_del(&dev_info->lh);
721 	blk_cleanup_queue(dev_info->dcssblk_queue);
722 	dev_info->gd->queue = NULL;
723 	put_disk(dev_info->gd);
724 	list_for_each_entry(seg_info, &dev_info->seg_list, lh) {
725 		segment_unload(seg_info->segment_name);
726 	}
727 	put_device(&dev_info->dev);
728 	up_write(&dcssblk_devices_sem);
729 	goto out;
730 dev_list_del:
731 	list_del(&dev_info->lh);
732 release_gd:
733 	blk_cleanup_queue(dev_info->dcssblk_queue);
734 	dev_info->gd->queue = NULL;
735 	put_disk(dev_info->gd);
736 	up_write(&dcssblk_devices_sem);
737 seg_list_del:
738 	if (dev_info == NULL)
739 		goto out;
740 	list_for_each_entry_safe(seg_info, temp, &dev_info->seg_list, lh) {
741 		list_del(&seg_info->lh);
742 		segment_unload(seg_info->segment_name);
743 		kfree(seg_info);
744 	}
745 	kfree(dev_info);
746 out:
747 	kfree(local_buf);
748 out_nobuf:
749 	return rc;
750 }
751 
752 /*
753  * device attribute for removing devices
754  */
755 static ssize_t
756 dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
757 {
758 	struct dcssblk_dev_info *dev_info;
759 	struct segment_info *entry;
760 	int rc, i;
761 	char *local_buf;
762 
763 	if (dev != dcssblk_root_dev) {
764 		return -EINVAL;
765 	}
766 	local_buf = kmalloc(count + 1, GFP_KERNEL);
767 	if (local_buf == NULL) {
768 		return -ENOMEM;
769 	}
770 	/*
771 	 * parse input
772 	 */
773 	for (i = 0; (i < count && (*(buf+i)!='\0') && (*(buf+i)!='\n')); i++) {
774 		local_buf[i] = toupper(buf[i]);
775 	}
776 	local_buf[i] = '\0';
777 	if ((i == 0) || (i > 8)) {
778 		rc = -ENAMETOOLONG;
779 		goto out_buf;
780 	}
781 
782 	down_write(&dcssblk_devices_sem);
783 	dev_info = dcssblk_get_device_by_name(local_buf);
784 	if (dev_info == NULL) {
785 		up_write(&dcssblk_devices_sem);
786 		pr_warn("Device %s cannot be removed because it is not a known device\n",
787 			local_buf);
788 		rc = -ENODEV;
789 		goto out_buf;
790 	}
791 	if (atomic_read(&dev_info->use_count) != 0) {
792 		up_write(&dcssblk_devices_sem);
793 		pr_warn("Device %s cannot be removed while it is in use\n",
794 			local_buf);
795 		rc = -EBUSY;
796 		goto out_buf;
797 	}
798 
799 	list_del(&dev_info->lh);
800 	kill_dax(dev_info->dax_dev);
801 	put_dax(dev_info->dax_dev);
802 	del_gendisk(dev_info->gd);
803 	blk_cleanup_queue(dev_info->dcssblk_queue);
804 	dev_info->gd->queue = NULL;
805 	put_disk(dev_info->gd);
806 
807 	/* unload all related segments */
808 	list_for_each_entry(entry, &dev_info->seg_list, lh)
809 		segment_unload(entry->segment_name);
810 
811 	up_write(&dcssblk_devices_sem);
812 
813 	device_unregister(&dev_info->dev);
814 	put_device(&dev_info->dev);
815 
816 	rc = count;
817 out_buf:
818 	kfree(local_buf);
819 	return rc;
820 }
821 
822 static int
823 dcssblk_open(struct block_device *bdev, fmode_t mode)
824 {
825 	struct dcssblk_dev_info *dev_info;
826 	int rc;
827 
828 	dev_info = bdev->bd_disk->private_data;
829 	if (NULL == dev_info) {
830 		rc = -ENODEV;
831 		goto out;
832 	}
833 	atomic_inc(&dev_info->use_count);
834 	rc = 0;
835 out:
836 	return rc;
837 }
838 
839 static void
840 dcssblk_release(struct gendisk *disk, fmode_t mode)
841 {
842 	struct dcssblk_dev_info *dev_info = disk->private_data;
843 	struct segment_info *entry;
844 
845 	if (!dev_info) {
846 		WARN_ON(1);
847 		return;
848 	}
849 	down_write(&dcssblk_devices_sem);
850 	if (atomic_dec_and_test(&dev_info->use_count)
851 	    && (dev_info->save_pending)) {
852 		pr_info("Device %s has become idle and is being saved "
853 			"now\n", dev_info->segment_name);
854 		list_for_each_entry(entry, &dev_info->seg_list, lh) {
855 			if (entry->segment_type == SEG_TYPE_EN ||
856 			    entry->segment_type == SEG_TYPE_SN)
857 				pr_warn("DCSS %s is of type SN or EN and cannot"
858 					" be saved\n", entry->segment_name);
859 			else
860 				segment_save(entry->segment_name);
861 		}
862 		dev_info->save_pending = 0;
863 	}
864 	up_write(&dcssblk_devices_sem);
865 }
866 
867 static blk_qc_t
868 dcssblk_submit_bio(struct bio *bio)
869 {
870 	struct dcssblk_dev_info *dev_info;
871 	struct bio_vec bvec;
872 	struct bvec_iter iter;
873 	unsigned long index;
874 	unsigned long page_addr;
875 	unsigned long source_addr;
876 	unsigned long bytes_done;
877 
878 	blk_queue_split(&bio);
879 
880 	bytes_done = 0;
881 	dev_info = bio->bi_bdev->bd_disk->private_data;
882 	if (dev_info == NULL)
883 		goto fail;
884 	if ((bio->bi_iter.bi_sector & 7) != 0 ||
885 	    (bio->bi_iter.bi_size & 4095) != 0)
886 		/* Request is not page-aligned. */
887 		goto fail;
888 	/* verify data transfer direction */
889 	if (dev_info->is_shared) {
890 		switch (dev_info->segment_type) {
891 		case SEG_TYPE_SR:
892 		case SEG_TYPE_ER:
893 		case SEG_TYPE_SC:
894 			/* cannot write to these segments */
895 			if (bio_data_dir(bio) == WRITE) {
896 				pr_warn("Writing to %s failed because it is a read-only device\n",
897 					dev_name(&dev_info->dev));
898 				goto fail;
899 			}
900 		}
901 	}
902 
903 	index = (bio->bi_iter.bi_sector >> 3);
904 	bio_for_each_segment(bvec, bio, iter) {
905 		page_addr = (unsigned long)
906 			page_address(bvec.bv_page) + bvec.bv_offset;
907 		source_addr = dev_info->start + (index<<12) + bytes_done;
908 		if (unlikely((page_addr & 4095) != 0) || (bvec.bv_len & 4095) != 0)
909 			// More paranoia.
910 			goto fail;
911 		if (bio_data_dir(bio) == READ) {
912 			memcpy((void*)page_addr, (void*)source_addr,
913 				bvec.bv_len);
914 		} else {
915 			memcpy((void*)source_addr, (void*)page_addr,
916 				bvec.bv_len);
917 		}
918 		bytes_done += bvec.bv_len;
919 	}
920 	bio_endio(bio);
921 	return BLK_QC_T_NONE;
922 fail:
923 	bio_io_error(bio);
924 	return BLK_QC_T_NONE;
925 }
926 
927 static long
928 __dcssblk_direct_access(struct dcssblk_dev_info *dev_info, pgoff_t pgoff,
929 		long nr_pages, void **kaddr, pfn_t *pfn)
930 {
931 	resource_size_t offset = pgoff * PAGE_SIZE;
932 	unsigned long dev_sz;
933 
934 	dev_sz = dev_info->end - dev_info->start + 1;
935 	if (kaddr)
936 		*kaddr = (void *) dev_info->start + offset;
937 	if (pfn)
938 		*pfn = __pfn_to_pfn_t(PFN_DOWN(dev_info->start + offset),
939 				PFN_DEV|PFN_SPECIAL);
940 
941 	return (dev_sz - offset) / PAGE_SIZE;
942 }
943 
944 static long
945 dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
946 		long nr_pages, void **kaddr, pfn_t *pfn)
947 {
948 	struct dcssblk_dev_info *dev_info = dax_get_private(dax_dev);
949 
950 	return __dcssblk_direct_access(dev_info, pgoff, nr_pages, kaddr, pfn);
951 }
952 
953 static void
954 dcssblk_check_params(void)
955 {
956 	int rc, i, j, k;
957 	char buf[DCSSBLK_PARM_LEN + 1];
958 	struct dcssblk_dev_info *dev_info;
959 
960 	for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
961 	     i++) {
962 		for (j = i; (j < DCSSBLK_PARM_LEN) &&
963 			    (dcssblk_segments[j] != ',')  &&
964 			    (dcssblk_segments[j] != '\0') &&
965 			    (dcssblk_segments[j] != '('); j++)
966 		{
967 			buf[j-i] = dcssblk_segments[j];
968 		}
969 		buf[j-i] = '\0';
970 		rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
971 		if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
972 			for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++)
973 				buf[k] = toupper(buf[k]);
974 			buf[k] = '\0';
975 			if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
976 				down_read(&dcssblk_devices_sem);
977 				dev_info = dcssblk_get_device_by_name(buf);
978 				up_read(&dcssblk_devices_sem);
979 				if (dev_info)
980 					dcssblk_shared_store(&dev_info->dev,
981 							     NULL, "0\n", 2);
982 			}
983 		}
984 		while ((dcssblk_segments[j] != ',') &&
985 		       (dcssblk_segments[j] != '\0'))
986 		{
987 			j++;
988 		}
989 		if (dcssblk_segments[j] == '\0')
990 			break;
991 		i = j;
992 	}
993 }
994 
995 /*
996  * The init/exit functions.
997  */
998 static void __exit
999 dcssblk_exit(void)
1000 {
1001 	root_device_unregister(dcssblk_root_dev);
1002 	unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
1003 }
1004 
1005 static int __init
1006 dcssblk_init(void)
1007 {
1008 	int rc;
1009 
1010 	dcssblk_root_dev = root_device_register("dcssblk");
1011 	if (IS_ERR(dcssblk_root_dev))
1012 		return PTR_ERR(dcssblk_root_dev);
1013 	rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
1014 	if (rc)
1015 		goto out_root;
1016 	rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
1017 	if (rc)
1018 		goto out_root;
1019 	rc = register_blkdev(0, DCSSBLK_NAME);
1020 	if (rc < 0)
1021 		goto out_root;
1022 	dcssblk_major = rc;
1023 	init_rwsem(&dcssblk_devices_sem);
1024 
1025 	dcssblk_check_params();
1026 	return 0;
1027 
1028 out_root:
1029 	root_device_unregister(dcssblk_root_dev);
1030 
1031 	return rc;
1032 }
1033 
1034 module_init(dcssblk_init);
1035 module_exit(dcssblk_exit);
1036 
1037 module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
1038 MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
1039 		 "comma-separated list, names in each set separated "
1040 		 "by commas are separated by colons, each set contains "
1041 		 "names of contiguous segments and each name max. 8 chars.\n"
1042 		 "Adding \"(local)\" to the end of each set equals echoing 0 "
1043 		 "to /sys/devices/dcssblk/<device name>/shared after loading "
1044 		 "the contiguous segments - \n"
1045 		 "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
1046 
1047 MODULE_LICENSE("GPL");
1048