1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2018 Cambridge Greys Ltd 4 * Copyright (C) 2015-2016 Anton Ivanov (aivanov@brocade.com) 5 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com) 6 */ 7 8 /* 2001-09-28...2002-04-17 9 * Partition stuff by James_McMechan@hotmail.com 10 * old style ubd by setting UBD_SHIFT to 0 11 * 2002-09-27...2002-10-18 massive tinkering for 2.5 12 * partitions have changed in 2.5 13 * 2003-01-29 more tinkering for 2.5.59-1 14 * This should now address the sysfs problems and has 15 * the symlink for devfs to allow for booting with 16 * the common /dev/ubd/discX/... names rather than 17 * only /dev/ubdN/discN this version also has lots of 18 * clean ups preparing for ubd-many. 19 * James McMechan 20 */ 21 22 #define UBD_SHIFT 4 23 24 #include <linux/module.h> 25 #include <linux/init.h> 26 #include <linux/blkdev.h> 27 #include <linux/blk-mq.h> 28 #include <linux/ata.h> 29 #include <linux/hdreg.h> 30 #include <linux/cdrom.h> 31 #include <linux/proc_fs.h> 32 #include <linux/seq_file.h> 33 #include <linux/ctype.h> 34 #include <linux/slab.h> 35 #include <linux/vmalloc.h> 36 #include <linux/platform_device.h> 37 #include <linux/scatterlist.h> 38 #include <asm/tlbflush.h> 39 #include <kern_util.h> 40 #include "mconsole_kern.h" 41 #include <init.h> 42 #include <irq_kern.h> 43 #include "ubd.h" 44 #include <os.h> 45 #include "cow.h" 46 47 /* Max request size is determined by sector mask - 32K */ 48 #define UBD_MAX_REQUEST (8 * sizeof(long)) 49 50 struct io_desc { 51 char *buffer; 52 unsigned long length; 53 unsigned long sector_mask; 54 unsigned long long cow_offset; 55 unsigned long bitmap_words[2]; 56 }; 57 58 struct io_thread_req { 59 struct request *req; 60 int fds[2]; 61 unsigned long offsets[2]; 62 unsigned long long offset; 63 int sectorsize; 64 int error; 65 66 int desc_cnt; 67 /* io_desc has to be the last element of the struct */ 68 struct io_desc io_desc[]; 69 }; 70 71 72 static struct io_thread_req * (*irq_req_buffer)[]; 73 static struct io_thread_req *irq_remainder; 74 static int irq_remainder_size; 75 76 static struct io_thread_req * (*io_req_buffer)[]; 77 static struct io_thread_req *io_remainder; 78 static int io_remainder_size; 79 80 81 82 static inline int ubd_test_bit(__u64 bit, unsigned char *data) 83 { 84 __u64 n; 85 int bits, off; 86 87 bits = sizeof(data[0]) * 8; 88 n = bit / bits; 89 off = bit % bits; 90 return (data[n] & (1 << off)) != 0; 91 } 92 93 static inline void ubd_set_bit(__u64 bit, unsigned char *data) 94 { 95 __u64 n; 96 int bits, off; 97 98 bits = sizeof(data[0]) * 8; 99 n = bit / bits; 100 off = bit % bits; 101 data[n] |= (1 << off); 102 } 103 /*End stuff from ubd_user.h*/ 104 105 #define DRIVER_NAME "uml-blkdev" 106 107 static DEFINE_MUTEX(ubd_lock); 108 static DEFINE_MUTEX(ubd_mutex); /* replaces BKL, might not be needed */ 109 110 static int ubd_open(struct block_device *bdev, fmode_t mode); 111 static void ubd_release(struct gendisk *disk, fmode_t mode); 112 static int ubd_ioctl(struct block_device *bdev, fmode_t mode, 113 unsigned int cmd, unsigned long arg); 114 static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo); 115 116 #define MAX_DEV (16) 117 118 static const struct block_device_operations ubd_blops = { 119 .owner = THIS_MODULE, 120 .open = ubd_open, 121 .release = ubd_release, 122 .ioctl = ubd_ioctl, 123 .compat_ioctl = blkdev_compat_ptr_ioctl, 124 .getgeo = ubd_getgeo, 125 }; 126 127 /* Protected by ubd_lock */ 128 static int fake_major = UBD_MAJOR; 129 static struct gendisk *ubd_gendisk[MAX_DEV]; 130 static struct gendisk *fake_gendisk[MAX_DEV]; 131 132 #ifdef CONFIG_BLK_DEV_UBD_SYNC 133 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \ 134 .cl = 1 }) 135 #else 136 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \ 137 .cl = 1 }) 138 #endif 139 static struct openflags global_openflags = OPEN_FLAGS; 140 141 struct cow { 142 /* backing file name */ 143 char *file; 144 /* backing file fd */ 145 int fd; 146 unsigned long *bitmap; 147 unsigned long bitmap_len; 148 int bitmap_offset; 149 int data_offset; 150 }; 151 152 #define MAX_SG 64 153 154 struct ubd { 155 /* name (and fd, below) of the file opened for writing, either the 156 * backing or the cow file. */ 157 char *file; 158 char *serial; 159 int count; 160 int fd; 161 __u64 size; 162 struct openflags boot_openflags; 163 struct openflags openflags; 164 unsigned shared:1; 165 unsigned no_cow:1; 166 unsigned no_trim:1; 167 struct cow cow; 168 struct platform_device pdev; 169 struct request_queue *queue; 170 struct blk_mq_tag_set tag_set; 171 spinlock_t lock; 172 }; 173 174 #define DEFAULT_COW { \ 175 .file = NULL, \ 176 .fd = -1, \ 177 .bitmap = NULL, \ 178 .bitmap_offset = 0, \ 179 .data_offset = 0, \ 180 } 181 182 #define DEFAULT_UBD { \ 183 .file = NULL, \ 184 .serial = NULL, \ 185 .count = 0, \ 186 .fd = -1, \ 187 .size = -1, \ 188 .boot_openflags = OPEN_FLAGS, \ 189 .openflags = OPEN_FLAGS, \ 190 .no_cow = 0, \ 191 .no_trim = 0, \ 192 .shared = 0, \ 193 .cow = DEFAULT_COW, \ 194 .lock = __SPIN_LOCK_UNLOCKED(ubd_devs.lock), \ 195 } 196 197 /* Protected by ubd_lock */ 198 static struct ubd ubd_devs[MAX_DEV] = { [0 ... MAX_DEV - 1] = DEFAULT_UBD }; 199 200 /* Only changed by fake_ide_setup which is a setup */ 201 static int fake_ide = 0; 202 static struct proc_dir_entry *proc_ide_root = NULL; 203 static struct proc_dir_entry *proc_ide = NULL; 204 205 static blk_status_t ubd_queue_rq(struct blk_mq_hw_ctx *hctx, 206 const struct blk_mq_queue_data *bd); 207 208 static void make_proc_ide(void) 209 { 210 proc_ide_root = proc_mkdir("ide", NULL); 211 proc_ide = proc_mkdir("ide0", proc_ide_root); 212 } 213 214 static int fake_ide_media_proc_show(struct seq_file *m, void *v) 215 { 216 seq_puts(m, "disk\n"); 217 return 0; 218 } 219 220 static void make_ide_entries(const char *dev_name) 221 { 222 struct proc_dir_entry *dir, *ent; 223 char name[64]; 224 225 if(proc_ide_root == NULL) make_proc_ide(); 226 227 dir = proc_mkdir(dev_name, proc_ide); 228 if(!dir) return; 229 230 ent = proc_create_single("media", S_IRUGO, dir, 231 fake_ide_media_proc_show); 232 if(!ent) return; 233 snprintf(name, sizeof(name), "ide0/%s", dev_name); 234 proc_symlink(dev_name, proc_ide_root, name); 235 } 236 237 static int fake_ide_setup(char *str) 238 { 239 fake_ide = 1; 240 return 1; 241 } 242 243 __setup("fake_ide", fake_ide_setup); 244 245 __uml_help(fake_ide_setup, 246 "fake_ide\n" 247 " Create ide0 entries that map onto ubd devices.\n\n" 248 ); 249 250 static int parse_unit(char **ptr) 251 { 252 char *str = *ptr, *end; 253 int n = -1; 254 255 if(isdigit(*str)) { 256 n = simple_strtoul(str, &end, 0); 257 if(end == str) 258 return -1; 259 *ptr = end; 260 } 261 else if (('a' <= *str) && (*str <= 'z')) { 262 n = *str - 'a'; 263 str++; 264 *ptr = str; 265 } 266 return n; 267 } 268 269 /* If *index_out == -1 at exit, the passed option was a general one; 270 * otherwise, the str pointer is used (and owned) inside ubd_devs array, so it 271 * should not be freed on exit. 272 */ 273 static int ubd_setup_common(char *str, int *index_out, char **error_out) 274 { 275 struct ubd *ubd_dev; 276 struct openflags flags = global_openflags; 277 char *file, *backing_file, *serial; 278 int n, err = 0, i; 279 280 if(index_out) *index_out = -1; 281 n = *str; 282 if(n == '='){ 283 char *end; 284 int major; 285 286 str++; 287 if(!strcmp(str, "sync")){ 288 global_openflags = of_sync(global_openflags); 289 return err; 290 } 291 292 err = -EINVAL; 293 major = simple_strtoul(str, &end, 0); 294 if((*end != '\0') || (end == str)){ 295 *error_out = "Didn't parse major number"; 296 return err; 297 } 298 299 mutex_lock(&ubd_lock); 300 if (fake_major != UBD_MAJOR) { 301 *error_out = "Can't assign a fake major twice"; 302 goto out1; 303 } 304 305 fake_major = major; 306 307 printk(KERN_INFO "Setting extra ubd major number to %d\n", 308 major); 309 err = 0; 310 out1: 311 mutex_unlock(&ubd_lock); 312 return err; 313 } 314 315 n = parse_unit(&str); 316 if(n < 0){ 317 *error_out = "Couldn't parse device number"; 318 return -EINVAL; 319 } 320 if(n >= MAX_DEV){ 321 *error_out = "Device number out of range"; 322 return 1; 323 } 324 325 err = -EBUSY; 326 mutex_lock(&ubd_lock); 327 328 ubd_dev = &ubd_devs[n]; 329 if(ubd_dev->file != NULL){ 330 *error_out = "Device is already configured"; 331 goto out; 332 } 333 334 if (index_out) 335 *index_out = n; 336 337 err = -EINVAL; 338 for (i = 0; i < sizeof("rscdt="); i++) { 339 switch (*str) { 340 case 'r': 341 flags.w = 0; 342 break; 343 case 's': 344 flags.s = 1; 345 break; 346 case 'd': 347 ubd_dev->no_cow = 1; 348 break; 349 case 'c': 350 ubd_dev->shared = 1; 351 break; 352 case 't': 353 ubd_dev->no_trim = 1; 354 break; 355 case '=': 356 str++; 357 goto break_loop; 358 default: 359 *error_out = "Expected '=' or flag letter " 360 "(r, s, c, t or d)"; 361 goto out; 362 } 363 str++; 364 } 365 366 if (*str == '=') 367 *error_out = "Too many flags specified"; 368 else 369 *error_out = "Missing '='"; 370 goto out; 371 372 break_loop: 373 file = strsep(&str, ",:"); 374 if (*file == '\0') 375 file = NULL; 376 377 backing_file = strsep(&str, ",:"); 378 if (backing_file && *backing_file == '\0') 379 backing_file = NULL; 380 381 serial = strsep(&str, ",:"); 382 if (serial && *serial == '\0') 383 serial = NULL; 384 385 if (backing_file && ubd_dev->no_cow) { 386 *error_out = "Can't specify both 'd' and a cow file"; 387 goto out; 388 } 389 390 err = 0; 391 ubd_dev->file = file; 392 ubd_dev->cow.file = backing_file; 393 ubd_dev->serial = serial; 394 ubd_dev->boot_openflags = flags; 395 out: 396 mutex_unlock(&ubd_lock); 397 return err; 398 } 399 400 static int ubd_setup(char *str) 401 { 402 char *error; 403 int err; 404 405 err = ubd_setup_common(str, NULL, &error); 406 if(err) 407 printk(KERN_ERR "Failed to initialize device with \"%s\" : " 408 "%s\n", str, error); 409 return 1; 410 } 411 412 __setup("ubd", ubd_setup); 413 __uml_help(ubd_setup, 414 "ubd<n><flags>=<filename>[(:|,)<filename2>][(:|,)<serial>]\n" 415 " This is used to associate a device with a file in the underlying\n" 416 " filesystem. When specifying two filenames, the first one is the\n" 417 " COW name and the second is the backing file name. As separator you can\n" 418 " use either a ':' or a ',': the first one allows writing things like;\n" 419 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n" 420 " while with a ',' the shell would not expand the 2nd '~'.\n" 421 " When using only one filename, UML will detect whether to treat it like\n" 422 " a COW file or a backing file. To override this detection, add the 'd'\n" 423 " flag:\n" 424 " ubd0d=BackingFile\n" 425 " Usually, there is a filesystem in the file, but \n" 426 " that's not required. Swap devices containing swap files can be\n" 427 " specified like this. Also, a file which doesn't contain a\n" 428 " filesystem can have its contents read in the virtual \n" 429 " machine by running 'dd' on the device. <n> must be in the range\n" 430 " 0 to 7. Appending an 'r' to the number will cause that device\n" 431 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n" 432 " an 's' will cause data to be written to disk on the host immediately.\n" 433 " 'c' will cause the device to be treated as being shared between multiple\n" 434 " UMLs and file locking will be turned off - this is appropriate for a\n" 435 " cluster filesystem and inappropriate at almost all other times.\n\n" 436 " 't' will disable trim/discard support on the device (enabled by default).\n\n" 437 " An optional device serial number can be exposed using the serial parameter\n" 438 " on the cmdline which is exposed as a sysfs entry. This is particularly\n" 439 " useful when a unique number should be given to the device. Note when\n" 440 " specifying a label, the filename2 must be also presented. It can be\n" 441 " an empty string, in which case the backing file is not used:\n" 442 " ubd0=File,,Serial\n" 443 ); 444 445 static int udb_setup(char *str) 446 { 447 printk("udb%s specified on command line is almost certainly a ubd -> " 448 "udb TYPO\n", str); 449 return 1; 450 } 451 452 __setup("udb", udb_setup); 453 __uml_help(udb_setup, 454 "udb\n" 455 " This option is here solely to catch ubd -> udb typos, which can be\n" 456 " to impossible to catch visually unless you specifically look for\n" 457 " them. The only result of any option starting with 'udb' is an error\n" 458 " in the boot output.\n\n" 459 ); 460 461 /* Only changed by ubd_init, which is an initcall. */ 462 static int thread_fd = -1; 463 464 /* Function to read several request pointers at a time 465 * handling fractional reads if (and as) needed 466 */ 467 468 static int bulk_req_safe_read( 469 int fd, 470 struct io_thread_req * (*request_buffer)[], 471 struct io_thread_req **remainder, 472 int *remainder_size, 473 int max_recs 474 ) 475 { 476 int n = 0; 477 int res = 0; 478 479 if (*remainder_size > 0) { 480 memmove( 481 (char *) request_buffer, 482 (char *) remainder, *remainder_size 483 ); 484 n = *remainder_size; 485 } 486 487 res = os_read_file( 488 fd, 489 ((char *) request_buffer) + *remainder_size, 490 sizeof(struct io_thread_req *)*max_recs 491 - *remainder_size 492 ); 493 if (res > 0) { 494 n += res; 495 if ((n % sizeof(struct io_thread_req *)) > 0) { 496 /* 497 * Read somehow returned not a multiple of dword 498 * theoretically possible, but never observed in the 499 * wild, so read routine must be able to handle it 500 */ 501 *remainder_size = n % sizeof(struct io_thread_req *); 502 WARN(*remainder_size > 0, "UBD IPC read returned a partial result"); 503 memmove( 504 remainder, 505 ((char *) request_buffer) + 506 (n/sizeof(struct io_thread_req *))*sizeof(struct io_thread_req *), 507 *remainder_size 508 ); 509 n = n - *remainder_size; 510 } 511 } else { 512 n = res; 513 } 514 return n; 515 } 516 517 /* Called without dev->lock held, and only in interrupt context. */ 518 static void ubd_handler(void) 519 { 520 int n; 521 int count; 522 523 while(1){ 524 n = bulk_req_safe_read( 525 thread_fd, 526 irq_req_buffer, 527 &irq_remainder, 528 &irq_remainder_size, 529 UBD_REQ_BUFFER_SIZE 530 ); 531 if (n < 0) { 532 if(n == -EAGAIN) 533 break; 534 printk(KERN_ERR "spurious interrupt in ubd_handler, " 535 "err = %d\n", -n); 536 return; 537 } 538 for (count = 0; count < n/sizeof(struct io_thread_req *); count++) { 539 struct io_thread_req *io_req = (*irq_req_buffer)[count]; 540 541 if ((io_req->error == BLK_STS_NOTSUPP) && (req_op(io_req->req) == REQ_OP_DISCARD)) { 542 blk_queue_max_discard_sectors(io_req->req->q, 0); 543 blk_queue_max_write_zeroes_sectors(io_req->req->q, 0); 544 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, io_req->req->q); 545 } 546 blk_mq_end_request(io_req->req, io_req->error); 547 kfree(io_req); 548 } 549 } 550 } 551 552 static irqreturn_t ubd_intr(int irq, void *dev) 553 { 554 ubd_handler(); 555 return IRQ_HANDLED; 556 } 557 558 /* Only changed by ubd_init, which is an initcall. */ 559 static int io_pid = -1; 560 561 static void kill_io_thread(void) 562 { 563 if(io_pid != -1) 564 os_kill_process(io_pid, 1); 565 } 566 567 __uml_exitcall(kill_io_thread); 568 569 static inline int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out) 570 { 571 char *file; 572 int fd; 573 int err; 574 575 __u32 version; 576 __u32 align; 577 char *backing_file; 578 time64_t mtime; 579 unsigned long long size; 580 int sector_size; 581 int bitmap_offset; 582 583 if (ubd_dev->file && ubd_dev->cow.file) { 584 file = ubd_dev->cow.file; 585 586 goto out; 587 } 588 589 fd = os_open_file(ubd_dev->file, of_read(OPENFLAGS()), 0); 590 if (fd < 0) 591 return fd; 592 593 err = read_cow_header(file_reader, &fd, &version, &backing_file, \ 594 &mtime, &size, §or_size, &align, &bitmap_offset); 595 os_close_file(fd); 596 597 if(err == -EINVAL) 598 file = ubd_dev->file; 599 else 600 file = backing_file; 601 602 out: 603 return os_file_size(file, size_out); 604 } 605 606 static int read_cow_bitmap(int fd, void *buf, int offset, int len) 607 { 608 int err; 609 610 err = os_pread_file(fd, buf, len, offset); 611 if (err < 0) 612 return err; 613 614 return 0; 615 } 616 617 static int backing_file_mismatch(char *file, __u64 size, time64_t mtime) 618 { 619 time64_t modtime; 620 unsigned long long actual; 621 int err; 622 623 err = os_file_modtime(file, &modtime); 624 if (err < 0) { 625 printk(KERN_ERR "Failed to get modification time of backing " 626 "file \"%s\", err = %d\n", file, -err); 627 return err; 628 } 629 630 err = os_file_size(file, &actual); 631 if (err < 0) { 632 printk(KERN_ERR "Failed to get size of backing file \"%s\", " 633 "err = %d\n", file, -err); 634 return err; 635 } 636 637 if (actual != size) { 638 /*__u64 can be a long on AMD64 and with %lu GCC complains; so 639 * the typecast.*/ 640 printk(KERN_ERR "Size mismatch (%llu vs %llu) of COW header " 641 "vs backing file\n", (unsigned long long) size, actual); 642 return -EINVAL; 643 } 644 if (modtime != mtime) { 645 printk(KERN_ERR "mtime mismatch (%lld vs %lld) of COW header vs " 646 "backing file\n", mtime, modtime); 647 return -EINVAL; 648 } 649 return 0; 650 } 651 652 static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow) 653 { 654 struct uml_stat buf1, buf2; 655 int err; 656 657 if (from_cmdline == NULL) 658 return 0; 659 if (!strcmp(from_cmdline, from_cow)) 660 return 0; 661 662 err = os_stat_file(from_cmdline, &buf1); 663 if (err < 0) { 664 printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cmdline, 665 -err); 666 return 0; 667 } 668 err = os_stat_file(from_cow, &buf2); 669 if (err < 0) { 670 printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cow, 671 -err); 672 return 1; 673 } 674 if ((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino)) 675 return 0; 676 677 printk(KERN_ERR "Backing file mismatch - \"%s\" requested, " 678 "\"%s\" specified in COW header of \"%s\"\n", 679 from_cmdline, from_cow, cow); 680 return 1; 681 } 682 683 static int open_ubd_file(char *file, struct openflags *openflags, int shared, 684 char **backing_file_out, int *bitmap_offset_out, 685 unsigned long *bitmap_len_out, int *data_offset_out, 686 int *create_cow_out) 687 { 688 time64_t mtime; 689 unsigned long long size; 690 __u32 version, align; 691 char *backing_file; 692 int fd, err, sectorsize, asked_switch, mode = 0644; 693 694 fd = os_open_file(file, *openflags, mode); 695 if (fd < 0) { 696 if ((fd == -ENOENT) && (create_cow_out != NULL)) 697 *create_cow_out = 1; 698 if (!openflags->w || 699 ((fd != -EROFS) && (fd != -EACCES))) 700 return fd; 701 openflags->w = 0; 702 fd = os_open_file(file, *openflags, mode); 703 if (fd < 0) 704 return fd; 705 } 706 707 if (shared) 708 printk(KERN_INFO "Not locking \"%s\" on the host\n", file); 709 else { 710 err = os_lock_file(fd, openflags->w); 711 if (err < 0) { 712 printk(KERN_ERR "Failed to lock '%s', err = %d\n", 713 file, -err); 714 goto out_close; 715 } 716 } 717 718 /* Successful return case! */ 719 if (backing_file_out == NULL) 720 return fd; 721 722 err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime, 723 &size, §orsize, &align, bitmap_offset_out); 724 if (err && (*backing_file_out != NULL)) { 725 printk(KERN_ERR "Failed to read COW header from COW file " 726 "\"%s\", errno = %d\n", file, -err); 727 goto out_close; 728 } 729 if (err) 730 return fd; 731 732 asked_switch = path_requires_switch(*backing_file_out, backing_file, 733 file); 734 735 /* Allow switching only if no mismatch. */ 736 if (asked_switch && !backing_file_mismatch(*backing_file_out, size, 737 mtime)) { 738 printk(KERN_ERR "Switching backing file to '%s'\n", 739 *backing_file_out); 740 err = write_cow_header(file, fd, *backing_file_out, 741 sectorsize, align, &size); 742 if (err) { 743 printk(KERN_ERR "Switch failed, errno = %d\n", -err); 744 goto out_close; 745 } 746 } else { 747 *backing_file_out = backing_file; 748 err = backing_file_mismatch(*backing_file_out, size, mtime); 749 if (err) 750 goto out_close; 751 } 752 753 cow_sizes(version, size, sectorsize, align, *bitmap_offset_out, 754 bitmap_len_out, data_offset_out); 755 756 return fd; 757 out_close: 758 os_close_file(fd); 759 return err; 760 } 761 762 static int create_cow_file(char *cow_file, char *backing_file, 763 struct openflags flags, 764 int sectorsize, int alignment, int *bitmap_offset_out, 765 unsigned long *bitmap_len_out, int *data_offset_out) 766 { 767 int err, fd; 768 769 flags.c = 1; 770 fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL); 771 if (fd < 0) { 772 err = fd; 773 printk(KERN_ERR "Open of COW file '%s' failed, errno = %d\n", 774 cow_file, -err); 775 goto out; 776 } 777 778 err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment, 779 bitmap_offset_out, bitmap_len_out, 780 data_offset_out); 781 if (!err) 782 return fd; 783 os_close_file(fd); 784 out: 785 return err; 786 } 787 788 static void ubd_close_dev(struct ubd *ubd_dev) 789 { 790 os_close_file(ubd_dev->fd); 791 if(ubd_dev->cow.file == NULL) 792 return; 793 794 os_close_file(ubd_dev->cow.fd); 795 vfree(ubd_dev->cow.bitmap); 796 ubd_dev->cow.bitmap = NULL; 797 } 798 799 static int ubd_open_dev(struct ubd *ubd_dev) 800 { 801 struct openflags flags; 802 char **back_ptr; 803 int err, create_cow, *create_ptr; 804 int fd; 805 806 ubd_dev->openflags = ubd_dev->boot_openflags; 807 create_cow = 0; 808 create_ptr = (ubd_dev->cow.file != NULL) ? &create_cow : NULL; 809 back_ptr = ubd_dev->no_cow ? NULL : &ubd_dev->cow.file; 810 811 fd = open_ubd_file(ubd_dev->file, &ubd_dev->openflags, ubd_dev->shared, 812 back_ptr, &ubd_dev->cow.bitmap_offset, 813 &ubd_dev->cow.bitmap_len, &ubd_dev->cow.data_offset, 814 create_ptr); 815 816 if((fd == -ENOENT) && create_cow){ 817 fd = create_cow_file(ubd_dev->file, ubd_dev->cow.file, 818 ubd_dev->openflags, SECTOR_SIZE, PAGE_SIZE, 819 &ubd_dev->cow.bitmap_offset, 820 &ubd_dev->cow.bitmap_len, 821 &ubd_dev->cow.data_offset); 822 if(fd >= 0){ 823 printk(KERN_INFO "Creating \"%s\" as COW file for " 824 "\"%s\"\n", ubd_dev->file, ubd_dev->cow.file); 825 } 826 } 827 828 if(fd < 0){ 829 printk("Failed to open '%s', errno = %d\n", ubd_dev->file, 830 -fd); 831 return fd; 832 } 833 ubd_dev->fd = fd; 834 835 if(ubd_dev->cow.file != NULL){ 836 blk_queue_max_hw_sectors(ubd_dev->queue, 8 * sizeof(long)); 837 838 err = -ENOMEM; 839 ubd_dev->cow.bitmap = vmalloc(ubd_dev->cow.bitmap_len); 840 if(ubd_dev->cow.bitmap == NULL){ 841 printk(KERN_ERR "Failed to vmalloc COW bitmap\n"); 842 goto error; 843 } 844 flush_tlb_kernel_vm(); 845 846 err = read_cow_bitmap(ubd_dev->fd, ubd_dev->cow.bitmap, 847 ubd_dev->cow.bitmap_offset, 848 ubd_dev->cow.bitmap_len); 849 if(err < 0) 850 goto error; 851 852 flags = ubd_dev->openflags; 853 flags.w = 0; 854 err = open_ubd_file(ubd_dev->cow.file, &flags, ubd_dev->shared, NULL, 855 NULL, NULL, NULL, NULL); 856 if(err < 0) goto error; 857 ubd_dev->cow.fd = err; 858 } 859 if (ubd_dev->no_trim == 0) { 860 ubd_dev->queue->limits.discard_granularity = SECTOR_SIZE; 861 ubd_dev->queue->limits.discard_alignment = SECTOR_SIZE; 862 blk_queue_max_discard_sectors(ubd_dev->queue, UBD_MAX_REQUEST); 863 blk_queue_max_write_zeroes_sectors(ubd_dev->queue, UBD_MAX_REQUEST); 864 blk_queue_flag_set(QUEUE_FLAG_DISCARD, ubd_dev->queue); 865 } 866 blk_queue_flag_set(QUEUE_FLAG_NONROT, ubd_dev->queue); 867 return 0; 868 error: 869 os_close_file(ubd_dev->fd); 870 return err; 871 } 872 873 static void ubd_device_release(struct device *dev) 874 { 875 struct ubd *ubd_dev = dev_get_drvdata(dev); 876 877 blk_cleanup_queue(ubd_dev->queue); 878 blk_mq_free_tag_set(&ubd_dev->tag_set); 879 *ubd_dev = ((struct ubd) DEFAULT_UBD); 880 } 881 882 static ssize_t serial_show(struct device *dev, 883 struct device_attribute *attr, char *buf) 884 { 885 struct gendisk *disk = dev_to_disk(dev); 886 struct ubd *ubd_dev = disk->private_data; 887 888 if (!ubd_dev) 889 return 0; 890 891 return sprintf(buf, "%s", ubd_dev->serial); 892 } 893 894 static DEVICE_ATTR_RO(serial); 895 896 static struct attribute *ubd_attrs[] = { 897 &dev_attr_serial.attr, 898 NULL, 899 }; 900 901 static umode_t ubd_attrs_are_visible(struct kobject *kobj, 902 struct attribute *a, int n) 903 { 904 return a->mode; 905 } 906 907 static const struct attribute_group ubd_attr_group = { 908 .attrs = ubd_attrs, 909 .is_visible = ubd_attrs_are_visible, 910 }; 911 912 static const struct attribute_group *ubd_attr_groups[] = { 913 &ubd_attr_group, 914 NULL, 915 }; 916 917 static int ubd_disk_register(int major, u64 size, int unit, 918 struct gendisk **disk_out) 919 { 920 struct device *parent = NULL; 921 struct gendisk *disk; 922 923 disk = alloc_disk(1 << UBD_SHIFT); 924 if(disk == NULL) 925 return -ENOMEM; 926 927 disk->major = major; 928 disk->first_minor = unit << UBD_SHIFT; 929 disk->fops = &ubd_blops; 930 set_capacity(disk, size / 512); 931 if (major == UBD_MAJOR) 932 sprintf(disk->disk_name, "ubd%c", 'a' + unit); 933 else 934 sprintf(disk->disk_name, "ubd_fake%d", unit); 935 936 /* sysfs register (not for ide fake devices) */ 937 if (major == UBD_MAJOR) { 938 ubd_devs[unit].pdev.id = unit; 939 ubd_devs[unit].pdev.name = DRIVER_NAME; 940 ubd_devs[unit].pdev.dev.release = ubd_device_release; 941 dev_set_drvdata(&ubd_devs[unit].pdev.dev, &ubd_devs[unit]); 942 platform_device_register(&ubd_devs[unit].pdev); 943 parent = &ubd_devs[unit].pdev.dev; 944 } 945 946 disk->private_data = &ubd_devs[unit]; 947 disk->queue = ubd_devs[unit].queue; 948 device_add_disk(parent, disk, ubd_attr_groups); 949 950 *disk_out = disk; 951 return 0; 952 } 953 954 #define ROUND_BLOCK(n) ((n + (SECTOR_SIZE - 1)) & (-SECTOR_SIZE)) 955 956 static const struct blk_mq_ops ubd_mq_ops = { 957 .queue_rq = ubd_queue_rq, 958 }; 959 960 static int ubd_add(int n, char **error_out) 961 { 962 struct ubd *ubd_dev = &ubd_devs[n]; 963 int err = 0; 964 965 if(ubd_dev->file == NULL) 966 goto out; 967 968 err = ubd_file_size(ubd_dev, &ubd_dev->size); 969 if(err < 0){ 970 *error_out = "Couldn't determine size of device's file"; 971 goto out; 972 } 973 974 ubd_dev->size = ROUND_BLOCK(ubd_dev->size); 975 976 ubd_dev->tag_set.ops = &ubd_mq_ops; 977 ubd_dev->tag_set.queue_depth = 64; 978 ubd_dev->tag_set.numa_node = NUMA_NO_NODE; 979 ubd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; 980 ubd_dev->tag_set.driver_data = ubd_dev; 981 ubd_dev->tag_set.nr_hw_queues = 1; 982 983 err = blk_mq_alloc_tag_set(&ubd_dev->tag_set); 984 if (err) 985 goto out; 986 987 ubd_dev->queue = blk_mq_init_queue(&ubd_dev->tag_set); 988 if (IS_ERR(ubd_dev->queue)) { 989 err = PTR_ERR(ubd_dev->queue); 990 goto out_cleanup_tags; 991 } 992 993 ubd_dev->queue->queuedata = ubd_dev; 994 blk_queue_write_cache(ubd_dev->queue, true, false); 995 996 blk_queue_max_segments(ubd_dev->queue, MAX_SG); 997 blk_queue_segment_boundary(ubd_dev->queue, PAGE_SIZE - 1); 998 err = ubd_disk_register(UBD_MAJOR, ubd_dev->size, n, &ubd_gendisk[n]); 999 if(err){ 1000 *error_out = "Failed to register device"; 1001 goto out_cleanup_tags; 1002 } 1003 1004 if (fake_major != UBD_MAJOR) 1005 ubd_disk_register(fake_major, ubd_dev->size, n, 1006 &fake_gendisk[n]); 1007 1008 /* 1009 * Perhaps this should also be under the "if (fake_major)" above 1010 * using the fake_disk->disk_name 1011 */ 1012 if (fake_ide) 1013 make_ide_entries(ubd_gendisk[n]->disk_name); 1014 1015 err = 0; 1016 out: 1017 return err; 1018 1019 out_cleanup_tags: 1020 blk_mq_free_tag_set(&ubd_dev->tag_set); 1021 if (!(IS_ERR(ubd_dev->queue))) 1022 blk_cleanup_queue(ubd_dev->queue); 1023 goto out; 1024 } 1025 1026 static int ubd_config(char *str, char **error_out) 1027 { 1028 int n, ret; 1029 1030 /* This string is possibly broken up and stored, so it's only 1031 * freed if ubd_setup_common fails, or if only general options 1032 * were set. 1033 */ 1034 str = kstrdup(str, GFP_KERNEL); 1035 if (str == NULL) { 1036 *error_out = "Failed to allocate memory"; 1037 return -ENOMEM; 1038 } 1039 1040 ret = ubd_setup_common(str, &n, error_out); 1041 if (ret) 1042 goto err_free; 1043 1044 if (n == -1) { 1045 ret = 0; 1046 goto err_free; 1047 } 1048 1049 mutex_lock(&ubd_lock); 1050 ret = ubd_add(n, error_out); 1051 if (ret) 1052 ubd_devs[n].file = NULL; 1053 mutex_unlock(&ubd_lock); 1054 1055 out: 1056 return ret; 1057 1058 err_free: 1059 kfree(str); 1060 goto out; 1061 } 1062 1063 static int ubd_get_config(char *name, char *str, int size, char **error_out) 1064 { 1065 struct ubd *ubd_dev; 1066 int n, len = 0; 1067 1068 n = parse_unit(&name); 1069 if((n >= MAX_DEV) || (n < 0)){ 1070 *error_out = "ubd_get_config : device number out of range"; 1071 return -1; 1072 } 1073 1074 ubd_dev = &ubd_devs[n]; 1075 mutex_lock(&ubd_lock); 1076 1077 if(ubd_dev->file == NULL){ 1078 CONFIG_CHUNK(str, size, len, "", 1); 1079 goto out; 1080 } 1081 1082 CONFIG_CHUNK(str, size, len, ubd_dev->file, 0); 1083 1084 if(ubd_dev->cow.file != NULL){ 1085 CONFIG_CHUNK(str, size, len, ",", 0); 1086 CONFIG_CHUNK(str, size, len, ubd_dev->cow.file, 1); 1087 } 1088 else CONFIG_CHUNK(str, size, len, "", 1); 1089 1090 out: 1091 mutex_unlock(&ubd_lock); 1092 return len; 1093 } 1094 1095 static int ubd_id(char **str, int *start_out, int *end_out) 1096 { 1097 int n; 1098 1099 n = parse_unit(str); 1100 *start_out = 0; 1101 *end_out = MAX_DEV - 1; 1102 return n; 1103 } 1104 1105 static int ubd_remove(int n, char **error_out) 1106 { 1107 struct gendisk *disk = ubd_gendisk[n]; 1108 struct ubd *ubd_dev; 1109 int err = -ENODEV; 1110 1111 mutex_lock(&ubd_lock); 1112 1113 ubd_dev = &ubd_devs[n]; 1114 1115 if(ubd_dev->file == NULL) 1116 goto out; 1117 1118 /* you cannot remove a open disk */ 1119 err = -EBUSY; 1120 if(ubd_dev->count > 0) 1121 goto out; 1122 1123 ubd_gendisk[n] = NULL; 1124 if(disk != NULL){ 1125 del_gendisk(disk); 1126 put_disk(disk); 1127 } 1128 1129 if(fake_gendisk[n] != NULL){ 1130 del_gendisk(fake_gendisk[n]); 1131 put_disk(fake_gendisk[n]); 1132 fake_gendisk[n] = NULL; 1133 } 1134 1135 err = 0; 1136 platform_device_unregister(&ubd_dev->pdev); 1137 out: 1138 mutex_unlock(&ubd_lock); 1139 return err; 1140 } 1141 1142 /* All these are called by mconsole in process context and without 1143 * ubd-specific locks. The structure itself is const except for .list. 1144 */ 1145 static struct mc_device ubd_mc = { 1146 .list = LIST_HEAD_INIT(ubd_mc.list), 1147 .name = "ubd", 1148 .config = ubd_config, 1149 .get_config = ubd_get_config, 1150 .id = ubd_id, 1151 .remove = ubd_remove, 1152 }; 1153 1154 static int __init ubd_mc_init(void) 1155 { 1156 mconsole_register_dev(&ubd_mc); 1157 return 0; 1158 } 1159 1160 __initcall(ubd_mc_init); 1161 1162 static int __init ubd0_init(void) 1163 { 1164 struct ubd *ubd_dev = &ubd_devs[0]; 1165 1166 mutex_lock(&ubd_lock); 1167 if(ubd_dev->file == NULL) 1168 ubd_dev->file = "root_fs"; 1169 mutex_unlock(&ubd_lock); 1170 1171 return 0; 1172 } 1173 1174 __initcall(ubd0_init); 1175 1176 /* Used in ubd_init, which is an initcall */ 1177 static struct platform_driver ubd_driver = { 1178 .driver = { 1179 .name = DRIVER_NAME, 1180 }, 1181 }; 1182 1183 static int __init ubd_init(void) 1184 { 1185 char *error; 1186 int i, err; 1187 1188 if (register_blkdev(UBD_MAJOR, "ubd")) 1189 return -1; 1190 1191 if (fake_major != UBD_MAJOR) { 1192 char name[sizeof("ubd_nnn\0")]; 1193 1194 snprintf(name, sizeof(name), "ubd_%d", fake_major); 1195 if (register_blkdev(fake_major, "ubd")) 1196 return -1; 1197 } 1198 1199 irq_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE, 1200 sizeof(struct io_thread_req *), 1201 GFP_KERNEL 1202 ); 1203 irq_remainder = 0; 1204 1205 if (irq_req_buffer == NULL) { 1206 printk(KERN_ERR "Failed to initialize ubd buffering\n"); 1207 return -1; 1208 } 1209 io_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE, 1210 sizeof(struct io_thread_req *), 1211 GFP_KERNEL 1212 ); 1213 1214 io_remainder = 0; 1215 1216 if (io_req_buffer == NULL) { 1217 printk(KERN_ERR "Failed to initialize ubd buffering\n"); 1218 return -1; 1219 } 1220 platform_driver_register(&ubd_driver); 1221 mutex_lock(&ubd_lock); 1222 for (i = 0; i < MAX_DEV; i++){ 1223 err = ubd_add(i, &error); 1224 if(err) 1225 printk(KERN_ERR "Failed to initialize ubd device %d :" 1226 "%s\n", i, error); 1227 } 1228 mutex_unlock(&ubd_lock); 1229 return 0; 1230 } 1231 1232 late_initcall(ubd_init); 1233 1234 static int __init ubd_driver_init(void){ 1235 unsigned long stack; 1236 int err; 1237 1238 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/ 1239 if(global_openflags.s){ 1240 printk(KERN_INFO "ubd: Synchronous mode\n"); 1241 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is 1242 * enough. So use anyway the io thread. */ 1243 } 1244 stack = alloc_stack(0, 0); 1245 io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *), 1246 &thread_fd); 1247 if(io_pid < 0){ 1248 printk(KERN_ERR 1249 "ubd : Failed to start I/O thread (errno = %d) - " 1250 "falling back to synchronous I/O\n", -io_pid); 1251 io_pid = -1; 1252 return 0; 1253 } 1254 err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr, 1255 0, "ubd", ubd_devs); 1256 if(err < 0) 1257 printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err); 1258 return 0; 1259 } 1260 1261 device_initcall(ubd_driver_init); 1262 1263 static int ubd_open(struct block_device *bdev, fmode_t mode) 1264 { 1265 struct gendisk *disk = bdev->bd_disk; 1266 struct ubd *ubd_dev = disk->private_data; 1267 int err = 0; 1268 1269 mutex_lock(&ubd_mutex); 1270 if(ubd_dev->count == 0){ 1271 err = ubd_open_dev(ubd_dev); 1272 if(err){ 1273 printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n", 1274 disk->disk_name, ubd_dev->file, -err); 1275 goto out; 1276 } 1277 } 1278 ubd_dev->count++; 1279 set_disk_ro(disk, !ubd_dev->openflags.w); 1280 1281 /* This should no more be needed. And it didn't work anyway to exclude 1282 * read-write remounting of filesystems.*/ 1283 /*if((mode & FMODE_WRITE) && !ubd_dev->openflags.w){ 1284 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev); 1285 err = -EROFS; 1286 }*/ 1287 out: 1288 mutex_unlock(&ubd_mutex); 1289 return err; 1290 } 1291 1292 static void ubd_release(struct gendisk *disk, fmode_t mode) 1293 { 1294 struct ubd *ubd_dev = disk->private_data; 1295 1296 mutex_lock(&ubd_mutex); 1297 if(--ubd_dev->count == 0) 1298 ubd_close_dev(ubd_dev); 1299 mutex_unlock(&ubd_mutex); 1300 } 1301 1302 static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask, 1303 __u64 *cow_offset, unsigned long *bitmap, 1304 __u64 bitmap_offset, unsigned long *bitmap_words, 1305 __u64 bitmap_len) 1306 { 1307 __u64 sector = io_offset >> SECTOR_SHIFT; 1308 int i, update_bitmap = 0; 1309 1310 for (i = 0; i < length >> SECTOR_SHIFT; i++) { 1311 if(cow_mask != NULL) 1312 ubd_set_bit(i, (unsigned char *) cow_mask); 1313 if(ubd_test_bit(sector + i, (unsigned char *) bitmap)) 1314 continue; 1315 1316 update_bitmap = 1; 1317 ubd_set_bit(sector + i, (unsigned char *) bitmap); 1318 } 1319 1320 if(!update_bitmap) 1321 return; 1322 1323 *cow_offset = sector / (sizeof(unsigned long) * 8); 1324 1325 /* This takes care of the case where we're exactly at the end of the 1326 * device, and *cow_offset + 1 is off the end. So, just back it up 1327 * by one word. Thanks to Lynn Kerby for the fix and James McMechan 1328 * for the original diagnosis. 1329 */ 1330 if (*cow_offset == (DIV_ROUND_UP(bitmap_len, 1331 sizeof(unsigned long)) - 1)) 1332 (*cow_offset)--; 1333 1334 bitmap_words[0] = bitmap[*cow_offset]; 1335 bitmap_words[1] = bitmap[*cow_offset + 1]; 1336 1337 *cow_offset *= sizeof(unsigned long); 1338 *cow_offset += bitmap_offset; 1339 } 1340 1341 static void cowify_req(struct io_thread_req *req, struct io_desc *segment, 1342 unsigned long offset, unsigned long *bitmap, 1343 __u64 bitmap_offset, __u64 bitmap_len) 1344 { 1345 __u64 sector = offset >> SECTOR_SHIFT; 1346 int i; 1347 1348 if (segment->length > (sizeof(segment->sector_mask) * 8) << SECTOR_SHIFT) 1349 panic("Operation too long"); 1350 1351 if (req_op(req->req) == REQ_OP_READ) { 1352 for (i = 0; i < segment->length >> SECTOR_SHIFT; i++) { 1353 if(ubd_test_bit(sector + i, (unsigned char *) bitmap)) 1354 ubd_set_bit(i, (unsigned char *) 1355 &segment->sector_mask); 1356 } 1357 } else { 1358 cowify_bitmap(offset, segment->length, &segment->sector_mask, 1359 &segment->cow_offset, bitmap, bitmap_offset, 1360 segment->bitmap_words, bitmap_len); 1361 } 1362 } 1363 1364 static void ubd_map_req(struct ubd *dev, struct io_thread_req *io_req, 1365 struct request *req) 1366 { 1367 struct bio_vec bvec; 1368 struct req_iterator iter; 1369 int i = 0; 1370 unsigned long byte_offset = io_req->offset; 1371 int op = req_op(req); 1372 1373 if (op == REQ_OP_WRITE_ZEROES || op == REQ_OP_DISCARD) { 1374 io_req->io_desc[0].buffer = NULL; 1375 io_req->io_desc[0].length = blk_rq_bytes(req); 1376 } else { 1377 rq_for_each_segment(bvec, req, iter) { 1378 BUG_ON(i >= io_req->desc_cnt); 1379 1380 io_req->io_desc[i].buffer = 1381 page_address(bvec.bv_page) + bvec.bv_offset; 1382 io_req->io_desc[i].length = bvec.bv_len; 1383 i++; 1384 } 1385 } 1386 1387 if (dev->cow.file) { 1388 for (i = 0; i < io_req->desc_cnt; i++) { 1389 cowify_req(io_req, &io_req->io_desc[i], byte_offset, 1390 dev->cow.bitmap, dev->cow.bitmap_offset, 1391 dev->cow.bitmap_len); 1392 byte_offset += io_req->io_desc[i].length; 1393 } 1394 1395 } 1396 } 1397 1398 static struct io_thread_req *ubd_alloc_req(struct ubd *dev, struct request *req, 1399 int desc_cnt) 1400 { 1401 struct io_thread_req *io_req; 1402 int i; 1403 1404 io_req = kmalloc(sizeof(*io_req) + 1405 (desc_cnt * sizeof(struct io_desc)), 1406 GFP_ATOMIC); 1407 if (!io_req) 1408 return NULL; 1409 1410 io_req->req = req; 1411 if (dev->cow.file) 1412 io_req->fds[0] = dev->cow.fd; 1413 else 1414 io_req->fds[0] = dev->fd; 1415 io_req->error = 0; 1416 io_req->sectorsize = SECTOR_SIZE; 1417 io_req->fds[1] = dev->fd; 1418 io_req->offset = (u64) blk_rq_pos(req) << SECTOR_SHIFT; 1419 io_req->offsets[0] = 0; 1420 io_req->offsets[1] = dev->cow.data_offset; 1421 1422 for (i = 0 ; i < desc_cnt; i++) { 1423 io_req->io_desc[i].sector_mask = 0; 1424 io_req->io_desc[i].cow_offset = -1; 1425 } 1426 1427 return io_req; 1428 } 1429 1430 static int ubd_submit_request(struct ubd *dev, struct request *req) 1431 { 1432 int segs = 0; 1433 struct io_thread_req *io_req; 1434 int ret; 1435 int op = req_op(req); 1436 1437 if (op == REQ_OP_FLUSH) 1438 segs = 0; 1439 else if (op == REQ_OP_WRITE_ZEROES || op == REQ_OP_DISCARD) 1440 segs = 1; 1441 else 1442 segs = blk_rq_nr_phys_segments(req); 1443 1444 io_req = ubd_alloc_req(dev, req, segs); 1445 if (!io_req) 1446 return -ENOMEM; 1447 1448 io_req->desc_cnt = segs; 1449 if (segs) 1450 ubd_map_req(dev, io_req, req); 1451 1452 ret = os_write_file(thread_fd, &io_req, sizeof(io_req)); 1453 if (ret != sizeof(io_req)) { 1454 if (ret != -EAGAIN) 1455 pr_err("write to io thread failed: %d\n", -ret); 1456 kfree(io_req); 1457 } 1458 return ret; 1459 } 1460 1461 static blk_status_t ubd_queue_rq(struct blk_mq_hw_ctx *hctx, 1462 const struct blk_mq_queue_data *bd) 1463 { 1464 struct ubd *ubd_dev = hctx->queue->queuedata; 1465 struct request *req = bd->rq; 1466 int ret = 0, res = BLK_STS_OK; 1467 1468 blk_mq_start_request(req); 1469 1470 spin_lock_irq(&ubd_dev->lock); 1471 1472 switch (req_op(req)) { 1473 case REQ_OP_FLUSH: 1474 case REQ_OP_READ: 1475 case REQ_OP_WRITE: 1476 case REQ_OP_DISCARD: 1477 case REQ_OP_WRITE_ZEROES: 1478 ret = ubd_submit_request(ubd_dev, req); 1479 break; 1480 default: 1481 WARN_ON_ONCE(1); 1482 res = BLK_STS_NOTSUPP; 1483 } 1484 1485 spin_unlock_irq(&ubd_dev->lock); 1486 1487 if (ret < 0) { 1488 if (ret == -ENOMEM) 1489 res = BLK_STS_RESOURCE; 1490 else 1491 res = BLK_STS_DEV_RESOURCE; 1492 } 1493 1494 return res; 1495 } 1496 1497 static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo) 1498 { 1499 struct ubd *ubd_dev = bdev->bd_disk->private_data; 1500 1501 geo->heads = 128; 1502 geo->sectors = 32; 1503 geo->cylinders = ubd_dev->size / (128 * 32 * 512); 1504 return 0; 1505 } 1506 1507 static int ubd_ioctl(struct block_device *bdev, fmode_t mode, 1508 unsigned int cmd, unsigned long arg) 1509 { 1510 struct ubd *ubd_dev = bdev->bd_disk->private_data; 1511 u16 ubd_id[ATA_ID_WORDS]; 1512 1513 switch (cmd) { 1514 struct cdrom_volctrl volume; 1515 case HDIO_GET_IDENTITY: 1516 memset(&ubd_id, 0, ATA_ID_WORDS * 2); 1517 ubd_id[ATA_ID_CYLS] = ubd_dev->size / (128 * 32 * 512); 1518 ubd_id[ATA_ID_HEADS] = 128; 1519 ubd_id[ATA_ID_SECTORS] = 32; 1520 if(copy_to_user((char __user *) arg, (char *) &ubd_id, 1521 sizeof(ubd_id))) 1522 return -EFAULT; 1523 return 0; 1524 1525 case CDROMVOLREAD: 1526 if(copy_from_user(&volume, (char __user *) arg, sizeof(volume))) 1527 return -EFAULT; 1528 volume.channel0 = 255; 1529 volume.channel1 = 255; 1530 volume.channel2 = 255; 1531 volume.channel3 = 255; 1532 if(copy_to_user((char __user *) arg, &volume, sizeof(volume))) 1533 return -EFAULT; 1534 return 0; 1535 } 1536 return -EINVAL; 1537 } 1538 1539 static int map_error(int error_code) 1540 { 1541 switch (error_code) { 1542 case 0: 1543 return BLK_STS_OK; 1544 case ENOSYS: 1545 case EOPNOTSUPP: 1546 return BLK_STS_NOTSUPP; 1547 case ENOSPC: 1548 return BLK_STS_NOSPC; 1549 } 1550 return BLK_STS_IOERR; 1551 } 1552 1553 /* 1554 * Everything from here onwards *IS NOT PART OF THE KERNEL* 1555 * 1556 * The following functions are part of UML hypervisor code. 1557 * All functions from here onwards are executed as a helper 1558 * thread and are not allowed to execute any kernel functions. 1559 * 1560 * Any communication must occur strictly via shared memory and IPC. 1561 * 1562 * Do not add printks, locks, kernel memory operations, etc - it 1563 * will result in unpredictable behaviour and/or crashes. 1564 */ 1565 1566 static int update_bitmap(struct io_thread_req *req, struct io_desc *segment) 1567 { 1568 int n; 1569 1570 if (segment->cow_offset == -1) 1571 return map_error(0); 1572 1573 n = os_pwrite_file(req->fds[1], &segment->bitmap_words, 1574 sizeof(segment->bitmap_words), segment->cow_offset); 1575 if (n != sizeof(segment->bitmap_words)) 1576 return map_error(-n); 1577 1578 return map_error(0); 1579 } 1580 1581 static void do_io(struct io_thread_req *req, struct io_desc *desc) 1582 { 1583 char *buf = NULL; 1584 unsigned long len; 1585 int n, nsectors, start, end, bit; 1586 __u64 off; 1587 1588 /* FLUSH is really a special case, we cannot "case" it with others */ 1589 1590 if (req_op(req->req) == REQ_OP_FLUSH) { 1591 /* fds[0] is always either the rw image or our cow file */ 1592 req->error = map_error(-os_sync_file(req->fds[0])); 1593 return; 1594 } 1595 1596 nsectors = desc->length / req->sectorsize; 1597 start = 0; 1598 do { 1599 bit = ubd_test_bit(start, (unsigned char *) &desc->sector_mask); 1600 end = start; 1601 while((end < nsectors) && 1602 (ubd_test_bit(end, (unsigned char *) &desc->sector_mask) == bit)) 1603 end++; 1604 1605 off = req->offset + req->offsets[bit] + 1606 start * req->sectorsize; 1607 len = (end - start) * req->sectorsize; 1608 if (desc->buffer != NULL) 1609 buf = &desc->buffer[start * req->sectorsize]; 1610 1611 switch (req_op(req->req)) { 1612 case REQ_OP_READ: 1613 n = 0; 1614 do { 1615 buf = &buf[n]; 1616 len -= n; 1617 n = os_pread_file(req->fds[bit], buf, len, off); 1618 if (n < 0) { 1619 req->error = map_error(-n); 1620 return; 1621 } 1622 } while((n < len) && (n != 0)); 1623 if (n < len) memset(&buf[n], 0, len - n); 1624 break; 1625 case REQ_OP_WRITE: 1626 n = os_pwrite_file(req->fds[bit], buf, len, off); 1627 if(n != len){ 1628 req->error = map_error(-n); 1629 return; 1630 } 1631 break; 1632 case REQ_OP_DISCARD: 1633 case REQ_OP_WRITE_ZEROES: 1634 n = os_falloc_punch(req->fds[bit], off, len); 1635 if (n) { 1636 req->error = map_error(-n); 1637 return; 1638 } 1639 break; 1640 default: 1641 WARN_ON_ONCE(1); 1642 req->error = BLK_STS_NOTSUPP; 1643 return; 1644 } 1645 1646 start = end; 1647 } while(start < nsectors); 1648 1649 req->offset += len; 1650 req->error = update_bitmap(req, desc); 1651 } 1652 1653 /* Changed in start_io_thread, which is serialized by being called only 1654 * from ubd_init, which is an initcall. 1655 */ 1656 int kernel_fd = -1; 1657 1658 /* Only changed by the io thread. XXX: currently unused. */ 1659 static int io_count = 0; 1660 1661 int io_thread(void *arg) 1662 { 1663 int n, count, written, res; 1664 1665 os_fix_helper_signals(); 1666 1667 while(1){ 1668 n = bulk_req_safe_read( 1669 kernel_fd, 1670 io_req_buffer, 1671 &io_remainder, 1672 &io_remainder_size, 1673 UBD_REQ_BUFFER_SIZE 1674 ); 1675 if (n <= 0) { 1676 if (n == -EAGAIN) 1677 ubd_read_poll(-1); 1678 1679 continue; 1680 } 1681 1682 for (count = 0; count < n/sizeof(struct io_thread_req *); count++) { 1683 struct io_thread_req *req = (*io_req_buffer)[count]; 1684 int i; 1685 1686 io_count++; 1687 for (i = 0; !req->error && i < req->desc_cnt; i++) 1688 do_io(req, &(req->io_desc[i])); 1689 1690 } 1691 1692 written = 0; 1693 1694 do { 1695 res = os_write_file(kernel_fd, 1696 ((char *) io_req_buffer) + written, 1697 n - written); 1698 if (res >= 0) { 1699 written += res; 1700 } 1701 if (written < n) { 1702 ubd_write_poll(-1); 1703 } 1704 } while (written < n); 1705 } 1706 1707 return 0; 1708 } 1709