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