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)\n", n, MAX_DEV);
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 static void ubd_finish(struct request *req, int error)
404 spin_lock(&ubd_io_lock);
406 spin_unlock(&ubd_io_lock);
409 nsect = req->current_nr_sectors;
410 req->sector += nsect;
411 req->buffer += nsect << 9;
413 req->nr_sectors -= nsect;
414 req->current_nr_sectors = 0;
415 spin_lock(&ubd_io_lock);
417 spin_unlock(&ubd_io_lock);
420 static void ubd_handler(void)
422 struct io_thread_req req;
423 struct request *rq = elv_next_request(ubd_queue);
428 n = read_ubd_fs(thread_fd, &req, sizeof(req));
429 if(n != sizeof(req)){
430 printk(KERN_ERR "Pid %d - spurious interrupt in ubd_handler, "
431 "err = %d\n", os_getpid(), -n);
432 spin_lock(&ubd_io_lock);
434 spin_unlock(&ubd_io_lock);
438 if((req.op != UBD_MMAP) &&
439 ((req.offset != ((__u64) (rq->sector)) << 9) ||
440 (req.length != (rq->current_nr_sectors) << 9)))
441 panic("I/O op mismatch");
443 if(req.map_fd != -1){
444 err = physmem_subst_mapping(req.buffer, req.map_fd,
447 printk("ubd_handler - physmem_subst_mapping failed, "
451 ubd_finish(rq, req.error);
452 reactivate_fd(thread_fd, UBD_IRQ);
453 do_ubd_request(ubd_queue);
456 static irqreturn_t ubd_intr(int irq, void *dev, struct pt_regs *unused)
462 /* Only changed by ubd_init, which is an initcall. */
463 static int io_pid = -1;
465 void kill_io_thread(void)
468 os_kill_process(io_pid, 1);
471 __uml_exitcall(kill_io_thread);
473 static int ubd_file_size(struct ubd *dev, __u64 *size_out)
477 file = dev->cow.file ? dev->cow.file : dev->file;
478 return(os_file_size(file, size_out));
481 static void ubd_close(struct ubd *dev)
484 physmem_forget_descriptor(dev->fd);
485 os_close_file(dev->fd);
486 if(dev->cow.file == NULL)
490 physmem_forget_descriptor(dev->cow.fd);
491 os_close_file(dev->cow.fd);
492 vfree(dev->cow.bitmap);
493 dev->cow.bitmap = NULL;
496 static int ubd_open_dev(struct ubd *dev)
498 struct openflags flags;
500 int err, create_cow, *create_ptr;
502 dev->openflags = dev->boot_openflags;
504 create_ptr = (dev->cow.file != NULL) ? &create_cow : NULL;
505 back_ptr = dev->no_cow ? NULL : &dev->cow.file;
506 dev->fd = open_ubd_file(dev->file, &dev->openflags, back_ptr,
507 &dev->cow.bitmap_offset, &dev->cow.bitmap_len,
508 &dev->cow.data_offset, create_ptr);
510 if((dev->fd == -ENOENT) && create_cow){
511 dev->fd = create_cow_file(dev->file, dev->cow.file,
512 dev->openflags, 1 << 9, PAGE_SIZE,
513 &dev->cow.bitmap_offset,
514 &dev->cow.bitmap_len,
515 &dev->cow.data_offset);
517 printk(KERN_INFO "Creating \"%s\" as COW file for "
518 "\"%s\"\n", dev->file, dev->cow.file);
522 if(dev->fd < 0) return(dev->fd);
524 if(dev->cow.file != NULL){
526 dev->cow.bitmap = (void *) vmalloc(dev->cow.bitmap_len);
527 if(dev->cow.bitmap == NULL){
528 printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
531 flush_tlb_kernel_vm();
533 err = read_cow_bitmap(dev->fd, dev->cow.bitmap,
534 dev->cow.bitmap_offset,
535 dev->cow.bitmap_len);
539 flags = dev->openflags;
541 err = open_ubd_file(dev->cow.file, &flags, NULL, NULL, NULL,
543 if(err < 0) goto error;
548 os_close_file(dev->fd);
552 static int ubd_new_disk(int major, u64 size, int unit,
553 struct gendisk **disk_out)
556 struct gendisk *disk;
557 char from[sizeof("ubd/nnnnn\0")], to[sizeof("discnnnnn/disc\0")];
560 disk = alloc_disk(1 << UBD_SHIFT);
565 disk->first_minor = unit << UBD_SHIFT;
566 disk->fops = &ubd_blops;
567 set_capacity(disk, size / 512);
568 if(major == MAJOR_NR){
569 sprintf(disk->disk_name, "ubd%c", 'a' + unit);
570 sprintf(disk->devfs_name, "ubd/disc%d", unit);
571 sprintf(from, "ubd/%d", unit);
572 sprintf(to, "disc%d/disc", unit);
573 err = devfs_mk_symlink(from, to);
575 printk("ubd_new_disk failed to make link from %s to "
576 "%s, error = %d\n", from, to, err);
579 sprintf(disk->disk_name, "ubd_fake%d", unit);
580 sprintf(disk->devfs_name, "ubd_fake/disc%d", unit);
583 disk->private_data = &ubd_dev[unit];
584 disk->queue = ubd_queue;
591 static int ubd_add(int n)
593 struct ubd *dev = &ubd_dev[n];
596 if(dev->file == NULL)
599 if (ubd_open_dev(dev))
602 err = ubd_file_size(dev, &dev->size);
606 err = ubd_new_disk(MAJOR_NR, dev->size, n, &ubd_gendisk[n]);
610 if(fake_major != MAJOR_NR)
611 ubd_new_disk(fake_major, dev->size, n,
614 /* perhaps this should also be under the "if (fake_major)" above */
615 /* using the fake_disk->disk_name and also the fakehd_set name */
617 make_ide_entries(ubd_gendisk[n]->disk_name);
623 static int ubd_config(char *str)
627 str = uml_strdup(str);
629 printk(KERN_ERR "ubd_config failed to strdup string\n");
632 err = ubd_setup_common(str, &n);
637 if(n == -1) return(0);
639 spin_lock(&ubd_lock);
642 ubd_dev[n].file = NULL;
643 spin_unlock(&ubd_lock);
648 static int ubd_get_config(char *name, char *str, int size, char **error_out)
654 n = simple_strtoul(name, &end, 0);
655 if((*end != '\0') || (end == name)){
656 *error_out = "ubd_get_config : didn't parse device number";
660 if((n >= MAX_DEV) || (n < 0)){
661 *error_out = "ubd_get_config : device number out of range";
666 spin_lock(&ubd_lock);
668 if(dev->file == NULL){
669 CONFIG_CHUNK(str, size, len, "", 1);
673 CONFIG_CHUNK(str, size, len, dev->file, 0);
675 if(dev->cow.file != NULL){
676 CONFIG_CHUNK(str, size, len, ",", 0);
677 CONFIG_CHUNK(str, size, len, dev->cow.file, 1);
679 else CONFIG_CHUNK(str, size, len, "", 1);
682 spin_unlock(&ubd_lock);
686 static int ubd_remove(char *str)
689 int n, err = -ENODEV;
691 n = parse_unit(&str);
693 if((n < 0) || (n >= MAX_DEV))
698 return(-EBUSY); /* you cannot remove a open disk */
701 spin_lock(&ubd_lock);
703 if(ubd_gendisk[n] == NULL)
706 del_gendisk(ubd_gendisk[n]);
707 put_disk(ubd_gendisk[n]);
708 ubd_gendisk[n] = NULL;
710 if(fake_gendisk[n] != NULL){
711 del_gendisk(fake_gendisk[n]);
712 put_disk(fake_gendisk[n]);
713 fake_gendisk[n] = NULL;
716 *dev = ((struct ubd) DEFAULT_UBD);
719 spin_unlock(&ubd_lock);
723 static struct mc_device ubd_mc = {
725 .config = ubd_config,
726 .get_config = ubd_get_config,
727 .remove = ubd_remove,
730 static int ubd_mc_init(void)
732 mconsole_register_dev(&ubd_mc);
736 __initcall(ubd_mc_init);
743 if (register_blkdev(MAJOR_NR, "ubd"))
746 ubd_queue = blk_init_queue(do_ubd_request, &ubd_io_lock);
748 unregister_blkdev(MAJOR_NR, "ubd");
752 elevator_init(ubd_queue, &elevator_noop);
754 if (fake_major != MAJOR_NR) {
755 char name[sizeof("ubd_nnn\0")];
757 snprintf(name, sizeof(name), "ubd_%d", fake_major);
759 if (register_blkdev(fake_major, "ubd"))
762 for (i = 0; i < MAX_DEV; i++)
767 late_initcall(ubd_init);
769 int ubd_driver_init(void){
773 if(global_openflags.s){
774 printk(KERN_INFO "ubd : Synchronous mode\n");
777 stack = alloc_stack(0, 0);
778 io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
783 "ubd : Failed to start I/O thread (errno = %d) - "
784 "falling back to synchronous I/O\n", -io_pid);
787 err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
788 SA_INTERRUPT, "ubd", ubd_dev);
790 printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
794 device_initcall(ubd_driver_init);
796 static int ubd_open(struct inode *inode, struct file *filp)
798 struct gendisk *disk = inode->i_bdev->bd_disk;
799 struct ubd *dev = disk->private_data;
803 err = ubd_open_dev(dev);
805 printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
806 disk->disk_name, dev->file, -err);
811 if((filp->f_mode & FMODE_WRITE) && !dev->openflags.w){
812 if(--dev->count == 0) ubd_close(dev);
819 static int ubd_release(struct inode * inode, struct file * file)
821 struct gendisk *disk = inode->i_bdev->bd_disk;
822 struct ubd *dev = disk->private_data;
824 if(--dev->count == 0)
829 static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
830 __u64 *cow_offset, unsigned long *bitmap,
831 __u64 bitmap_offset, unsigned long *bitmap_words,
834 __u64 sector = io_offset >> 9;
835 int i, update_bitmap = 0;
837 for(i = 0; i < length >> 9; i++){
839 ubd_set_bit(i, (unsigned char *) cow_mask);
840 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
844 ubd_set_bit(sector + i, (unsigned char *) bitmap);
850 *cow_offset = sector / (sizeof(unsigned long) * 8);
852 /* This takes care of the case where we're exactly at the end of the
853 * device, and *cow_offset + 1 is off the end. So, just back it up
854 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
855 * for the original diagnosis.
857 if(*cow_offset == ((bitmap_len + sizeof(unsigned long) - 1) /
858 sizeof(unsigned long) - 1))
861 bitmap_words[0] = bitmap[*cow_offset];
862 bitmap_words[1] = bitmap[*cow_offset + 1];
864 *cow_offset *= sizeof(unsigned long);
865 *cow_offset += bitmap_offset;
868 static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
869 __u64 bitmap_offset, __u64 bitmap_len)
871 __u64 sector = req->offset >> 9;
874 if(req->length > (sizeof(req->sector_mask) * 8) << 9)
875 panic("Operation too long");
877 if(req->op == UBD_READ) {
878 for(i = 0; i < req->length >> 9; i++){
879 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
880 ubd_set_bit(i, (unsigned char *)
884 else cowify_bitmap(req->offset, req->length, &req->sector_mask,
885 &req->cow_offset, bitmap, bitmap_offset,
886 req->bitmap_words, bitmap_len);
889 static int mmap_fd(struct request *req, struct ubd *dev, __u64 offset)
892 unsigned char *bitmap;
895 /* mmap must have been requested on the command line */
899 /* The buffer must be page aligned */
900 if(((unsigned long) req->buffer % UBD_MMAP_BLOCK_SIZE) != 0)
903 /* The request must be a page long */
904 if((req->current_nr_sectors << 9) != PAGE_SIZE)
907 if(dev->cow.file == NULL)
910 sector = offset >> 9;
911 bitmap = (unsigned char *) dev->cow.bitmap;
912 bit = ubd_test_bit(sector, bitmap);
914 for(i = 1; i < req->current_nr_sectors; i++){
915 if(ubd_test_bit(sector + i, bitmap) != bit)
919 if(bit || (rq_data_dir(req) == WRITE))
920 offset += dev->cow.data_offset;
922 /* The data on disk must be page aligned */
923 if((offset % UBD_MMAP_BLOCK_SIZE) != 0)
926 return(bit ? dev->fd : dev->cow.fd);
929 static int prepare_mmap_request(struct ubd *dev, int fd, __u64 offset,
931 struct io_thread_req *io_req)
935 if(rq_data_dir(req) == WRITE){
936 /* Writes are almost no-ops since the new data is already in the
940 if(dev->cow.file != NULL)
941 cowify_bitmap(io_req->offset, io_req->length,
942 &io_req->sector_mask, &io_req->cow_offset,
943 dev->cow.bitmap, dev->cow.bitmap_offset,
944 io_req->bitmap_words,
945 dev->cow.bitmap_len);
950 if((dev->cow.file != NULL) && (fd == dev->cow.fd))
952 else w = dev->openflags.w;
954 if((dev->cow.file != NULL) && (fd == dev->fd))
955 offset += dev->cow.data_offset;
957 err = physmem_subst_mapping(req->buffer, fd, offset, w);
959 printk("physmem_subst_mapping failed, err = %d\n",
965 io_req->op = UBD_MMAP;
966 io_req->buffer = req->buffer;
970 static int prepare_request(struct request *req, struct io_thread_req *io_req)
972 struct gendisk *disk = req->rq_disk;
973 struct ubd *dev = disk->private_data;
977 if(req->rq_status == RQ_INACTIVE) return(1);
979 if((rq_data_dir(req) == WRITE) && !dev->openflags.w){
980 printk("Write attempted on readonly ubd device %s\n",
982 spin_lock(&ubd_io_lock);
984 spin_unlock(&ubd_io_lock);
988 offset = ((__u64) req->sector) << 9;
989 len = req->current_nr_sectors << 9;
991 io_req->fds[0] = (dev->cow.file != NULL) ? dev->cow.fd : dev->fd;
992 io_req->fds[1] = dev->fd;
994 io_req->cow_offset = -1;
995 io_req->offset = offset;
996 io_req->length = len;
998 io_req->sector_mask = 0;
1000 fd = mmap_fd(req, dev, io_req->offset);
1002 /* If mmapping is otherwise OK, but the first access to the
1003 * page is a write, then it's not mapped in yet. So we have
1004 * to write the data to disk first, then we can map the disk
1005 * page in and continue normally from there.
1007 if((rq_data_dir(req) == WRITE) && !is_remapped(req->buffer, dev->fd, io_req->offset + dev->cow.data_offset)){
1008 io_req->map_fd = dev->fd;
1009 io_req->map_offset = io_req->offset +
1010 dev->cow.data_offset;
1013 else return(prepare_mmap_request(dev, fd, io_req->offset, req,
1017 if(rq_data_dir(req) == READ)
1019 else dev->nomap_writes++;
1021 io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
1022 io_req->offsets[0] = 0;
1023 io_req->offsets[1] = dev->cow.data_offset;
1024 io_req->buffer = req->buffer;
1025 io_req->sectorsize = 1 << 9;
1027 if(dev->cow.file != NULL)
1028 cowify_req(io_req, dev->cow.bitmap, dev->cow.bitmap_offset,
1029 dev->cow.bitmap_len);
1034 static void do_ubd_request(request_queue_t *q)
1036 struct io_thread_req io_req;
1037 struct request *req;
1040 if(thread_fd == -1){
1041 while(!elv_queue_empty(q)){
1042 req = elv_next_request(q);
1043 err = prepare_request(req, &io_req);
1046 ubd_finish(req, io_req.error);
1051 if(do_ubd || elv_queue_empty(q))
1053 req = elv_next_request(q);
1054 err = prepare_request(req, &io_req);
1056 do_ubd = ubd_handler;
1057 n = write_ubd_fs(thread_fd, (char *) &io_req,
1059 if(n != sizeof(io_req))
1060 printk("write to io thread failed, "
1061 "errno = %d\n", -n);
1066 static int ubd_ioctl(struct inode * inode, struct file * file,
1067 unsigned int cmd, unsigned long arg)
1069 struct hd_geometry *loc = (struct hd_geometry *) arg;
1070 struct ubd *dev = inode->i_bdev->bd_disk->private_data;
1072 struct hd_driveid ubd_id = {
1079 struct hd_geometry g;
1080 struct cdrom_volctrl volume;
1082 if(!loc) return(-EINVAL);
1085 g.cylinders = dev->size / (128 * 32 * 512);
1086 g.start = get_start_sect(inode->i_bdev);
1087 return(copy_to_user(loc, &g, sizeof(g)) ? -EFAULT : 0);
1089 case HDIO_SET_UNMASKINTR:
1090 if(!capable(CAP_SYS_ADMIN)) return(-EACCES);
1091 if((arg > 1) || (inode->i_bdev->bd_contains != inode->i_bdev))
1095 case HDIO_GET_UNMASKINTR:
1096 if(!arg) return(-EINVAL);
1097 err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
1102 case HDIO_GET_MULTCOUNT:
1103 if(!arg) return(-EINVAL);
1104 err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
1109 case HDIO_SET_MULTCOUNT:
1110 if(!capable(CAP_SYS_ADMIN)) return(-EACCES);
1111 if(inode->i_bdev->bd_contains != inode->i_bdev)
1115 case HDIO_GET_IDENTITY:
1116 ubd_id.cyls = dev->size / (128 * 32 * 512);
1117 if(copy_to_user((char *) arg, (char *) &ubd_id,
1123 if(copy_from_user(&volume, (char *) arg, sizeof(volume)))
1125 volume.channel0 = 255;
1126 volume.channel1 = 255;
1127 volume.channel2 = 255;
1128 volume.channel3 = 255;
1129 if(copy_to_user((char *) arg, &volume, sizeof(volume)))
1136 static int ubd_check_remapped(int fd, unsigned long address, int is_write,
1137 __u64 offset, int is_user)
1139 __u64 bitmap_offset;
1140 unsigned long new_bitmap[2];
1143 /* This can only fix kernelspace faults */
1147 /* ubd-mmap is only enabled in skas mode */
1148 if(CHOOSE_MODE(1, 0))
1151 /* If it's not a write access, we can't do anything about it */
1155 /* We have a write */
1156 for(i = 0; i < sizeof(ubd_dev) / sizeof(ubd_dev[0]); i++){
1157 struct ubd *dev = &ubd_dev[i];
1159 if((dev->fd != fd) && (dev->cow.fd != fd))
1162 /* It's a write to a ubd device */
1164 if(!dev->openflags.w){
1165 /* It's a write access on a read-only device - probably
1166 * shouldn't happen. If the kernel is trying to change
1167 * something with no intention of writing it back out,
1168 * then this message will clue us in that this needs
1171 printk("Write access to mapped page from readonly ubd "
1176 /* It's a write to a writeable ubd device - it must be COWed
1177 * because, otherwise, the page would have been mapped in
1182 panic("Write fault on writeable non-COW ubd device %d",
1185 /* It should also be an access to the backing file since the
1186 * COW pages should be mapped in read-write
1190 panic("Write fault on a backing page of ubd "
1193 /* So, we do the write, copying the backing data to the COW
1197 err = os_seek_file(dev->fd, offset + dev->cow.data_offset);
1199 panic("Couldn't seek to %lld in COW file of ubd "
1200 "device %d, err = %d",
1201 offset + dev->cow.data_offset, i, -err);
1203 n = os_write_file(dev->fd, (void *) address, PAGE_SIZE);
1205 panic("Couldn't copy data to COW file of ubd "
1206 "device %d, err = %d", i, -n);
1208 /* ... updating the COW bitmap... */
1210 cowify_bitmap(offset, PAGE_SIZE, NULL, &bitmap_offset,
1211 dev->cow.bitmap, dev->cow.bitmap_offset,
1212 new_bitmap, dev->cow.bitmap_len);
1214 err = os_seek_file(dev->fd, bitmap_offset);
1216 panic("Couldn't seek to %lld in COW file of ubd "
1217 "device %d, err = %d", bitmap_offset, i, -err);
1219 n = os_write_file(dev->fd, new_bitmap, sizeof(new_bitmap));
1220 if(n != sizeof(new_bitmap))
1221 panic("Couldn't update bitmap of ubd device %d, "
1224 /* Maybe we can map the COW page in, and maybe we can't. If
1225 * it is a pre-V3 COW file, we can't, since the alignment will
1226 * be wrong. If it is a V3 or later COW file which has been
1227 * moved to a system with a larger page size, then maybe we
1228 * can't, depending on the exact location of the page.
1231 offset += dev->cow.data_offset;
1233 /* Remove the remapping, putting the original anonymous page
1234 * back. If the COW file can be mapped in, that is done.
1235 * Otherwise, the COW page is read in.
1238 if(!physmem_remove_mapping((void *) address))
1239 panic("Address 0x%lx not remapped by ubd device %d",
1241 if((offset % UBD_MMAP_BLOCK_SIZE) == 0)
1242 physmem_subst_mapping((void *) address, dev->fd,
1245 err = os_seek_file(dev->fd, offset);
1247 panic("Couldn't seek to %lld in COW file of "
1248 "ubd device %d, err = %d", offset, i,
1251 n = os_read_file(dev->fd, (void *) address, PAGE_SIZE);
1253 panic("Failed to read page from offset %llx of "
1254 "COW file of ubd device %d, err = %d",
1261 /* It's not a write on a ubd device */
1265 static struct remapper ubd_remapper = {
1266 .list = LIST_HEAD_INIT(ubd_remapper.list),
1267 .proc = ubd_check_remapped,
1270 static int ubd_remapper_setup(void)
1273 register_remapper(&ubd_remapper);
1278 __initcall(ubd_remapper_setup);
1281 * Overrides for Emacs so that we follow Linus's tabbing style.
1282 * Emacs will notice this stuff at the end of the file and automatically
1283 * adjust the settings for this buffer only. This must remain at the end
1285 * ---------------------------------------------------------------------------
1287 * c-file-style: "linux"