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 *),
787 "ubd : Failed to start I/O thread (errno = %d) - "
788 "falling back to synchronous I/O\n", -io_pid
);
792 err
= um_request_irq(UBD_IRQ
, thread_fd
, IRQ_READ
, ubd_intr
,
793 SA_INTERRUPT
, "ubd", ubd_dev
);
795 printk(KERN_ERR
"um_request_irq failed - errno = %d\n", -err
);
799 device_initcall(ubd_driver_init
);
801 static int ubd_open(struct inode
*inode
, struct file
*filp
)
803 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
804 struct ubd
*dev
= disk
->private_data
;
808 err
= ubd_open_dev(dev
);
810 printk(KERN_ERR
"%s: Can't open \"%s\": errno = %d\n",
811 disk
->disk_name
, dev
->file
, -err
);
816 if((filp
->f_mode
& FMODE_WRITE
) && !dev
->openflags
.w
){
817 if(--dev
->count
== 0) ubd_close(dev
);
824 static int ubd_release(struct inode
* inode
, struct file
* file
)
826 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
827 struct ubd
*dev
= disk
->private_data
;
829 if(--dev
->count
== 0)
834 static void cowify_bitmap(__u64 io_offset
, int length
, unsigned long *cow_mask
,
835 __u64
*cow_offset
, unsigned long *bitmap
,
836 __u64 bitmap_offset
, unsigned long *bitmap_words
,
839 __u64 sector
= io_offset
>> 9;
840 int i
, update_bitmap
= 0;
842 for(i
= 0; i
< length
>> 9; i
++){
844 ubd_set_bit(i
, (unsigned char *) cow_mask
);
845 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
849 ubd_set_bit(sector
+ i
, (unsigned char *) bitmap
);
855 *cow_offset
= sector
/ (sizeof(unsigned long) * 8);
857 /* This takes care of the case where we're exactly at the end of the
858 * device, and *cow_offset + 1 is off the end. So, just back it up
859 * by one word. Thanks to Lynn Kerby for the fix and James McMechan
860 * for the original diagnosis.
862 if(*cow_offset
== ((bitmap_len
+ sizeof(unsigned long) - 1) /
863 sizeof(unsigned long) - 1))
866 bitmap_words
[0] = bitmap
[*cow_offset
];
867 bitmap_words
[1] = bitmap
[*cow_offset
+ 1];
869 *cow_offset
*= sizeof(unsigned long);
870 *cow_offset
+= bitmap_offset
;
873 static void cowify_req(struct io_thread_req
*req
, unsigned long *bitmap
,
874 __u64 bitmap_offset
, __u64 bitmap_len
)
876 __u64 sector
= req
->offset
>> 9;
879 if(req
->length
> (sizeof(req
->sector_mask
) * 8) << 9)
880 panic("Operation too long");
882 if(req
->op
== UBD_READ
) {
883 for(i
= 0; i
< req
->length
>> 9; i
++){
884 if(ubd_test_bit(sector
+ i
, (unsigned char *) bitmap
))
885 ubd_set_bit(i
, (unsigned char *)
889 else cowify_bitmap(req
->offset
, req
->length
, &req
->sector_mask
,
890 &req
->cow_offset
, bitmap
, bitmap_offset
,
891 req
->bitmap_words
, bitmap_len
);
894 static int mmap_fd(struct request
*req
, struct ubd
*dev
, __u64 offset
)
897 unsigned char *bitmap
;
900 /* mmap must have been requested on the command line */
904 /* The buffer must be page aligned */
905 if(((unsigned long) req
->buffer
% UBD_MMAP_BLOCK_SIZE
) != 0)
908 /* The request must be a page long */
909 if((req
->current_nr_sectors
<< 9) != PAGE_SIZE
)
912 if(dev
->cow
.file
== NULL
)
915 sector
= offset
>> 9;
916 bitmap
= (unsigned char *) dev
->cow
.bitmap
;
917 bit
= ubd_test_bit(sector
, bitmap
);
919 for(i
= 1; i
< req
->current_nr_sectors
; i
++){
920 if(ubd_test_bit(sector
+ i
, bitmap
) != bit
)
924 if(bit
|| (rq_data_dir(req
) == WRITE
))
925 offset
+= dev
->cow
.data_offset
;
927 /* The data on disk must be page aligned */
928 if((offset
% UBD_MMAP_BLOCK_SIZE
) != 0)
931 return(bit
? dev
->fd
: dev
->cow
.fd
);
934 static int prepare_mmap_request(struct ubd
*dev
, int fd
, __u64 offset
,
936 struct io_thread_req
*io_req
)
940 if(rq_data_dir(req
) == WRITE
){
941 /* Writes are almost no-ops since the new data is already in the
945 if(dev
->cow
.file
!= NULL
)
946 cowify_bitmap(io_req
->offset
, io_req
->length
,
947 &io_req
->sector_mask
, &io_req
->cow_offset
,
948 dev
->cow
.bitmap
, dev
->cow
.bitmap_offset
,
949 io_req
->bitmap_words
,
950 dev
->cow
.bitmap_len
);
955 if((dev
->cow
.file
!= NULL
) && (fd
== dev
->cow
.fd
))
957 else w
= dev
->openflags
.w
;
959 if((dev
->cow
.file
!= NULL
) && (fd
== dev
->fd
))
960 offset
+= dev
->cow
.data_offset
;
962 err
= physmem_subst_mapping(req
->buffer
, fd
, offset
, w
);
964 printk("physmem_subst_mapping failed, err = %d\n",
970 io_req
->op
= UBD_MMAP
;
971 io_req
->buffer
= req
->buffer
;
975 /* Called with ubd_io_lock held */
976 static int prepare_request(struct request
*req
, struct io_thread_req
*io_req
)
978 struct gendisk
*disk
= req
->rq_disk
;
979 struct ubd
*dev
= disk
->private_data
;
983 if(req
->rq_status
== RQ_INACTIVE
) return(1);
985 if((rq_data_dir(req
) == WRITE
) && !dev
->openflags
.w
){
986 printk("Write attempted on readonly ubd device %s\n",
992 offset
= ((__u64
) req
->sector
) << 9;
993 len
= req
->current_nr_sectors
<< 9;
995 io_req
->fds
[0] = (dev
->cow
.file
!= NULL
) ? dev
->cow
.fd
: dev
->fd
;
996 io_req
->fds
[1] = dev
->fd
;
998 io_req
->cow_offset
= -1;
999 io_req
->offset
= offset
;
1000 io_req
->length
= len
;
1002 io_req
->sector_mask
= 0;
1004 fd
= mmap_fd(req
, dev
, io_req
->offset
);
1006 /* If mmapping is otherwise OK, but the first access to the
1007 * page is a write, then it's not mapped in yet. So we have
1008 * to write the data to disk first, then we can map the disk
1009 * page in and continue normally from there.
1011 if((rq_data_dir(req
) == WRITE
) && !is_remapped(req
->buffer
)){
1012 io_req
->map_fd
= dev
->fd
;
1013 io_req
->map_offset
= io_req
->offset
+
1014 dev
->cow
.data_offset
;
1017 else return(prepare_mmap_request(dev
, fd
, io_req
->offset
, req
,
1021 if(rq_data_dir(req
) == READ
)
1023 else dev
->nomap_writes
++;
1025 io_req
->op
= (rq_data_dir(req
) == READ
) ? UBD_READ
: UBD_WRITE
;
1026 io_req
->offsets
[0] = 0;
1027 io_req
->offsets
[1] = dev
->cow
.data_offset
;
1028 io_req
->buffer
= req
->buffer
;
1029 io_req
->sectorsize
= 1 << 9;
1031 if(dev
->cow
.file
!= NULL
)
1032 cowify_req(io_req
, dev
->cow
.bitmap
, dev
->cow
.bitmap_offset
,
1033 dev
->cow
.bitmap_len
);
1038 /* Called with ubd_io_lock held */
1039 static void do_ubd_request(request_queue_t
*q
)
1041 struct io_thread_req io_req
;
1042 struct request
*req
;
1045 if(thread_fd
== -1){
1046 while((req
= elv_next_request(q
)) != NULL
){
1047 err
= prepare_request(req
, &io_req
);
1050 __ubd_finish(req
, io_req
.error
);
1055 if(do_ubd
|| (req
= elv_next_request(q
)) == NULL
)
1057 err
= prepare_request(req
, &io_req
);
1059 do_ubd
= ubd_handler
;
1060 n
= write_ubd_fs(thread_fd
, (char *) &io_req
,
1062 if(n
!= sizeof(io_req
))
1063 printk("write to io thread failed, "
1064 "errno = %d\n", -n
);
1069 static int ubd_ioctl(struct inode
* inode
, struct file
* file
,
1070 unsigned int cmd
, unsigned long arg
)
1072 struct hd_geometry
*loc
= (struct hd_geometry
*) arg
;
1073 struct ubd
*dev
= inode
->i_bdev
->bd_disk
->private_data
;
1074 struct hd_driveid ubd_id
= {
1081 struct hd_geometry g
;
1082 struct cdrom_volctrl volume
;
1084 if(!loc
) return(-EINVAL
);
1087 g
.cylinders
= dev
->size
/ (128 * 32 * 512);
1088 g
.start
= get_start_sect(inode
->i_bdev
);
1089 return(copy_to_user(loc
, &g
, sizeof(g
)) ? -EFAULT
: 0);
1091 case HDIO_GET_IDENTITY
:
1092 ubd_id
.cyls
= dev
->size
/ (128 * 32 * 512);
1093 if(copy_to_user((char *) arg
, (char *) &ubd_id
,
1099 if(copy_from_user(&volume
, (char *) arg
, sizeof(volume
)))
1101 volume
.channel0
= 255;
1102 volume
.channel1
= 255;
1103 volume
.channel2
= 255;
1104 volume
.channel3
= 255;
1105 if(copy_to_user((char *) arg
, &volume
, sizeof(volume
)))
1112 static int ubd_check_remapped(int fd
, unsigned long address
, int is_write
,
1115 __u64 bitmap_offset
;
1116 unsigned long new_bitmap
[2];
1119 /* If it's not a write access, we can't do anything about it */
1123 /* We have a write */
1124 for(i
= 0; i
< sizeof(ubd_dev
) / sizeof(ubd_dev
[0]); i
++){
1125 struct ubd
*dev
= &ubd_dev
[i
];
1127 if((dev
->fd
!= fd
) && (dev
->cow
.fd
!= fd
))
1130 /* It's a write to a ubd device */
1132 if(!dev
->openflags
.w
){
1133 /* It's a write access on a read-only device - probably
1134 * shouldn't happen. If the kernel is trying to change
1135 * something with no intention of writing it back out,
1136 * then this message will clue us in that this needs
1139 printk("Write access to mapped page from readonly ubd "
1144 /* It's a write to a writeable ubd device - it must be COWed
1145 * because, otherwise, the page would have been mapped in
1150 panic("Write fault on writeable non-COW ubd device %d",
1153 /* It should also be an access to the backing file since the
1154 * COW pages should be mapped in read-write
1158 panic("Write fault on a backing page of ubd "
1161 /* So, we do the write, copying the backing data to the COW
1165 err
= os_seek_file(dev
->fd
, offset
+ dev
->cow
.data_offset
);
1167 panic("Couldn't seek to %lld in COW file of ubd "
1168 "device %d, err = %d",
1169 offset
+ dev
->cow
.data_offset
, i
, -err
);
1171 n
= os_write_file(dev
->fd
, (void *) address
, PAGE_SIZE
);
1173 panic("Couldn't copy data to COW file of ubd "
1174 "device %d, err = %d", i
, -n
);
1176 /* ... updating the COW bitmap... */
1178 cowify_bitmap(offset
, PAGE_SIZE
, NULL
, &bitmap_offset
,
1179 dev
->cow
.bitmap
, dev
->cow
.bitmap_offset
,
1180 new_bitmap
, dev
->cow
.bitmap_len
);
1182 err
= os_seek_file(dev
->fd
, bitmap_offset
);
1184 panic("Couldn't seek to %lld in COW file of ubd "
1185 "device %d, err = %d", bitmap_offset
, i
, -err
);
1187 n
= os_write_file(dev
->fd
, new_bitmap
, sizeof(new_bitmap
));
1188 if(n
!= sizeof(new_bitmap
))
1189 panic("Couldn't update bitmap of ubd device %d, "
1192 /* Maybe we can map the COW page in, and maybe we can't. If
1193 * it is a pre-V3 COW file, we can't, since the alignment will
1194 * be wrong. If it is a V3 or later COW file which has been
1195 * moved to a system with a larger page size, then maybe we
1196 * can't, depending on the exact location of the page.
1199 offset
+= dev
->cow
.data_offset
;
1201 /* Remove the remapping, putting the original anonymous page
1202 * back. If the COW file can be mapped in, that is done.
1203 * Otherwise, the COW page is read in.
1206 if(!physmem_remove_mapping((void *) address
))
1207 panic("Address 0x%lx not remapped by ubd device %d",
1209 if((offset
% UBD_MMAP_BLOCK_SIZE
) == 0)
1210 physmem_subst_mapping((void *) address
, dev
->fd
,
1213 err
= os_seek_file(dev
->fd
, offset
);
1215 panic("Couldn't seek to %lld in COW file of "
1216 "ubd device %d, err = %d", offset
, i
,
1219 n
= os_read_file(dev
->fd
, (void *) address
, PAGE_SIZE
);
1221 panic("Failed to read page from offset %llx of "
1222 "COW file of ubd device %d, err = %d",
1229 /* It's not a write on a ubd device */
1233 static struct remapper ubd_remapper
= {
1234 .list
= LIST_HEAD_INIT(ubd_remapper
.list
),
1235 .proc
= ubd_check_remapped
,
1238 static int ubd_remapper_setup(void)
1241 register_remapper(&ubd_remapper
);
1246 __initcall(ubd_remapper_setup
);
1249 * Overrides for Emacs so that we follow Linus's tabbing style.
1250 * Emacs will notice this stuff at the end of the file and automatically
1251 * adjust the settings for this buffer only. This must remain at the end
1253 * ---------------------------------------------------------------------------
1255 * c-file-style: "linux"