ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / arch / um / drivers / ubd_kern.c
blob0491e40d6968d9916f4dfaef93bd28d7c694145d
1 /*
2 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
6 /* 2001-09-28...2002-04-17
7 * Partition stuff by James_McMechan@hotmail.com
8 * old style ubd by setting UBD_SHIFT to 0
9 * 2002-09-27...2002-10-18 massive tinkering for 2.5
10 * partitions have changed in 2.5
11 * 2003-01-29 more tinkering for 2.5.59-1
12 * This should now address the sysfs problems and has
13 * the symlink for devfs to allow for booting with
14 * the common /dev/ubd/discX/... names rather than
15 * only /dev/ubdN/discN this version also has lots of
16 * clean ups preparing for ubd-many.
17 * James McMechan
20 #define UBD_SHIFT 4
22 #include "linux/kernel.h"
23 #include "linux/module.h"
24 #include "linux/blkdev.h"
25 #include "linux/ata.h"
26 #include "linux/hdreg.h"
27 #include "linux/init.h"
28 #include "linux/cdrom.h"
29 #include "linux/proc_fs.h"
30 #include "linux/seq_file.h"
31 #include "linux/ctype.h"
32 #include "linux/capability.h"
33 #include "linux/mm.h"
34 #include "linux/slab.h"
35 #include "linux/vmalloc.h"
36 #include "linux/mutex.h"
37 #include "linux/blkpg.h"
38 #include "linux/genhd.h"
39 #include "linux/spinlock.h"
40 #include "linux/platform_device.h"
41 #include "linux/scatterlist.h"
42 #include "asm/segment.h"
43 #include "asm/uaccess.h"
44 #include "asm/irq.h"
45 #include "asm/types.h"
46 #include "asm/tlbflush.h"
47 #include "mem_user.h"
48 #include "kern_util.h"
49 #include "kern.h"
50 #include "mconsole_kern.h"
51 #include "init.h"
52 #include "irq_user.h"
53 #include "irq_kern.h"
54 #include "ubd_user.h"
55 #include "os.h"
56 #include "mem.h"
57 #include "mem_kern.h"
58 #include "cow.h"
60 enum ubd_req { UBD_READ, UBD_WRITE };
62 struct io_thread_req {
63 struct request *req;
64 enum ubd_req op;
65 int fds[2];
66 unsigned long offsets[2];
67 unsigned long long offset;
68 unsigned long length;
69 char *buffer;
70 int sectorsize;
71 unsigned long sector_mask;
72 unsigned long long cow_offset;
73 unsigned long bitmap_words[2];
74 int error;
77 static inline int ubd_test_bit(__u64 bit, unsigned char *data)
79 __u64 n;
80 int bits, off;
82 bits = sizeof(data[0]) * 8;
83 n = bit / bits;
84 off = bit % bits;
85 return (data[n] & (1 << off)) != 0;
88 static inline void ubd_set_bit(__u64 bit, unsigned char *data)
90 __u64 n;
91 int bits, off;
93 bits = sizeof(data[0]) * 8;
94 n = bit / bits;
95 off = bit % bits;
96 data[n] |= (1 << off);
98 /*End stuff from ubd_user.h*/
100 #define DRIVER_NAME "uml-blkdev"
102 static DEFINE_MUTEX(ubd_lock);
103 static DEFINE_MUTEX(ubd_mutex); /* replaces BKL, might not be needed */
105 static int ubd_open(struct block_device *bdev, fmode_t mode);
106 static int ubd_release(struct gendisk *disk, fmode_t mode);
107 static int ubd_ioctl(struct block_device *bdev, fmode_t mode,
108 unsigned int cmd, unsigned long arg);
109 static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo);
111 #define MAX_DEV (16)
113 static const struct block_device_operations ubd_blops = {
114 .owner = THIS_MODULE,
115 .open = ubd_open,
116 .release = ubd_release,
117 .ioctl = ubd_ioctl,
118 .getgeo = ubd_getgeo,
121 /* Protected by ubd_lock */
122 static int fake_major = UBD_MAJOR;
123 static struct gendisk *ubd_gendisk[MAX_DEV];
124 static struct gendisk *fake_gendisk[MAX_DEV];
126 #ifdef CONFIG_BLK_DEV_UBD_SYNC
127 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
128 .cl = 1 })
129 #else
130 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
131 .cl = 1 })
132 #endif
133 static struct openflags global_openflags = OPEN_FLAGS;
135 struct cow {
136 /* backing file name */
137 char *file;
138 /* backing file fd */
139 int fd;
140 unsigned long *bitmap;
141 unsigned long bitmap_len;
142 int bitmap_offset;
143 int data_offset;
146 #define MAX_SG 64
148 struct ubd {
149 struct list_head restart;
150 /* name (and fd, below) of the file opened for writing, either the
151 * backing or the cow file. */
152 char *file;
153 int count;
154 int fd;
155 __u64 size;
156 struct openflags boot_openflags;
157 struct openflags openflags;
158 unsigned shared:1;
159 unsigned no_cow:1;
160 struct cow cow;
161 struct platform_device pdev;
162 struct request_queue *queue;
163 spinlock_t lock;
164 struct scatterlist sg[MAX_SG];
165 struct request *request;
166 int start_sg, end_sg;
167 sector_t rq_pos;
170 #define DEFAULT_COW { \
171 .file = NULL, \
172 .fd = -1, \
173 .bitmap = NULL, \
174 .bitmap_offset = 0, \
175 .data_offset = 0, \
178 #define DEFAULT_UBD { \
179 .file = NULL, \
180 .count = 0, \
181 .fd = -1, \
182 .size = -1, \
183 .boot_openflags = OPEN_FLAGS, \
184 .openflags = OPEN_FLAGS, \
185 .no_cow = 0, \
186 .shared = 0, \
187 .cow = DEFAULT_COW, \
188 .lock = __SPIN_LOCK_UNLOCKED(ubd_devs.lock), \
189 .request = NULL, \
190 .start_sg = 0, \
191 .end_sg = 0, \
192 .rq_pos = 0, \
195 /* Protected by ubd_lock */
196 static struct ubd ubd_devs[MAX_DEV] = { [0 ... MAX_DEV - 1] = DEFAULT_UBD };
198 /* Only changed by fake_ide_setup which is a setup */
199 static int fake_ide = 0;
200 static struct proc_dir_entry *proc_ide_root = NULL;
201 static struct proc_dir_entry *proc_ide = NULL;
203 static void make_proc_ide(void)
205 proc_ide_root = proc_mkdir("ide", NULL);
206 proc_ide = proc_mkdir("ide0", proc_ide_root);
209 static int fake_ide_media_proc_show(struct seq_file *m, void *v)
211 seq_puts(m, "disk\n");
212 return 0;
215 static int fake_ide_media_proc_open(struct inode *inode, struct file *file)
217 return single_open(file, fake_ide_media_proc_show, NULL);
220 static const struct file_operations fake_ide_media_proc_fops = {
221 .owner = THIS_MODULE,
222 .open = fake_ide_media_proc_open,
223 .read = seq_read,
224 .llseek = seq_lseek,
225 .release = single_release,
228 static void make_ide_entries(const char *dev_name)
230 struct proc_dir_entry *dir, *ent;
231 char name[64];
233 if(proc_ide_root == NULL) make_proc_ide();
235 dir = proc_mkdir(dev_name, proc_ide);
236 if(!dir) return;
238 ent = proc_create("media", S_IRUGO, dir, &fake_ide_media_proc_fops);
239 if(!ent) return;
240 snprintf(name, sizeof(name), "ide0/%s", dev_name);
241 proc_symlink(dev_name, proc_ide_root, name);
244 static int fake_ide_setup(char *str)
246 fake_ide = 1;
247 return 1;
250 __setup("fake_ide", fake_ide_setup);
252 __uml_help(fake_ide_setup,
253 "fake_ide\n"
254 " Create ide0 entries that map onto ubd devices.\n\n"
257 static int parse_unit(char **ptr)
259 char *str = *ptr, *end;
260 int n = -1;
262 if(isdigit(*str)) {
263 n = simple_strtoul(str, &end, 0);
264 if(end == str)
265 return -1;
266 *ptr = end;
268 else if (('a' <= *str) && (*str <= 'z')) {
269 n = *str - 'a';
270 str++;
271 *ptr = str;
273 return n;
276 /* If *index_out == -1 at exit, the passed option was a general one;
277 * otherwise, the str pointer is used (and owned) inside ubd_devs array, so it
278 * should not be freed on exit.
280 static int ubd_setup_common(char *str, int *index_out, char **error_out)
282 struct ubd *ubd_dev;
283 struct openflags flags = global_openflags;
284 char *backing_file;
285 int n, err = 0, i;
287 if(index_out) *index_out = -1;
288 n = *str;
289 if(n == '='){
290 char *end;
291 int major;
293 str++;
294 if(!strcmp(str, "sync")){
295 global_openflags = of_sync(global_openflags);
296 goto out1;
299 err = -EINVAL;
300 major = simple_strtoul(str, &end, 0);
301 if((*end != '\0') || (end == str)){
302 *error_out = "Didn't parse major number";
303 goto out1;
306 mutex_lock(&ubd_lock);
307 if (fake_major != UBD_MAJOR) {
308 *error_out = "Can't assign a fake major twice";
309 goto out1;
312 fake_major = major;
314 printk(KERN_INFO "Setting extra ubd major number to %d\n",
315 major);
316 err = 0;
317 out1:
318 mutex_unlock(&ubd_lock);
319 return err;
322 n = parse_unit(&str);
323 if(n < 0){
324 *error_out = "Couldn't parse device number";
325 return -EINVAL;
327 if(n >= MAX_DEV){
328 *error_out = "Device number out of range";
329 return 1;
332 err = -EBUSY;
333 mutex_lock(&ubd_lock);
335 ubd_dev = &ubd_devs[n];
336 if(ubd_dev->file != NULL){
337 *error_out = "Device is already configured";
338 goto out;
341 if (index_out)
342 *index_out = n;
344 err = -EINVAL;
345 for (i = 0; i < sizeof("rscd="); i++) {
346 switch (*str) {
347 case 'r':
348 flags.w = 0;
349 break;
350 case 's':
351 flags.s = 1;
352 break;
353 case 'd':
354 ubd_dev->no_cow = 1;
355 break;
356 case 'c':
357 ubd_dev->shared = 1;
358 break;
359 case '=':
360 str++;
361 goto break_loop;
362 default:
363 *error_out = "Expected '=' or flag letter "
364 "(r, s, c, or d)";
365 goto out;
367 str++;
370 if (*str == '=')
371 *error_out = "Too many flags specified";
372 else
373 *error_out = "Missing '='";
374 goto out;
376 break_loop:
377 backing_file = strchr(str, ',');
379 if (backing_file == NULL)
380 backing_file = strchr(str, ':');
382 if(backing_file != NULL){
383 if(ubd_dev->no_cow){
384 *error_out = "Can't specify both 'd' and a cow file";
385 goto out;
387 else {
388 *backing_file = '\0';
389 backing_file++;
392 err = 0;
393 ubd_dev->file = str;
394 ubd_dev->cow.file = backing_file;
395 ubd_dev->boot_openflags = flags;
396 out:
397 mutex_unlock(&ubd_lock);
398 return err;
401 static int ubd_setup(char *str)
403 char *error;
404 int err;
406 err = ubd_setup_common(str, NULL, &error);
407 if(err)
408 printk(KERN_ERR "Failed to initialize device with \"%s\" : "
409 "%s\n", str, error);
410 return 1;
413 __setup("ubd", ubd_setup);
414 __uml_help(ubd_setup,
415 "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
416 " This is used to associate a device with a file in the underlying\n"
417 " filesystem. When specifying two filenames, the first one is the\n"
418 " COW name and the second is the backing file name. As separator you can\n"
419 " use either a ':' or a ',': the first one allows writing things like;\n"
420 " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
421 " while with a ',' the shell would not expand the 2nd '~'.\n"
422 " When using only one filename, UML will detect whether to treat it like\n"
423 " a COW file or a backing file. To override this detection, add the 'd'\n"
424 " flag:\n"
425 " ubd0d=BackingFile\n"
426 " Usually, there is a filesystem in the file, but \n"
427 " that's not required. Swap devices containing swap files can be\n"
428 " specified like this. Also, a file which doesn't contain a\n"
429 " filesystem can have its contents read in the virtual \n"
430 " machine by running 'dd' on the device. <n> must be in the range\n"
431 " 0 to 7. Appending an 'r' to the number will cause that device\n"
432 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
433 " an 's' will cause data to be written to disk on the host immediately.\n"
434 " 'c' will cause the device to be treated as being shared between multiple\n"
435 " UMLs and file locking will be turned off - this is appropriate for a\n"
436 " cluster filesystem and inappropriate at almost all other times.\n\n"
439 static int udb_setup(char *str)
441 printk("udb%s specified on command line is almost certainly a ubd -> "
442 "udb TYPO\n", str);
443 return 1;
446 __setup("udb", udb_setup);
447 __uml_help(udb_setup,
448 "udb\n"
449 " This option is here solely to catch ubd -> udb typos, which can be\n"
450 " to impossible to catch visually unless you specifically look for\n"
451 " them. The only result of any option starting with 'udb' is an error\n"
452 " in the boot output.\n\n"
455 static void do_ubd_request(struct request_queue * q);
457 /* Only changed by ubd_init, which is an initcall. */
458 static int thread_fd = -1;
459 static LIST_HEAD(restart);
461 /* XXX - move this inside ubd_intr. */
462 /* Called without dev->lock held, and only in interrupt context. */
463 static void ubd_handler(void)
465 struct io_thread_req *req;
466 struct ubd *ubd;
467 struct list_head *list, *next_ele;
468 unsigned long flags;
469 int n;
471 while(1){
472 n = os_read_file(thread_fd, &req,
473 sizeof(struct io_thread_req *));
474 if(n != sizeof(req)){
475 if(n == -EAGAIN)
476 break;
477 printk(KERN_ERR "spurious interrupt in ubd_handler, "
478 "err = %d\n", -n);
479 return;
482 blk_end_request(req->req, 0, req->length);
483 kfree(req);
485 reactivate_fd(thread_fd, UBD_IRQ);
487 list_for_each_safe(list, next_ele, &restart){
488 ubd = container_of(list, struct ubd, restart);
489 list_del_init(&ubd->restart);
490 spin_lock_irqsave(&ubd->lock, flags);
491 do_ubd_request(ubd->queue);
492 spin_unlock_irqrestore(&ubd->lock, flags);
496 static irqreturn_t ubd_intr(int irq, void *dev)
498 ubd_handler();
499 return IRQ_HANDLED;
502 /* Only changed by ubd_init, which is an initcall. */
503 static int io_pid = -1;
505 static void kill_io_thread(void)
507 if(io_pid != -1)
508 os_kill_process(io_pid, 1);
511 __uml_exitcall(kill_io_thread);
513 static inline int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out)
515 char *file;
516 int fd;
517 int err;
519 __u32 version;
520 __u32 align;
521 char *backing_file;
522 time_t mtime;
523 unsigned long long size;
524 int sector_size;
525 int bitmap_offset;
527 if (ubd_dev->file && ubd_dev->cow.file) {
528 file = ubd_dev->cow.file;
530 goto out;
533 fd = os_open_file(ubd_dev->file, global_openflags, 0);
534 if (fd < 0)
535 return fd;
537 err = read_cow_header(file_reader, &fd, &version, &backing_file, \
538 &mtime, &size, &sector_size, &align, &bitmap_offset);
539 os_close_file(fd);
541 if(err == -EINVAL)
542 file = ubd_dev->file;
543 else
544 file = backing_file;
546 out:
547 return os_file_size(file, size_out);
550 static int read_cow_bitmap(int fd, void *buf, int offset, int len)
552 int err;
554 err = os_seek_file(fd, offset);
555 if (err < 0)
556 return err;
558 err = os_read_file(fd, buf, len);
559 if (err < 0)
560 return err;
562 return 0;
565 static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
567 unsigned long modtime;
568 unsigned long long actual;
569 int err;
571 err = os_file_modtime(file, &modtime);
572 if (err < 0) {
573 printk(KERN_ERR "Failed to get modification time of backing "
574 "file \"%s\", err = %d\n", file, -err);
575 return err;
578 err = os_file_size(file, &actual);
579 if (err < 0) {
580 printk(KERN_ERR "Failed to get size of backing file \"%s\", "
581 "err = %d\n", file, -err);
582 return err;
585 if (actual != size) {
586 /*__u64 can be a long on AMD64 and with %lu GCC complains; so
587 * the typecast.*/
588 printk(KERN_ERR "Size mismatch (%llu vs %llu) of COW header "
589 "vs backing file\n", (unsigned long long) size, actual);
590 return -EINVAL;
592 if (modtime != mtime) {
593 printk(KERN_ERR "mtime mismatch (%ld vs %ld) of COW header vs "
594 "backing file\n", mtime, modtime);
595 return -EINVAL;
597 return 0;
600 static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
602 struct uml_stat buf1, buf2;
603 int err;
605 if (from_cmdline == NULL)
606 return 0;
607 if (!strcmp(from_cmdline, from_cow))
608 return 0;
610 err = os_stat_file(from_cmdline, &buf1);
611 if (err < 0) {
612 printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cmdline,
613 -err);
614 return 0;
616 err = os_stat_file(from_cow, &buf2);
617 if (err < 0) {
618 printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cow,
619 -err);
620 return 1;
622 if ((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
623 return 0;
625 printk(KERN_ERR "Backing file mismatch - \"%s\" requested, "
626 "\"%s\" specified in COW header of \"%s\"\n",
627 from_cmdline, from_cow, cow);
628 return 1;
631 static int open_ubd_file(char *file, struct openflags *openflags, int shared,
632 char **backing_file_out, int *bitmap_offset_out,
633 unsigned long *bitmap_len_out, int *data_offset_out,
634 int *create_cow_out)
636 time_t mtime;
637 unsigned long long size;
638 __u32 version, align;
639 char *backing_file;
640 int fd, err, sectorsize, asked_switch, mode = 0644;
642 fd = os_open_file(file, *openflags, mode);
643 if (fd < 0) {
644 if ((fd == -ENOENT) && (create_cow_out != NULL))
645 *create_cow_out = 1;
646 if (!openflags->w ||
647 ((fd != -EROFS) && (fd != -EACCES)))
648 return fd;
649 openflags->w = 0;
650 fd = os_open_file(file, *openflags, mode);
651 if (fd < 0)
652 return fd;
655 if (shared)
656 printk(KERN_INFO "Not locking \"%s\" on the host\n", file);
657 else {
658 err = os_lock_file(fd, openflags->w);
659 if (err < 0) {
660 printk(KERN_ERR "Failed to lock '%s', err = %d\n",
661 file, -err);
662 goto out_close;
666 /* Successful return case! */
667 if (backing_file_out == NULL)
668 return fd;
670 err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
671 &size, &sectorsize, &align, bitmap_offset_out);
672 if (err && (*backing_file_out != NULL)) {
673 printk(KERN_ERR "Failed to read COW header from COW file "
674 "\"%s\", errno = %d\n", file, -err);
675 goto out_close;
677 if (err)
678 return fd;
680 asked_switch = path_requires_switch(*backing_file_out, backing_file,
681 file);
683 /* Allow switching only if no mismatch. */
684 if (asked_switch && !backing_file_mismatch(*backing_file_out, size,
685 mtime)) {
686 printk(KERN_ERR "Switching backing file to '%s'\n",
687 *backing_file_out);
688 err = write_cow_header(file, fd, *backing_file_out,
689 sectorsize, align, &size);
690 if (err) {
691 printk(KERN_ERR "Switch failed, errno = %d\n", -err);
692 goto out_close;
694 } else {
695 *backing_file_out = backing_file;
696 err = backing_file_mismatch(*backing_file_out, size, mtime);
697 if (err)
698 goto out_close;
701 cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
702 bitmap_len_out, data_offset_out);
704 return fd;
705 out_close:
706 os_close_file(fd);
707 return err;
710 static int create_cow_file(char *cow_file, char *backing_file,
711 struct openflags flags,
712 int sectorsize, int alignment, int *bitmap_offset_out,
713 unsigned long *bitmap_len_out, int *data_offset_out)
715 int err, fd;
717 flags.c = 1;
718 fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL);
719 if (fd < 0) {
720 err = fd;
721 printk(KERN_ERR "Open of COW file '%s' failed, errno = %d\n",
722 cow_file, -err);
723 goto out;
726 err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
727 bitmap_offset_out, bitmap_len_out,
728 data_offset_out);
729 if (!err)
730 return fd;
731 os_close_file(fd);
732 out:
733 return err;
736 static void ubd_close_dev(struct ubd *ubd_dev)
738 os_close_file(ubd_dev->fd);
739 if(ubd_dev->cow.file == NULL)
740 return;
742 os_close_file(ubd_dev->cow.fd);
743 vfree(ubd_dev->cow.bitmap);
744 ubd_dev->cow.bitmap = NULL;
747 static int ubd_open_dev(struct ubd *ubd_dev)
749 struct openflags flags;
750 char **back_ptr;
751 int err, create_cow, *create_ptr;
752 int fd;
754 ubd_dev->openflags = ubd_dev->boot_openflags;
755 create_cow = 0;
756 create_ptr = (ubd_dev->cow.file != NULL) ? &create_cow : NULL;
757 back_ptr = ubd_dev->no_cow ? NULL : &ubd_dev->cow.file;
759 fd = open_ubd_file(ubd_dev->file, &ubd_dev->openflags, ubd_dev->shared,
760 back_ptr, &ubd_dev->cow.bitmap_offset,
761 &ubd_dev->cow.bitmap_len, &ubd_dev->cow.data_offset,
762 create_ptr);
764 if((fd == -ENOENT) && create_cow){
765 fd = create_cow_file(ubd_dev->file, ubd_dev->cow.file,
766 ubd_dev->openflags, 1 << 9, PAGE_SIZE,
767 &ubd_dev->cow.bitmap_offset,
768 &ubd_dev->cow.bitmap_len,
769 &ubd_dev->cow.data_offset);
770 if(fd >= 0){
771 printk(KERN_INFO "Creating \"%s\" as COW file for "
772 "\"%s\"\n", ubd_dev->file, ubd_dev->cow.file);
776 if(fd < 0){
777 printk("Failed to open '%s', errno = %d\n", ubd_dev->file,
778 -fd);
779 return fd;
781 ubd_dev->fd = fd;
783 if(ubd_dev->cow.file != NULL){
784 blk_queue_max_hw_sectors(ubd_dev->queue, 8 * sizeof(long));
786 err = -ENOMEM;
787 ubd_dev->cow.bitmap = vmalloc(ubd_dev->cow.bitmap_len);
788 if(ubd_dev->cow.bitmap == NULL){
789 printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
790 goto error;
792 flush_tlb_kernel_vm();
794 err = read_cow_bitmap(ubd_dev->fd, ubd_dev->cow.bitmap,
795 ubd_dev->cow.bitmap_offset,
796 ubd_dev->cow.bitmap_len);
797 if(err < 0)
798 goto error;
800 flags = ubd_dev->openflags;
801 flags.w = 0;
802 err = open_ubd_file(ubd_dev->cow.file, &flags, ubd_dev->shared, NULL,
803 NULL, NULL, NULL, NULL);
804 if(err < 0) goto error;
805 ubd_dev->cow.fd = err;
807 return 0;
808 error:
809 os_close_file(ubd_dev->fd);
810 return err;
813 static void ubd_device_release(struct device *dev)
815 struct ubd *ubd_dev = dev_get_drvdata(dev);
817 blk_cleanup_queue(ubd_dev->queue);
818 *ubd_dev = ((struct ubd) DEFAULT_UBD);
821 static int ubd_disk_register(int major, u64 size, int unit,
822 struct gendisk **disk_out)
824 struct gendisk *disk;
826 disk = alloc_disk(1 << UBD_SHIFT);
827 if(disk == NULL)
828 return -ENOMEM;
830 disk->major = major;
831 disk->first_minor = unit << UBD_SHIFT;
832 disk->fops = &ubd_blops;
833 set_capacity(disk, size / 512);
834 if (major == UBD_MAJOR)
835 sprintf(disk->disk_name, "ubd%c", 'a' + unit);
836 else
837 sprintf(disk->disk_name, "ubd_fake%d", unit);
839 /* sysfs register (not for ide fake devices) */
840 if (major == UBD_MAJOR) {
841 ubd_devs[unit].pdev.id = unit;
842 ubd_devs[unit].pdev.name = DRIVER_NAME;
843 ubd_devs[unit].pdev.dev.release = ubd_device_release;
844 dev_set_drvdata(&ubd_devs[unit].pdev.dev, &ubd_devs[unit]);
845 platform_device_register(&ubd_devs[unit].pdev);
846 disk->driverfs_dev = &ubd_devs[unit].pdev.dev;
849 disk->private_data = &ubd_devs[unit];
850 disk->queue = ubd_devs[unit].queue;
851 add_disk(disk);
853 *disk_out = disk;
854 return 0;
857 #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
859 static int ubd_add(int n, char **error_out)
861 struct ubd *ubd_dev = &ubd_devs[n];
862 int err = 0;
864 if(ubd_dev->file == NULL)
865 goto out;
867 err = ubd_file_size(ubd_dev, &ubd_dev->size);
868 if(err < 0){
869 *error_out = "Couldn't determine size of device's file";
870 goto out;
873 ubd_dev->size = ROUND_BLOCK(ubd_dev->size);
875 INIT_LIST_HEAD(&ubd_dev->restart);
876 sg_init_table(ubd_dev->sg, MAX_SG);
878 err = -ENOMEM;
879 ubd_dev->queue = blk_init_queue(do_ubd_request, &ubd_dev->lock);
880 if (ubd_dev->queue == NULL) {
881 *error_out = "Failed to initialize device queue";
882 goto out;
884 ubd_dev->queue->queuedata = ubd_dev;
886 blk_queue_max_segments(ubd_dev->queue, MAX_SG);
887 err = ubd_disk_register(UBD_MAJOR, ubd_dev->size, n, &ubd_gendisk[n]);
888 if(err){
889 *error_out = "Failed to register device";
890 goto out_cleanup;
893 if (fake_major != UBD_MAJOR)
894 ubd_disk_register(fake_major, ubd_dev->size, n,
895 &fake_gendisk[n]);
898 * Perhaps this should also be under the "if (fake_major)" above
899 * using the fake_disk->disk_name
901 if (fake_ide)
902 make_ide_entries(ubd_gendisk[n]->disk_name);
904 err = 0;
905 out:
906 return err;
908 out_cleanup:
909 blk_cleanup_queue(ubd_dev->queue);
910 goto out;
913 static int ubd_config(char *str, char **error_out)
915 int n, ret;
917 /* This string is possibly broken up and stored, so it's only
918 * freed if ubd_setup_common fails, or if only general options
919 * were set.
921 str = kstrdup(str, GFP_KERNEL);
922 if (str == NULL) {
923 *error_out = "Failed to allocate memory";
924 return -ENOMEM;
927 ret = ubd_setup_common(str, &n, error_out);
928 if (ret)
929 goto err_free;
931 if (n == -1) {
932 ret = 0;
933 goto err_free;
936 mutex_lock(&ubd_lock);
937 ret = ubd_add(n, error_out);
938 if (ret)
939 ubd_devs[n].file = NULL;
940 mutex_unlock(&ubd_lock);
942 out:
943 return ret;
945 err_free:
946 kfree(str);
947 goto out;
950 static int ubd_get_config(char *name, char *str, int size, char **error_out)
952 struct ubd *ubd_dev;
953 int n, len = 0;
955 n = parse_unit(&name);
956 if((n >= MAX_DEV) || (n < 0)){
957 *error_out = "ubd_get_config : device number out of range";
958 return -1;
961 ubd_dev = &ubd_devs[n];
962 mutex_lock(&ubd_lock);
964 if(ubd_dev->file == NULL){
965 CONFIG_CHUNK(str, size, len, "", 1);
966 goto out;
969 CONFIG_CHUNK(str, size, len, ubd_dev->file, 0);
971 if(ubd_dev->cow.file != NULL){
972 CONFIG_CHUNK(str, size, len, ",", 0);
973 CONFIG_CHUNK(str, size, len, ubd_dev->cow.file, 1);
975 else CONFIG_CHUNK(str, size, len, "", 1);
977 out:
978 mutex_unlock(&ubd_lock);
979 return len;
982 static int ubd_id(char **str, int *start_out, int *end_out)
984 int n;
986 n = parse_unit(str);
987 *start_out = 0;
988 *end_out = MAX_DEV - 1;
989 return n;
992 static int ubd_remove(int n, char **error_out)
994 struct gendisk *disk = ubd_gendisk[n];
995 struct ubd *ubd_dev;
996 int err = -ENODEV;
998 mutex_lock(&ubd_lock);
1000 ubd_dev = &ubd_devs[n];
1002 if(ubd_dev->file == NULL)
1003 goto out;
1005 /* you cannot remove a open disk */
1006 err = -EBUSY;
1007 if(ubd_dev->count > 0)
1008 goto out;
1010 ubd_gendisk[n] = NULL;
1011 if(disk != NULL){
1012 del_gendisk(disk);
1013 put_disk(disk);
1016 if(fake_gendisk[n] != NULL){
1017 del_gendisk(fake_gendisk[n]);
1018 put_disk(fake_gendisk[n]);
1019 fake_gendisk[n] = NULL;
1022 err = 0;
1023 platform_device_unregister(&ubd_dev->pdev);
1024 out:
1025 mutex_unlock(&ubd_lock);
1026 return err;
1029 /* All these are called by mconsole in process context and without
1030 * ubd-specific locks. The structure itself is const except for .list.
1032 static struct mc_device ubd_mc = {
1033 .list = LIST_HEAD_INIT(ubd_mc.list),
1034 .name = "ubd",
1035 .config = ubd_config,
1036 .get_config = ubd_get_config,
1037 .id = ubd_id,
1038 .remove = ubd_remove,
1041 static int __init ubd_mc_init(void)
1043 mconsole_register_dev(&ubd_mc);
1044 return 0;
1047 __initcall(ubd_mc_init);
1049 static int __init ubd0_init(void)
1051 struct ubd *ubd_dev = &ubd_devs[0];
1053 mutex_lock(&ubd_lock);
1054 if(ubd_dev->file == NULL)
1055 ubd_dev->file = "root_fs";
1056 mutex_unlock(&ubd_lock);
1058 return 0;
1061 __initcall(ubd0_init);
1063 /* Used in ubd_init, which is an initcall */
1064 static struct platform_driver ubd_driver = {
1065 .driver = {
1066 .name = DRIVER_NAME,
1070 static int __init ubd_init(void)
1072 char *error;
1073 int i, err;
1075 if (register_blkdev(UBD_MAJOR, "ubd"))
1076 return -1;
1078 if (fake_major != UBD_MAJOR) {
1079 char name[sizeof("ubd_nnn\0")];
1081 snprintf(name, sizeof(name), "ubd_%d", fake_major);
1082 if (register_blkdev(fake_major, "ubd"))
1083 return -1;
1085 platform_driver_register(&ubd_driver);
1086 mutex_lock(&ubd_lock);
1087 for (i = 0; i < MAX_DEV; i++){
1088 err = ubd_add(i, &error);
1089 if(err)
1090 printk(KERN_ERR "Failed to initialize ubd device %d :"
1091 "%s\n", i, error);
1093 mutex_unlock(&ubd_lock);
1094 return 0;
1097 late_initcall(ubd_init);
1099 static int __init ubd_driver_init(void){
1100 unsigned long stack;
1101 int err;
1103 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
1104 if(global_openflags.s){
1105 printk(KERN_INFO "ubd: Synchronous mode\n");
1106 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
1107 * enough. So use anyway the io thread. */
1109 stack = alloc_stack(0, 0);
1110 io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
1111 &thread_fd);
1112 if(io_pid < 0){
1113 printk(KERN_ERR
1114 "ubd : Failed to start I/O thread (errno = %d) - "
1115 "falling back to synchronous I/O\n", -io_pid);
1116 io_pid = -1;
1117 return 0;
1119 err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
1120 IRQF_DISABLED, "ubd", ubd_devs);
1121 if(err != 0)
1122 printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
1123 return 0;
1126 device_initcall(ubd_driver_init);
1128 static int ubd_open(struct block_device *bdev, fmode_t mode)
1130 struct gendisk *disk = bdev->bd_disk;
1131 struct ubd *ubd_dev = disk->private_data;
1132 int err = 0;
1134 mutex_lock(&ubd_mutex);
1135 if(ubd_dev->count == 0){
1136 err = ubd_open_dev(ubd_dev);
1137 if(err){
1138 printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
1139 disk->disk_name, ubd_dev->file, -err);
1140 goto out;
1143 ubd_dev->count++;
1144 set_disk_ro(disk, !ubd_dev->openflags.w);
1146 /* This should no more be needed. And it didn't work anyway to exclude
1147 * read-write remounting of filesystems.*/
1148 /*if((mode & FMODE_WRITE) && !ubd_dev->openflags.w){
1149 if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
1150 err = -EROFS;
1152 out:
1153 mutex_unlock(&ubd_mutex);
1154 return err;
1157 static int ubd_release(struct gendisk *disk, fmode_t mode)
1159 struct ubd *ubd_dev = disk->private_data;
1161 mutex_lock(&ubd_mutex);
1162 if(--ubd_dev->count == 0)
1163 ubd_close_dev(ubd_dev);
1164 mutex_unlock(&ubd_mutex);
1165 return 0;
1168 static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
1169 __u64 *cow_offset, unsigned long *bitmap,
1170 __u64 bitmap_offset, unsigned long *bitmap_words,
1171 __u64 bitmap_len)
1173 __u64 sector = io_offset >> 9;
1174 int i, update_bitmap = 0;
1176 for(i = 0; i < length >> 9; i++){
1177 if(cow_mask != NULL)
1178 ubd_set_bit(i, (unsigned char *) cow_mask);
1179 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
1180 continue;
1182 update_bitmap = 1;
1183 ubd_set_bit(sector + i, (unsigned char *) bitmap);
1186 if(!update_bitmap)
1187 return;
1189 *cow_offset = sector / (sizeof(unsigned long) * 8);
1191 /* This takes care of the case where we're exactly at the end of the
1192 * device, and *cow_offset + 1 is off the end. So, just back it up
1193 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
1194 * for the original diagnosis.
1196 if (*cow_offset == (DIV_ROUND_UP(bitmap_len,
1197 sizeof(unsigned long)) - 1))
1198 (*cow_offset)--;
1200 bitmap_words[0] = bitmap[*cow_offset];
1201 bitmap_words[1] = bitmap[*cow_offset + 1];
1203 *cow_offset *= sizeof(unsigned long);
1204 *cow_offset += bitmap_offset;
1207 static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
1208 __u64 bitmap_offset, __u64 bitmap_len)
1210 __u64 sector = req->offset >> 9;
1211 int i;
1213 if(req->length > (sizeof(req->sector_mask) * 8) << 9)
1214 panic("Operation too long");
1216 if(req->op == UBD_READ) {
1217 for(i = 0; i < req->length >> 9; i++){
1218 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
1219 ubd_set_bit(i, (unsigned char *)
1220 &req->sector_mask);
1223 else cowify_bitmap(req->offset, req->length, &req->sector_mask,
1224 &req->cow_offset, bitmap, bitmap_offset,
1225 req->bitmap_words, bitmap_len);
1228 /* Called with dev->lock held */
1229 static void prepare_request(struct request *req, struct io_thread_req *io_req,
1230 unsigned long long offset, int page_offset,
1231 int len, struct page *page)
1233 struct gendisk *disk = req->rq_disk;
1234 struct ubd *ubd_dev = disk->private_data;
1236 io_req->req = req;
1237 io_req->fds[0] = (ubd_dev->cow.file != NULL) ? ubd_dev->cow.fd :
1238 ubd_dev->fd;
1239 io_req->fds[1] = ubd_dev->fd;
1240 io_req->cow_offset = -1;
1241 io_req->offset = offset;
1242 io_req->length = len;
1243 io_req->error = 0;
1244 io_req->sector_mask = 0;
1246 io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
1247 io_req->offsets[0] = 0;
1248 io_req->offsets[1] = ubd_dev->cow.data_offset;
1249 io_req->buffer = page_address(page) + page_offset;
1250 io_req->sectorsize = 1 << 9;
1252 if(ubd_dev->cow.file != NULL)
1253 cowify_req(io_req, ubd_dev->cow.bitmap,
1254 ubd_dev->cow.bitmap_offset, ubd_dev->cow.bitmap_len);
1258 /* Called with dev->lock held */
1259 static void do_ubd_request(struct request_queue *q)
1261 struct io_thread_req *io_req;
1262 struct request *req;
1263 int n;
1265 while(1){
1266 struct ubd *dev = q->queuedata;
1267 if(dev->end_sg == 0){
1268 struct request *req = blk_fetch_request(q);
1269 if(req == NULL)
1270 return;
1272 dev->request = req;
1273 dev->rq_pos = blk_rq_pos(req);
1274 dev->start_sg = 0;
1275 dev->end_sg = blk_rq_map_sg(q, req, dev->sg);
1278 req = dev->request;
1279 while(dev->start_sg < dev->end_sg){
1280 struct scatterlist *sg = &dev->sg[dev->start_sg];
1282 io_req = kmalloc(sizeof(struct io_thread_req),
1283 GFP_ATOMIC);
1284 if(io_req == NULL){
1285 if(list_empty(&dev->restart))
1286 list_add(&dev->restart, &restart);
1287 return;
1289 prepare_request(req, io_req,
1290 (unsigned long long)dev->rq_pos << 9,
1291 sg->offset, sg->length, sg_page(sg));
1293 n = os_write_file(thread_fd, &io_req,
1294 sizeof(struct io_thread_req *));
1295 if(n != sizeof(struct io_thread_req *)){
1296 if(n != -EAGAIN)
1297 printk("write to io thread failed, "
1298 "errno = %d\n", -n);
1299 else if(list_empty(&dev->restart))
1300 list_add(&dev->restart, &restart);
1301 kfree(io_req);
1302 return;
1305 dev->rq_pos += sg->length >> 9;
1306 dev->start_sg++;
1308 dev->end_sg = 0;
1309 dev->request = NULL;
1313 static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1315 struct ubd *ubd_dev = bdev->bd_disk->private_data;
1317 geo->heads = 128;
1318 geo->sectors = 32;
1319 geo->cylinders = ubd_dev->size / (128 * 32 * 512);
1320 return 0;
1323 static int ubd_ioctl(struct block_device *bdev, fmode_t mode,
1324 unsigned int cmd, unsigned long arg)
1326 struct ubd *ubd_dev = bdev->bd_disk->private_data;
1327 u16 ubd_id[ATA_ID_WORDS];
1329 switch (cmd) {
1330 struct cdrom_volctrl volume;
1331 case HDIO_GET_IDENTITY:
1332 memset(&ubd_id, 0, ATA_ID_WORDS * 2);
1333 ubd_id[ATA_ID_CYLS] = ubd_dev->size / (128 * 32 * 512);
1334 ubd_id[ATA_ID_HEADS] = 128;
1335 ubd_id[ATA_ID_SECTORS] = 32;
1336 if(copy_to_user((char __user *) arg, (char *) &ubd_id,
1337 sizeof(ubd_id)))
1338 return -EFAULT;
1339 return 0;
1341 case CDROMVOLREAD:
1342 if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
1343 return -EFAULT;
1344 volume.channel0 = 255;
1345 volume.channel1 = 255;
1346 volume.channel2 = 255;
1347 volume.channel3 = 255;
1348 if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
1349 return -EFAULT;
1350 return 0;
1352 return -EINVAL;
1355 static int update_bitmap(struct io_thread_req *req)
1357 int n;
1359 if(req->cow_offset == -1)
1360 return 0;
1362 n = os_seek_file(req->fds[1], req->cow_offset);
1363 if(n < 0){
1364 printk("do_io - bitmap lseek failed : err = %d\n", -n);
1365 return 1;
1368 n = os_write_file(req->fds[1], &req->bitmap_words,
1369 sizeof(req->bitmap_words));
1370 if(n != sizeof(req->bitmap_words)){
1371 printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
1372 req->fds[1]);
1373 return 1;
1376 return 0;
1379 static void do_io(struct io_thread_req *req)
1381 char *buf;
1382 unsigned long len;
1383 int n, nsectors, start, end, bit;
1384 int err;
1385 __u64 off;
1387 nsectors = req->length / req->sectorsize;
1388 start = 0;
1389 do {
1390 bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask);
1391 end = start;
1392 while((end < nsectors) &&
1393 (ubd_test_bit(end, (unsigned char *)
1394 &req->sector_mask) == bit))
1395 end++;
1397 off = req->offset + req->offsets[bit] +
1398 start * req->sectorsize;
1399 len = (end - start) * req->sectorsize;
1400 buf = &req->buffer[start * req->sectorsize];
1402 err = os_seek_file(req->fds[bit], off);
1403 if(err < 0){
1404 printk("do_io - lseek failed : err = %d\n", -err);
1405 req->error = 1;
1406 return;
1408 if(req->op == UBD_READ){
1409 n = 0;
1410 do {
1411 buf = &buf[n];
1412 len -= n;
1413 n = os_read_file(req->fds[bit], buf, len);
1414 if (n < 0) {
1415 printk("do_io - read failed, err = %d "
1416 "fd = %d\n", -n, req->fds[bit]);
1417 req->error = 1;
1418 return;
1420 } while((n < len) && (n != 0));
1421 if (n < len) memset(&buf[n], 0, len - n);
1422 } else {
1423 n = os_write_file(req->fds[bit], buf, len);
1424 if(n != len){
1425 printk("do_io - write failed err = %d "
1426 "fd = %d\n", -n, req->fds[bit]);
1427 req->error = 1;
1428 return;
1432 start = end;
1433 } while(start < nsectors);
1435 req->error = update_bitmap(req);
1438 /* Changed in start_io_thread, which is serialized by being called only
1439 * from ubd_init, which is an initcall.
1441 int kernel_fd = -1;
1443 /* Only changed by the io thread. XXX: currently unused. */
1444 static int io_count = 0;
1446 int io_thread(void *arg)
1448 struct io_thread_req *req;
1449 int n;
1451 ignore_sigwinch_sig();
1452 while(1){
1453 n = os_read_file(kernel_fd, &req,
1454 sizeof(struct io_thread_req *));
1455 if(n != sizeof(struct io_thread_req *)){
1456 if(n < 0)
1457 printk("io_thread - read failed, fd = %d, "
1458 "err = %d\n", kernel_fd, -n);
1459 else {
1460 printk("io_thread - short read, fd = %d, "
1461 "length = %d\n", kernel_fd, n);
1463 continue;
1465 io_count++;
1466 do_io(req);
1467 n = os_write_file(kernel_fd, &req,
1468 sizeof(struct io_thread_req *));
1469 if(n != sizeof(struct io_thread_req *))
1470 printk("io_thread - write failed, fd = %d, err = %d\n",
1471 kernel_fd, -n);
1474 return 0;