2 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
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.
20 #define MAJOR_NR UBD_MAJOR
23 #include "linux/config.h"
24 #include "linux/module.h"
25 #include "linux/blkdev.h"
26 #include "linux/hdreg.h"
27 #include "linux/init.h"
28 #include "linux/devfs_fs_kernel.h"
29 #include "linux/cdrom.h"
30 #include "linux/proc_fs.h"
31 #include "linux/ctype.h"
32 #include "linux/capability.h"
34 #include "linux/vmalloc.h"
35 #include "linux/blkpg.h"
36 #include "linux/genhd.h"
37 #include "linux/spinlock.h"
38 #include "asm/segment.h"
39 #include "asm/uaccess.h"
41 #include "asm/types.h"
42 #include "asm/tlbflush.h"
43 #include "user_util.h"
45 #include "kern_util.h"
47 #include "mconsole_kern.h"
52 #include "2_5compat.h"
57 static spinlock_t ubd_io_lock = SPIN_LOCK_UNLOCKED;
58 static spinlock_t ubd_lock = SPIN_LOCK_UNLOCKED;
60 static void (*do_ubd)(void);
62 static int ubd_open(struct inode * inode, struct file * filp);
63 static int ubd_release(struct inode * inode, struct file * file);
64 static int ubd_ioctl(struct inode * inode, struct file * file,
65 unsigned int cmd, unsigned long arg);
69 /* Changed in early boot */
70 static int ubd_do_mmap = 0;
71 #define UBD_MMAP_BLOCK_SIZE PAGE_SIZE
73 static struct block_device_operations ubd_blops = {
76 .release = ubd_release,
80 /* Protected by the queue_lock */
81 static request_queue_t *ubd_queue;
83 /* Protected by ubd_lock */
84 static int fake_major = MAJOR_NR;
86 static struct gendisk *ubd_gendisk[MAX_DEV];
87 static struct gendisk *fake_gendisk[MAX_DEV];
89 #ifdef CONFIG_BLK_DEV_UBD_SYNC
90 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
93 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
97 /* Not protected - changed only in ubd_setup_common and then only to
100 static struct openflags global_openflags = OPEN_FLAGS;
105 unsigned long *bitmap;
106 unsigned long bitmap_len;
116 struct openflags boot_openflags;
117 struct openflags openflags;
128 #define DEFAULT_COW { \
132 .bitmap_offset = 0, \
136 #define DEFAULT_UBD { \
141 .boot_openflags = OPEN_FLAGS, \
142 .openflags = OPEN_FLAGS, \
144 .cow = DEFAULT_COW, \
152 struct ubd ubd_dev[MAX_DEV] = { [ 0 ... MAX_DEV - 1 ] = DEFAULT_UBD };
154 static int ubd0_init(void)
156 struct ubd *dev = &ubd_dev[0];
158 if(dev->file == NULL)
159 dev->file = "root_fs";
163 __initcall(ubd0_init);
165 /* Only changed by fake_ide_setup which is a setup */
166 static int fake_ide = 0;
167 static struct proc_dir_entry *proc_ide_root = NULL;
168 static struct proc_dir_entry *proc_ide = NULL;
170 static void make_proc_ide(void)
172 proc_ide_root = proc_mkdir("ide", 0);
173 proc_ide = proc_mkdir("ide0", proc_ide_root);
176 static int proc_ide_read_media(char *page, char **start, off_t off, int count,
177 int *eof, void *data)
181 strcpy(page, "disk\n");
182 len = strlen("disk\n");
186 if (len <= 0) return 0;
193 static void make_ide_entries(char *dev_name)
195 struct proc_dir_entry *dir, *ent;
198 if(proc_ide_root == NULL) make_proc_ide();
200 dir = proc_mkdir(dev_name, proc_ide);
203 ent = create_proc_entry("media", S_IFREG|S_IRUGO, dir);
207 ent->read_proc = proc_ide_read_media;
208 ent->write_proc = NULL;
209 sprintf(name,"ide0/%s", dev_name);
210 proc_symlink(dev_name, proc_ide_root, name);
213 static int fake_ide_setup(char *str)
219 __setup("fake_ide", fake_ide_setup);
221 __uml_help(fake_ide_setup,
223 " Create ide0 entries that map onto ubd devices.\n\n"
226 static int parse_unit(char **ptr)
228 char *str = *ptr, *end;
232 n = simple_strtoul(str, &end, 0);
237 else if (('a' <= *str) && (*str <= 'h')) {
245 static int ubd_setup_common(char *str, int *index_out)
248 struct openflags flags = global_openflags;
252 if(index_out) *index_out = -1;
259 if(!strcmp(str, "mmap")){
260 CHOOSE_MODE(printk("mmap not supported by the ubd "
261 "driver in tt mode\n"),
266 if(!strcmp(str, "sync")){
267 global_openflags.s = 1;
270 major = simple_strtoul(str, &end, 0);
271 if((*end != '\0') || (end == str)){
273 "ubd_setup : didn't parse major number\n");
278 spin_lock(&ubd_lock);
279 if(fake_major != MAJOR_NR){
280 printk(KERN_ERR "Can't assign a fake major twice\n");
286 printk(KERN_INFO "Setting extra ubd major number to %d\n",
290 spin_unlock(&ubd_lock);
294 n = parse_unit(&str);
296 printk(KERN_ERR "ubd_setup : couldn't parse unit number "
301 printk(KERN_ERR "ubd_setup : index %d out of range "
302 "(%d devices, from 0 to %d)\n", n, MAX_DEV, MAX_DEV - 1);
307 spin_lock(&ubd_lock);
310 if(dev->file != NULL){
311 printk(KERN_ERR "ubd_setup : device already configured\n");
315 if(index_out) *index_out = n;
331 printk(KERN_ERR "ubd_setup : Expected '='\n");
336 backing_file = strchr(str, ',');
339 printk(KERN_ERR "Can't specify both 'd' and a "
342 *backing_file = '\0';
347 dev->cow.file = backing_file;
348 dev->boot_openflags = flags;
350 spin_unlock(&ubd_lock);
354 static int ubd_setup(char *str)
356 ubd_setup_common(str, NULL);
360 __setup("ubd", ubd_setup);
361 __uml_help(ubd_setup,
362 "ubd<n>=<filename>\n"
363 " This is used to associate a device with a file in the underlying\n"
364 " filesystem. Usually, there is a filesystem in the file, but \n"
365 " that's not required. Swap devices containing swap files can be\n"
366 " specified like this. Also, a file which doesn't contain a\n"
367 " filesystem can have its contents read in the virtual \n"
368 " machine by running dd on the device. n must be in the range\n"
369 " 0 to 7. Appending an 'r' to the number will cause that device\n"
370 " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
371 " an 's' (has to be _after_ 'r', if there is one) will cause data\n"
372 " to be written to disk on the host immediately.\n\n"
375 static int fakehd_set = 0;
376 static int fakehd(char *str)
378 printk(KERN_INFO "fakehd : Changing ubd name to \"hd\".\n");
383 __setup("fakehd", fakehd);
386 " Change the ubd device name to \"hd\".\n\n"
389 static void do_ubd_request(request_queue_t * q);
391 /* Only changed by ubd_init, which is an initcall. */
394 /* Changed by ubd_handler, which is serialized because interrupts only
399 /* call ubd_finish if you need to serialize */
400 static void __ubd_finish(struct request *req, int error)
408 nsect = req->current_nr_sectors;
409 req->sector += nsect;
410 req->buffer += nsect << 9;
412 req->nr_sectors -= nsect;
413 req->current_nr_sectors = 0;
417 static inline void ubd_finish(struct request *req, int error)
419 spin_lock(&ubd_io_lock);
420 __ubd_finish(req, error);
421 spin_unlock(&ubd_io_lock);
424 /* Called without ubd_io_lock held */
425 static void ubd_handler(void)
427 struct io_thread_req req;
428 struct request *rq = elv_next_request(ubd_queue);
433 n = read_ubd_fs(thread_fd, &req, sizeof(req));
434 if(n != sizeof(req)){
435 printk(KERN_ERR "Pid %d - spurious interrupt in ubd_handler, "
436 "err = %d\n", os_getpid(), -n);
437 spin_lock(&ubd_io_lock);
439 spin_unlock(&ubd_io_lock);
443 if((req.op != UBD_MMAP) &&
444 ((req.offset != ((__u64) (rq->sector)) << 9) ||
445 (req.length != (rq->current_nr_sectors) << 9)))
446 panic("I/O op mismatch");
448 if(req.map_fd != -1){
449 err = physmem_subst_mapping(req.buffer, req.map_fd,
452 printk("ubd_handler - physmem_subst_mapping failed, "
456 ubd_finish(rq, req.error);
457 reactivate_fd(thread_fd, UBD_IRQ);
458 do_ubd_request(ubd_queue);
461 static irqreturn_t ubd_intr(int irq, void *dev, struct pt_regs *unused)
467 /* Only changed by ubd_init, which is an initcall. */
468 static int io_pid = -1;
470 void kill_io_thread(void)
473 os_kill_process(io_pid, 1);
476 __uml_exitcall(kill_io_thread);
478 static int ubd_file_size(struct ubd *dev, __u64 *size_out)
482 file = dev->cow.file ? dev->cow.file : dev->file;
483 return(os_file_size(file, size_out));
486 static void ubd_close(struct ubd *dev)
489 physmem_forget_descriptor(dev->fd);
490 os_close_file(dev->fd);
491 if(dev->cow.file == NULL)
495 physmem_forget_descriptor(dev->cow.fd);
496 os_close_file(dev->cow.fd);
497 vfree(dev->cow.bitmap);
498 dev->cow.bitmap = NULL;
501 static int ubd_open_dev(struct ubd *dev)
503 struct openflags flags;
505 int err, create_cow, *create_ptr;
507 dev->openflags = dev->boot_openflags;
509 create_ptr = (dev->cow.file != NULL) ? &create_cow : NULL;
510 back_ptr = dev->no_cow ? NULL : &dev->cow.file;
511 dev->fd = open_ubd_file(dev->file, &dev->openflags, back_ptr,
512 &dev->cow.bitmap_offset, &dev->cow.bitmap_len,
513 &dev->cow.data_offset, create_ptr);
515 if((dev->fd == -ENOENT) && create_cow){
516 dev->fd = create_cow_file(dev->file, dev->cow.file,
517 dev->openflags, 1 << 9, PAGE_SIZE,
518 &dev->cow.bitmap_offset,
519 &dev->cow.bitmap_len,
520 &dev->cow.data_offset);
522 printk(KERN_INFO "Creating \"%s\" as COW file for "
523 "\"%s\"\n", dev->file, dev->cow.file);
527 if(dev->fd < 0) return(dev->fd);
529 if(dev->cow.file != NULL){
531 dev->cow.bitmap = (void *) vmalloc(dev->cow.bitmap_len);
532 if(dev->cow.bitmap == NULL){
533 printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
536 flush_tlb_kernel_vm();
538 err = read_cow_bitmap(dev->fd, dev->cow.bitmap,
539 dev->cow.bitmap_offset,
540 dev->cow.bitmap_len);
544 flags = dev->openflags;
546 err = open_ubd_file(dev->cow.file, &flags, NULL, NULL, NULL,
548 if(err < 0) goto error;
553 os_close_file(dev->fd);
557 static int ubd_new_disk(int major, u64 size, int unit,
558 struct gendisk **disk_out)
561 struct gendisk *disk;
562 char from[sizeof("ubd/nnnnn\0")], to[sizeof("discnnnnn/disc\0")];
565 disk = alloc_disk(1 << UBD_SHIFT);
570 disk->first_minor = unit << UBD_SHIFT;
571 disk->fops = &ubd_blops;
572 set_capacity(disk, size / 512);
573 if(major == MAJOR_NR){
574 sprintf(disk->disk_name, "ubd%c", 'a' + unit);
575 sprintf(disk->devfs_name, "ubd/disc%d", unit);
576 sprintf(from, "ubd/%d", unit);
577 sprintf(to, "disc%d/disc", unit);
578 err = devfs_mk_symlink(from, to);
580 printk("ubd_new_disk failed to make link from %s to "
581 "%s, error = %d\n", from, to, err);
584 sprintf(disk->disk_name, "ubd_fake%d", unit);
585 sprintf(disk->devfs_name, "ubd_fake/disc%d", unit);
588 disk->private_data = &ubd_dev[unit];
589 disk->queue = ubd_queue;
596 static int ubd_add(int n)
598 struct ubd *dev = &ubd_dev[n];
601 if(dev->file == NULL)
604 if (ubd_open_dev(dev))
607 err = ubd_file_size(dev, &dev->size);
611 err = ubd_new_disk(MAJOR_NR, dev->size, n, &ubd_gendisk[n]);
615 if(fake_major != MAJOR_NR)
616 ubd_new_disk(fake_major, dev->size, n,
619 /* perhaps this should also be under the "if (fake_major)" above */
620 /* using the fake_disk->disk_name and also the fakehd_set name */
622 make_ide_entries(ubd_gendisk[n]->disk_name);
628 static int ubd_config(char *str)
632 str = uml_strdup(str);
634 printk(KERN_ERR "ubd_config failed to strdup string\n");
637 err = ubd_setup_common(str, &n);
642 if(n == -1) return(0);
644 spin_lock(&ubd_lock);
647 ubd_dev[n].file = NULL;
648 spin_unlock(&ubd_lock);
653 static int ubd_get_config(char *name, char *str, int size, char **error_out)
659 n = simple_strtoul(name, &end, 0);
660 if((*end != '\0') || (end == name)){
661 *error_out = "ubd_get_config : didn't parse device number";
665 if((n >= MAX_DEV) || (n < 0)){
666 *error_out = "ubd_get_config : device number out of range";
671 spin_lock(&ubd_lock);
673 if(dev->file == NULL){
674 CONFIG_CHUNK(str, size, len, "", 1);
678 CONFIG_CHUNK(str, size, len, dev->file, 0);
680 if(dev->cow.file != NULL){
681 CONFIG_CHUNK(str, size, len, ",", 0);
682 CONFIG_CHUNK(str, size, len, dev->cow.file, 1);
684 else CONFIG_CHUNK(str, size, len, "", 1);
687 spin_unlock(&ubd_lock);
691 static int ubd_remove(char *str)
694 int n, err = -ENODEV;
696 n = parse_unit(&str);
698 if((n < 0) || (n >= MAX_DEV))
703 return(-EBUSY); /* you cannot remove a open disk */
706 spin_lock(&ubd_lock);
708 if(ubd_gendisk[n] == NULL)
711 del_gendisk(ubd_gendisk[n]);
712 put_disk(ubd_gendisk[n]);
713 ubd_gendisk[n] = NULL;
715 if(fake_gendisk[n] != NULL){
716 del_gendisk(fake_gendisk[n]);
717 put_disk(fake_gendisk[n]);
718 fake_gendisk[n] = NULL;
721 *dev = ((struct ubd) DEFAULT_UBD);
724 spin_unlock(&ubd_lock);
728 static struct mc_device ubd_mc = {
730 .config = ubd_config,
731 .get_config = ubd_get_config,
732 .remove = ubd_remove,
735 static int ubd_mc_init(void)
737 mconsole_register_dev(&ubd_mc);
741 __initcall(ubd_mc_init);
748 if (register_blkdev(MAJOR_NR, "ubd"))
751 ubd_queue = blk_init_queue(do_ubd_request, &ubd_io_lock);
753 unregister_blkdev(MAJOR_NR, "ubd");
757 if (fake_major != MAJOR_NR) {
758 char name[sizeof("ubd_nnn\0")];
760 snprintf(name, sizeof(name), "ubd_%d", fake_major);
762 if (register_blkdev(fake_major, "ubd"))
765 for (i = 0; i < MAX_DEV; i++)
770 late_initcall(ubd_init);
772 int ubd_driver_init(void){
776 /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
777 if(global_openflags.s){
778 printk(KERN_INFO "ubd: Synchronous mode\n");
779 /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
780 * enough. So use anyway the io thread. */
782 stack = alloc_stack(0, 0);
783 io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
788 "ubd : Failed to start I/O thread (errno = %d) - "
789 "falling back to synchronous I/O\n", -io_pid);
793 err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
794 SA_INTERRUPT, "ubd", ubd_dev);
796 printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
800 device_initcall(ubd_driver_init);
802 static int ubd_open(struct inode *inode, struct file *filp)
804 struct gendisk *disk = inode->i_bdev->bd_disk;
805 struct ubd *dev = disk->private_data;
809 err = ubd_open_dev(dev);
811 printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
812 disk->disk_name, dev->file, -err);
817 if((filp->f_mode & FMODE_WRITE) && !dev->openflags.w){
818 if(--dev->count == 0) ubd_close(dev);
825 static int ubd_release(struct inode * inode, struct file * file)
827 struct gendisk *disk = inode->i_bdev->bd_disk;
828 struct ubd *dev = disk->private_data;
830 if(--dev->count == 0)
835 static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
836 __u64 *cow_offset, unsigned long *bitmap,
837 __u64 bitmap_offset, unsigned long *bitmap_words,
840 __u64 sector = io_offset >> 9;
841 int i, update_bitmap = 0;
843 for(i = 0; i < length >> 9; i++){
845 ubd_set_bit(i, (unsigned char *) cow_mask);
846 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
850 ubd_set_bit(sector + i, (unsigned char *) bitmap);
856 *cow_offset = sector / (sizeof(unsigned long) * 8);
858 /* This takes care of the case where we're exactly at the end of the
859 * device, and *cow_offset + 1 is off the end. So, just back it up
860 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
861 * for the original diagnosis.
863 if(*cow_offset == ((bitmap_len + sizeof(unsigned long) - 1) /
864 sizeof(unsigned long) - 1))
867 bitmap_words[0] = bitmap[*cow_offset];
868 bitmap_words[1] = bitmap[*cow_offset + 1];
870 *cow_offset *= sizeof(unsigned long);
871 *cow_offset += bitmap_offset;
874 static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
875 __u64 bitmap_offset, __u64 bitmap_len)
877 __u64 sector = req->offset >> 9;
880 if(req->length > (sizeof(req->sector_mask) * 8) << 9)
881 panic("Operation too long");
883 if(req->op == UBD_READ) {
884 for(i = 0; i < req->length >> 9; i++){
885 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
886 ubd_set_bit(i, (unsigned char *)
890 else cowify_bitmap(req->offset, req->length, &req->sector_mask,
891 &req->cow_offset, bitmap, bitmap_offset,
892 req->bitmap_words, bitmap_len);
895 static int mmap_fd(struct request *req, struct ubd *dev, __u64 offset)
898 unsigned char *bitmap;
901 /* mmap must have been requested on the command line */
905 /* The buffer must be page aligned */
906 if(((unsigned long) req->buffer % UBD_MMAP_BLOCK_SIZE) != 0)
909 /* The request must be a page long */
910 if((req->current_nr_sectors << 9) != PAGE_SIZE)
913 if(dev->cow.file == NULL)
916 sector = offset >> 9;
917 bitmap = (unsigned char *) dev->cow.bitmap;
918 bit = ubd_test_bit(sector, bitmap);
920 for(i = 1; i < req->current_nr_sectors; i++){
921 if(ubd_test_bit(sector + i, bitmap) != bit)
925 if(bit || (rq_data_dir(req) == WRITE))
926 offset += dev->cow.data_offset;
928 /* The data on disk must be page aligned */
929 if((offset % UBD_MMAP_BLOCK_SIZE) != 0)
932 return(bit ? dev->fd : dev->cow.fd);
935 static int prepare_mmap_request(struct ubd *dev, int fd, __u64 offset,
937 struct io_thread_req *io_req)
941 if(rq_data_dir(req) == WRITE){
942 /* Writes are almost no-ops since the new data is already in the
946 if(dev->cow.file != NULL)
947 cowify_bitmap(io_req->offset, io_req->length,
948 &io_req->sector_mask, &io_req->cow_offset,
949 dev->cow.bitmap, dev->cow.bitmap_offset,
950 io_req->bitmap_words,
951 dev->cow.bitmap_len);
956 if((dev->cow.file != NULL) && (fd == dev->cow.fd))
958 else w = dev->openflags.w;
960 if((dev->cow.file != NULL) && (fd == dev->fd))
961 offset += dev->cow.data_offset;
963 err = physmem_subst_mapping(req->buffer, fd, offset, w);
965 printk("physmem_subst_mapping failed, err = %d\n",
971 io_req->op = UBD_MMAP;
972 io_req->buffer = req->buffer;
976 /* Called with ubd_io_lock held */
977 static int prepare_request(struct request *req, struct io_thread_req *io_req)
979 struct gendisk *disk = req->rq_disk;
980 struct ubd *dev = disk->private_data;
984 if(req->rq_status == RQ_INACTIVE) return(1);
986 if((rq_data_dir(req) == WRITE) && !dev->openflags.w){
987 printk("Write attempted on readonly ubd device %s\n",
993 offset = ((__u64) req->sector) << 9;
994 len = req->current_nr_sectors << 9;
996 io_req->fds[0] = (dev->cow.file != NULL) ? dev->cow.fd : dev->fd;
997 io_req->fds[1] = dev->fd;
999 io_req->cow_offset = -1;
1000 io_req->offset = offset;
1001 io_req->length = len;
1003 io_req->sector_mask = 0;
1005 fd = mmap_fd(req, dev, io_req->offset);
1007 /* If mmapping is otherwise OK, but the first access to the
1008 * page is a write, then it's not mapped in yet. So we have
1009 * to write the data to disk first, then we can map the disk
1010 * page in and continue normally from there.
1012 if((rq_data_dir(req) == WRITE) && !is_remapped(req->buffer)){
1013 io_req->map_fd = dev->fd;
1014 io_req->map_offset = io_req->offset +
1015 dev->cow.data_offset;
1018 else return(prepare_mmap_request(dev, fd, io_req->offset, req,
1022 if(rq_data_dir(req) == READ)
1024 else dev->nomap_writes++;
1026 io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
1027 io_req->offsets[0] = 0;
1028 io_req->offsets[1] = dev->cow.data_offset;
1029 io_req->buffer = req->buffer;
1030 io_req->sectorsize = 1 << 9;
1032 if(dev->cow.file != NULL)
1033 cowify_req(io_req, dev->cow.bitmap, dev->cow.bitmap_offset,
1034 dev->cow.bitmap_len);
1039 /* Called with ubd_io_lock held */
1040 static void do_ubd_request(request_queue_t *q)
1042 struct io_thread_req io_req;
1043 struct request *req;
1046 if(thread_fd == -1){
1047 while((req = elv_next_request(q)) != NULL){
1048 err = prepare_request(req, &io_req);
1051 __ubd_finish(req, io_req.error);
1056 if(do_ubd || (req = elv_next_request(q)) == NULL)
1058 err = prepare_request(req, &io_req);
1060 do_ubd = ubd_handler;
1061 n = write_ubd_fs(thread_fd, (char *) &io_req,
1063 if(n != sizeof(io_req))
1064 printk("write to io thread failed, "
1065 "errno = %d\n", -n);
1070 static int ubd_ioctl(struct inode * inode, struct file * file,
1071 unsigned int cmd, unsigned long arg)
1073 struct hd_geometry *loc = (struct hd_geometry *) arg;
1074 struct ubd *dev = inode->i_bdev->bd_disk->private_data;
1075 struct hd_driveid ubd_id = {
1082 struct hd_geometry g;
1083 struct cdrom_volctrl volume;
1085 if(!loc) return(-EINVAL);
1088 g.cylinders = dev->size / (128 * 32 * 512);
1089 g.start = get_start_sect(inode->i_bdev);
1090 return(copy_to_user(loc, &g, sizeof(g)) ? -EFAULT : 0);
1092 case HDIO_GET_IDENTITY:
1093 ubd_id.cyls = dev->size / (128 * 32 * 512);
1094 if(copy_to_user((char *) arg, (char *) &ubd_id,
1100 if(copy_from_user(&volume, (char *) arg, sizeof(volume)))
1102 volume.channel0 = 255;
1103 volume.channel1 = 255;
1104 volume.channel2 = 255;
1105 volume.channel3 = 255;
1106 if(copy_to_user((char *) arg, &volume, sizeof(volume)))
1113 static int ubd_check_remapped(int fd, unsigned long address, int is_write,
1116 __u64 bitmap_offset;
1117 unsigned long new_bitmap[2];
1120 /* If it's not a write access, we can't do anything about it */
1124 /* We have a write */
1125 for(i = 0; i < sizeof(ubd_dev) / sizeof(ubd_dev[0]); i++){
1126 struct ubd *dev = &ubd_dev[i];
1128 if((dev->fd != fd) && (dev->cow.fd != fd))
1131 /* It's a write to a ubd device */
1133 if(!dev->openflags.w){
1134 /* It's a write access on a read-only device - probably
1135 * shouldn't happen. If the kernel is trying to change
1136 * something with no intention of writing it back out,
1137 * then this message will clue us in that this needs
1140 printk("Write access to mapped page from readonly ubd "
1145 /* It's a write to a writeable ubd device - it must be COWed
1146 * because, otherwise, the page would have been mapped in
1151 panic("Write fault on writeable non-COW ubd device %d",
1154 /* It should also be an access to the backing file since the
1155 * COW pages should be mapped in read-write
1159 panic("Write fault on a backing page of ubd "
1162 /* So, we do the write, copying the backing data to the COW
1166 err = os_seek_file(dev->fd, offset + dev->cow.data_offset);
1168 panic("Couldn't seek to %lld in COW file of ubd "
1169 "device %d, err = %d",
1170 offset + dev->cow.data_offset, i, -err);
1172 n = os_write_file(dev->fd, (void *) address, PAGE_SIZE);
1174 panic("Couldn't copy data to COW file of ubd "
1175 "device %d, err = %d", i, -n);
1177 /* ... updating the COW bitmap... */
1179 cowify_bitmap(offset, PAGE_SIZE, NULL, &bitmap_offset,
1180 dev->cow.bitmap, dev->cow.bitmap_offset,
1181 new_bitmap, dev->cow.bitmap_len);
1183 err = os_seek_file(dev->fd, bitmap_offset);
1185 panic("Couldn't seek to %lld in COW file of ubd "
1186 "device %d, err = %d", bitmap_offset, i, -err);
1188 n = os_write_file(dev->fd, new_bitmap, sizeof(new_bitmap));
1189 if(n != sizeof(new_bitmap))
1190 panic("Couldn't update bitmap of ubd device %d, "
1193 /* Maybe we can map the COW page in, and maybe we can't. If
1194 * it is a pre-V3 COW file, we can't, since the alignment will
1195 * be wrong. If it is a V3 or later COW file which has been
1196 * moved to a system with a larger page size, then maybe we
1197 * can't, depending on the exact location of the page.
1200 offset += dev->cow.data_offset;
1202 /* Remove the remapping, putting the original anonymous page
1203 * back. If the COW file can be mapped in, that is done.
1204 * Otherwise, the COW page is read in.
1207 if(!physmem_remove_mapping((void *) address))
1208 panic("Address 0x%lx not remapped by ubd device %d",
1210 if((offset % UBD_MMAP_BLOCK_SIZE) == 0)
1211 physmem_subst_mapping((void *) address, dev->fd,
1214 err = os_seek_file(dev->fd, offset);
1216 panic("Couldn't seek to %lld in COW file of "
1217 "ubd device %d, err = %d", offset, i,
1220 n = os_read_file(dev->fd, (void *) address, PAGE_SIZE);
1222 panic("Failed to read page from offset %llx of "
1223 "COW file of ubd device %d, err = %d",
1230 /* It's not a write on a ubd device */
1234 static struct remapper ubd_remapper = {
1235 .list = LIST_HEAD_INIT(ubd_remapper.list),
1236 .proc = ubd_check_remapped,
1239 static int ubd_remapper_setup(void)
1242 register_remapper(&ubd_remapper);
1247 __initcall(ubd_remapper_setup);
1250 * Overrides for Emacs so that we follow Linus's tabbing style.
1251 * Emacs will notice this stuff at the end of the file and automatically
1252 * adjust the settings for this buffer only. This must remain at the end
1254 * ---------------------------------------------------------------------------
1256 * c-file-style: "linux"