Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 2000 Jeff Dike (jdike@karaya.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 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 |
| 18 | */ |
| 19 | |
| 20 | #define MAJOR_NR UBD_MAJOR |
| 21 | #define UBD_SHIFT 4 |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "linux/module.h" |
| 24 | #include "linux/blkdev.h" |
| 25 | #include "linux/hdreg.h" |
| 26 | #include "linux/init.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "linux/cdrom.h" |
| 28 | #include "linux/proc_fs.h" |
| 29 | #include "linux/ctype.h" |
| 30 | #include "linux/capability.h" |
| 31 | #include "linux/mm.h" |
| 32 | #include "linux/vmalloc.h" |
| 33 | #include "linux/blkpg.h" |
| 34 | #include "linux/genhd.h" |
| 35 | #include "linux/spinlock.h" |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 36 | #include "linux/platform_device.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "asm/segment.h" |
| 38 | #include "asm/uaccess.h" |
| 39 | #include "asm/irq.h" |
| 40 | #include "asm/types.h" |
| 41 | #include "asm/tlbflush.h" |
| 42 | #include "user_util.h" |
| 43 | #include "mem_user.h" |
| 44 | #include "kern_util.h" |
| 45 | #include "kern.h" |
| 46 | #include "mconsole_kern.h" |
| 47 | #include "init.h" |
| 48 | #include "irq_user.h" |
| 49 | #include "irq_kern.h" |
| 50 | #include "ubd_user.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include "os.h" |
| 52 | #include "mem.h" |
| 53 | #include "mem_kern.h" |
| 54 | #include "cow.h" |
| 55 | |
Jeff Dike | 7b9014c | 2005-05-20 13:59:11 -0700 | [diff] [blame] | 56 | enum ubd_req { UBD_READ, UBD_WRITE }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | struct io_thread_req { |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 59 | enum ubd_req op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | int fds[2]; |
| 61 | unsigned long offsets[2]; |
| 62 | unsigned long long offset; |
| 63 | unsigned long length; |
| 64 | char *buffer; |
| 65 | int sectorsize; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 66 | unsigned long sector_mask; |
| 67 | unsigned long long cow_offset; |
| 68 | unsigned long bitmap_words[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | int error; |
| 70 | }; |
| 71 | |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 72 | extern int open_ubd_file(char *file, struct openflags *openflags, int shared, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | char **backing_file_out, int *bitmap_offset_out, |
| 74 | unsigned long *bitmap_len_out, int *data_offset_out, |
| 75 | int *create_cow_out); |
| 76 | extern int create_cow_file(char *cow_file, char *backing_file, |
| 77 | struct openflags flags, int sectorsize, |
| 78 | int alignment, int *bitmap_offset_out, |
| 79 | unsigned long *bitmap_len_out, |
| 80 | int *data_offset_out); |
| 81 | extern int read_cow_bitmap(int fd, void *buf, int offset, int len); |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 82 | extern void do_io(struct io_thread_req *req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 84 | static inline int ubd_test_bit(__u64 bit, unsigned char *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | __u64 n; |
| 87 | int bits, off; |
| 88 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 89 | bits = sizeof(data[0]) * 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | n = bit / bits; |
| 91 | off = bit % bits; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 92 | return((data[n] & (1 << off)) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 95 | static inline void ubd_set_bit(__u64 bit, unsigned char *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
| 97 | __u64 n; |
| 98 | int bits, off; |
| 99 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 100 | bits = sizeof(data[0]) * 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | n = bit / bits; |
| 102 | off = bit % bits; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 103 | data[n] |= (1 << off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | /*End stuff from ubd_user.h*/ |
| 106 | |
| 107 | #define DRIVER_NAME "uml-blkdev" |
| 108 | |
Paolo 'Blaisorblade' Giarrusso | 33f775e | 2006-10-30 22:07:08 -0800 | [diff] [blame] | 109 | /* Can be taken in interrupt context, and is passed to the block layer to lock |
| 110 | * the request queue. Kernel side code knows that. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | static DEFINE_SPINLOCK(ubd_io_lock); |
Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 112 | |
| 113 | static DEFINE_MUTEX(ubd_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Paolo 'Blaisorblade' Giarrusso | 2fe30a3 | 2006-10-30 22:07:09 -0800 | [diff] [blame^] | 115 | /* XXX - this made sense in 2.4 days, now it's only used as a boolean, and |
| 116 | * probably it doesn't make sense even for that. */ |
| 117 | static int do_ubd; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | static int ubd_open(struct inode * inode, struct file * filp); |
| 120 | static int ubd_release(struct inode * inode, struct file * file); |
| 121 | static int ubd_ioctl(struct inode * inode, struct file * file, |
| 122 | unsigned int cmd, unsigned long arg); |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 123 | static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Paolo 'Blaisorblade' Giarrusso | 97d88ac | 2006-10-30 22:07:03 -0800 | [diff] [blame] | 125 | #define MAX_DEV (16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | static struct block_device_operations ubd_blops = { |
| 128 | .owner = THIS_MODULE, |
| 129 | .open = ubd_open, |
| 130 | .release = ubd_release, |
| 131 | .ioctl = ubd_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 132 | .getgeo = ubd_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | /* Protected by the queue_lock */ |
| 136 | static request_queue_t *ubd_queue; |
| 137 | |
| 138 | /* Protected by ubd_lock */ |
| 139 | static int fake_major = MAJOR_NR; |
| 140 | |
| 141 | static struct gendisk *ubd_gendisk[MAX_DEV]; |
| 142 | static struct gendisk *fake_gendisk[MAX_DEV]; |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | #ifdef CONFIG_BLK_DEV_UBD_SYNC |
| 145 | #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \ |
| 146 | .cl = 1 }) |
| 147 | #else |
| 148 | #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \ |
| 149 | .cl = 1 }) |
| 150 | #endif |
| 151 | |
| 152 | /* Not protected - changed only in ubd_setup_common and then only to |
| 153 | * to enable O_SYNC. |
| 154 | */ |
| 155 | static struct openflags global_openflags = OPEN_FLAGS; |
| 156 | |
| 157 | struct cow { |
Paolo 'Blaisorblade' Giarrusso | 2a9d32f | 2006-10-30 22:07:04 -0800 | [diff] [blame] | 158 | /* backing file name */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | char *file; |
Paolo 'Blaisorblade' Giarrusso | 2a9d32f | 2006-10-30 22:07:04 -0800 | [diff] [blame] | 160 | /* backing file fd */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | int fd; |
| 162 | unsigned long *bitmap; |
| 163 | unsigned long bitmap_len; |
| 164 | int bitmap_offset; |
| 165 | int data_offset; |
| 166 | }; |
| 167 | |
| 168 | struct ubd { |
Paolo 'Blaisorblade' Giarrusso | 2a9d32f | 2006-10-30 22:07:04 -0800 | [diff] [blame] | 169 | /* name (and fd, below) of the file opened for writing, either the |
| 170 | * backing or the cow file. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | char *file; |
| 172 | int count; |
| 173 | int fd; |
| 174 | __u64 size; |
| 175 | struct openflags boot_openflags; |
| 176 | struct openflags openflags; |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 177 | int shared; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | int no_cow; |
| 179 | struct cow cow; |
| 180 | struct platform_device pdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | #define DEFAULT_COW { \ |
| 184 | .file = NULL, \ |
| 185 | .fd = -1, \ |
| 186 | .bitmap = NULL, \ |
| 187 | .bitmap_offset = 0, \ |
| 188 | .data_offset = 0, \ |
| 189 | } |
| 190 | |
| 191 | #define DEFAULT_UBD { \ |
| 192 | .file = NULL, \ |
| 193 | .count = 0, \ |
| 194 | .fd = -1, \ |
| 195 | .size = -1, \ |
| 196 | .boot_openflags = OPEN_FLAGS, \ |
| 197 | .openflags = OPEN_FLAGS, \ |
| 198 | .no_cow = 0, \ |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 199 | .shared = 0, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | .cow = DEFAULT_COW, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 203 | struct ubd ubd_devs[MAX_DEV] = { [ 0 ... MAX_DEV - 1 ] = DEFAULT_UBD }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | static int ubd0_init(void) |
| 206 | { |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 207 | struct ubd *ubd_dev = &ubd_devs[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 209 | if(ubd_dev->file == NULL) |
| 210 | ubd_dev->file = "root_fs"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | return(0); |
| 212 | } |
| 213 | |
| 214 | __initcall(ubd0_init); |
| 215 | |
| 216 | /* Only changed by fake_ide_setup which is a setup */ |
| 217 | static int fake_ide = 0; |
| 218 | static struct proc_dir_entry *proc_ide_root = NULL; |
| 219 | static struct proc_dir_entry *proc_ide = NULL; |
| 220 | |
| 221 | static void make_proc_ide(void) |
| 222 | { |
| 223 | proc_ide_root = proc_mkdir("ide", NULL); |
| 224 | proc_ide = proc_mkdir("ide0", proc_ide_root); |
| 225 | } |
| 226 | |
| 227 | static int proc_ide_read_media(char *page, char **start, off_t off, int count, |
| 228 | int *eof, void *data) |
| 229 | { |
| 230 | int len; |
| 231 | |
| 232 | strcpy(page, "disk\n"); |
| 233 | len = strlen("disk\n"); |
| 234 | len -= off; |
| 235 | if (len < count){ |
| 236 | *eof = 1; |
| 237 | if (len <= 0) return 0; |
| 238 | } |
| 239 | else len = count; |
| 240 | *start = page + off; |
| 241 | return len; |
| 242 | } |
| 243 | |
| 244 | static void make_ide_entries(char *dev_name) |
| 245 | { |
| 246 | struct proc_dir_entry *dir, *ent; |
| 247 | char name[64]; |
| 248 | |
| 249 | if(proc_ide_root == NULL) make_proc_ide(); |
| 250 | |
| 251 | dir = proc_mkdir(dev_name, proc_ide); |
| 252 | if(!dir) return; |
| 253 | |
| 254 | ent = create_proc_entry("media", S_IFREG|S_IRUGO, dir); |
| 255 | if(!ent) return; |
| 256 | ent->nlink = 1; |
| 257 | ent->data = NULL; |
| 258 | ent->read_proc = proc_ide_read_media; |
| 259 | ent->write_proc = NULL; |
| 260 | sprintf(name,"ide0/%s", dev_name); |
| 261 | proc_symlink(dev_name, proc_ide_root, name); |
| 262 | } |
| 263 | |
| 264 | static int fake_ide_setup(char *str) |
| 265 | { |
| 266 | fake_ide = 1; |
| 267 | return(1); |
| 268 | } |
| 269 | |
| 270 | __setup("fake_ide", fake_ide_setup); |
| 271 | |
| 272 | __uml_help(fake_ide_setup, |
| 273 | "fake_ide\n" |
| 274 | " Create ide0 entries that map onto ubd devices.\n\n" |
| 275 | ); |
| 276 | |
| 277 | static int parse_unit(char **ptr) |
| 278 | { |
| 279 | char *str = *ptr, *end; |
| 280 | int n = -1; |
| 281 | |
| 282 | if(isdigit(*str)) { |
| 283 | n = simple_strtoul(str, &end, 0); |
| 284 | if(end == str) |
| 285 | return(-1); |
| 286 | *ptr = end; |
| 287 | } |
Paolo 'Blaisorblade' Giarrusso | 97d88ac | 2006-10-30 22:07:03 -0800 | [diff] [blame] | 288 | else if (('a' <= *str) && (*str <= 'z')) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | n = *str - 'a'; |
| 290 | str++; |
| 291 | *ptr = str; |
| 292 | } |
| 293 | return(n); |
| 294 | } |
| 295 | |
| 296 | static int ubd_setup_common(char *str, int *index_out) |
| 297 | { |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 298 | struct ubd *ubd_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | struct openflags flags = global_openflags; |
| 300 | char *backing_file; |
| 301 | int n, err, i; |
| 302 | |
| 303 | if(index_out) *index_out = -1; |
| 304 | n = *str; |
| 305 | if(n == '='){ |
| 306 | char *end; |
| 307 | int major; |
| 308 | |
| 309 | str++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | if(!strcmp(str, "sync")){ |
| 311 | global_openflags = of_sync(global_openflags); |
| 312 | return(0); |
| 313 | } |
| 314 | major = simple_strtoul(str, &end, 0); |
| 315 | if((*end != '\0') || (end == str)){ |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 316 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | "ubd_setup : didn't parse major number\n"); |
| 318 | return(1); |
| 319 | } |
| 320 | |
| 321 | err = 1; |
Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 322 | mutex_lock(&ubd_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | if(fake_major != MAJOR_NR){ |
| 324 | printk(KERN_ERR "Can't assign a fake major twice\n"); |
| 325 | goto out1; |
| 326 | } |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | fake_major = major; |
| 329 | |
| 330 | printk(KERN_INFO "Setting extra ubd major number to %d\n", |
| 331 | major); |
| 332 | err = 0; |
| 333 | out1: |
Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 334 | mutex_unlock(&ubd_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | return(err); |
| 336 | } |
| 337 | |
| 338 | n = parse_unit(&str); |
| 339 | if(n < 0){ |
| 340 | printk(KERN_ERR "ubd_setup : couldn't parse unit number " |
| 341 | "'%s'\n", str); |
| 342 | return(1); |
| 343 | } |
| 344 | if(n >= MAX_DEV){ |
| 345 | printk(KERN_ERR "ubd_setup : index %d out of range " |
| 346 | "(%d devices, from 0 to %d)\n", n, MAX_DEV, MAX_DEV - 1); |
| 347 | return(1); |
| 348 | } |
| 349 | |
| 350 | err = 1; |
Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 351 | mutex_lock(&ubd_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 353 | ubd_dev = &ubd_devs[n]; |
| 354 | if(ubd_dev->file != NULL){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | printk(KERN_ERR "ubd_setup : device already configured\n"); |
| 356 | goto out; |
| 357 | } |
| 358 | |
| 359 | if (index_out) |
| 360 | *index_out = n; |
| 361 | |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 362 | for (i = 0; i < sizeof("rscd="); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | switch (*str) { |
| 364 | case 'r': |
| 365 | flags.w = 0; |
| 366 | break; |
| 367 | case 's': |
| 368 | flags.s = 1; |
| 369 | break; |
| 370 | case 'd': |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 371 | ubd_dev->no_cow = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | break; |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 373 | case 'c': |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 374 | ubd_dev->shared = 1; |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 375 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | case '=': |
| 377 | str++; |
| 378 | goto break_loop; |
| 379 | default: |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 380 | printk(KERN_ERR "ubd_setup : Expected '=' or flag letter (r, s, c, or d)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | goto out; |
| 382 | } |
| 383 | str++; |
| 384 | } |
| 385 | |
| 386 | if (*str == '=') |
| 387 | printk(KERN_ERR "ubd_setup : Too many flags specified\n"); |
| 388 | else |
| 389 | printk(KERN_ERR "ubd_setup : Expected '='\n"); |
| 390 | goto out; |
| 391 | |
| 392 | break_loop: |
| 393 | err = 0; |
| 394 | backing_file = strchr(str, ','); |
| 395 | |
| 396 | if (!backing_file) { |
| 397 | backing_file = strchr(str, ':'); |
| 398 | } |
| 399 | |
| 400 | if(backing_file){ |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 401 | if(ubd_dev->no_cow) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | printk(KERN_ERR "Can't specify both 'd' and a " |
| 403 | "cow file\n"); |
| 404 | else { |
| 405 | *backing_file = '\0'; |
| 406 | backing_file++; |
| 407 | } |
| 408 | } |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 409 | ubd_dev->file = str; |
| 410 | ubd_dev->cow.file = backing_file; |
| 411 | ubd_dev->boot_openflags = flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | out: |
Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 413 | mutex_unlock(&ubd_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | return(err); |
| 415 | } |
| 416 | |
| 417 | static int ubd_setup(char *str) |
| 418 | { |
| 419 | ubd_setup_common(str, NULL); |
| 420 | return(1); |
| 421 | } |
| 422 | |
| 423 | __setup("ubd", ubd_setup); |
| 424 | __uml_help(ubd_setup, |
| 425 | "ubd<n><flags>=<filename>[(:|,)<filename2>]\n" |
| 426 | " This is used to associate a device with a file in the underlying\n" |
| 427 | " filesystem. When specifying two filenames, the first one is the\n" |
| 428 | " COW name and the second is the backing file name. As separator you can\n" |
| 429 | " use either a ':' or a ',': the first one allows writing things like;\n" |
| 430 | " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n" |
| 431 | " while with a ',' the shell would not expand the 2nd '~'.\n" |
| 432 | " When using only one filename, UML will detect whether to thread it like\n" |
| 433 | " a COW file or a backing file. To override this detection, add the 'd'\n" |
| 434 | " flag:\n" |
| 435 | " ubd0d=BackingFile\n" |
| 436 | " Usually, there is a filesystem in the file, but \n" |
| 437 | " that's not required. Swap devices containing swap files can be\n" |
| 438 | " specified like this. Also, a file which doesn't contain a\n" |
| 439 | " filesystem can have its contents read in the virtual \n" |
| 440 | " machine by running 'dd' on the device. <n> must be in the range\n" |
| 441 | " 0 to 7. Appending an 'r' to the number will cause that device\n" |
| 442 | " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n" |
| 443 | " an 's' will cause data to be written to disk on the host immediately.\n\n" |
| 444 | ); |
| 445 | |
| 446 | static int udb_setup(char *str) |
| 447 | { |
| 448 | printk("udb%s specified on command line is almost certainly a ubd -> " |
| 449 | "udb TYPO\n", str); |
| 450 | return(1); |
| 451 | } |
| 452 | |
| 453 | __setup("udb", udb_setup); |
| 454 | __uml_help(udb_setup, |
| 455 | "udb\n" |
Jeff Dike | 0894e27 | 2005-05-28 15:51:55 -0700 | [diff] [blame] | 456 | " This option is here solely to catch ubd -> udb typos, which can be\n" |
| 457 | " to impossible to catch visually unless you specifically look for\n" |
| 458 | " them. The only result of any option starting with 'udb' is an error\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | " in the boot output.\n\n" |
| 460 | ); |
| 461 | |
| 462 | static int fakehd_set = 0; |
| 463 | static int fakehd(char *str) |
| 464 | { |
| 465 | printk(KERN_INFO "fakehd : Changing ubd name to \"hd\".\n"); |
| 466 | fakehd_set = 1; |
| 467 | return 1; |
| 468 | } |
| 469 | |
| 470 | __setup("fakehd", fakehd); |
| 471 | __uml_help(fakehd, |
| 472 | "fakehd\n" |
| 473 | " Change the ubd device name to \"hd\".\n\n" |
| 474 | ); |
| 475 | |
| 476 | static void do_ubd_request(request_queue_t * q); |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 477 | |
| 478 | /* Only changed by ubd_init, which is an initcall. */ |
| 479 | int thread_fd = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
| 481 | /* Changed by ubd_handler, which is serialized because interrupts only |
| 482 | * happen on CPU 0. |
| 483 | */ |
| 484 | int intr_count = 0; |
| 485 | |
| 486 | /* call ubd_finish if you need to serialize */ |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 487 | static void __ubd_finish(struct request *req, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 489 | int nsect; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 491 | if(error){ |
| 492 | end_request(req, 0); |
| 493 | return; |
| 494 | } |
| 495 | nsect = req->current_nr_sectors; |
| 496 | req->sector += nsect; |
| 497 | req->buffer += nsect << 9; |
| 498 | req->errors = 0; |
| 499 | req->nr_sectors -= nsect; |
| 500 | req->current_nr_sectors = 0; |
| 501 | end_request(req, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Paolo 'Blaisorblade' Giarrusso | 33f775e | 2006-10-30 22:07:08 -0800 | [diff] [blame] | 504 | /* Callable only from interrupt context - otherwise you need to do |
| 505 | * spin_lock_irq()/spin_lock_irqsave() */ |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 506 | static inline void ubd_finish(struct request *req, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | { |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 508 | spin_lock(&ubd_io_lock); |
| 509 | __ubd_finish(req, error); |
| 510 | spin_unlock(&ubd_io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Paolo 'Blaisorblade' Giarrusso | 2fe30a3 | 2006-10-30 22:07:09 -0800 | [diff] [blame^] | 513 | /* XXX - move this inside ubd_intr. */ |
Paolo 'Blaisorblade' Giarrusso | 33f775e | 2006-10-30 22:07:08 -0800 | [diff] [blame] | 514 | /* Called without ubd_io_lock held, and only in interrupt context. */ |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 515 | static void ubd_handler(void) |
| 516 | { |
| 517 | struct io_thread_req req; |
| 518 | struct request *rq = elv_next_request(ubd_queue); |
| 519 | int n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
Paolo 'Blaisorblade' Giarrusso | 2fe30a3 | 2006-10-30 22:07:09 -0800 | [diff] [blame^] | 521 | do_ubd = 0; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 522 | intr_count++; |
| 523 | n = os_read_file(thread_fd, &req, sizeof(req)); |
| 524 | if(n != sizeof(req)){ |
| 525 | printk(KERN_ERR "Pid %d - spurious interrupt in ubd_handler, " |
| 526 | "err = %d\n", os_getpid(), -n); |
| 527 | spin_lock(&ubd_io_lock); |
| 528 | end_request(rq, 0); |
| 529 | spin_unlock(&ubd_io_lock); |
| 530 | return; |
| 531 | } |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 532 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 533 | ubd_finish(rq, req.error); |
| 534 | reactivate_fd(thread_fd, UBD_IRQ); |
Paolo 'Blaisorblade' Giarrusso | 33f775e | 2006-10-30 22:07:08 -0800 | [diff] [blame] | 535 | spin_lock(&ubd_io_lock); |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 536 | do_ubd_request(ubd_queue); |
Paolo 'Blaisorblade' Giarrusso | 33f775e | 2006-10-30 22:07:08 -0800 | [diff] [blame] | 537 | spin_unlock(&ubd_io_lock); |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 538 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 540 | static irqreturn_t ubd_intr(int irq, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | { |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 542 | ubd_handler(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | return(IRQ_HANDLED); |
| 544 | } |
| 545 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 546 | /* Only changed by ubd_init, which is an initcall. */ |
| 547 | static int io_pid = -1; |
| 548 | |
| 549 | void kill_io_thread(void) |
| 550 | { |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 551 | if(io_pid != -1) |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 552 | os_kill_process(io_pid, 1); |
| 553 | } |
| 554 | |
| 555 | __uml_exitcall(kill_io_thread); |
| 556 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 557 | static int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | { |
| 559 | char *file; |
| 560 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 561 | file = ubd_dev->cow.file ? ubd_dev->cow.file : ubd_dev->file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | return(os_file_size(file, size_out)); |
| 563 | } |
| 564 | |
Paolo 'Blaisorblade' Giarrusso | 5f75a4f | 2006-10-30 22:07:06 -0800 | [diff] [blame] | 565 | static void ubd_close_dev(struct ubd *ubd_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | { |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 567 | os_close_file(ubd_dev->fd); |
| 568 | if(ubd_dev->cow.file == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | return; |
| 570 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 571 | os_close_file(ubd_dev->cow.fd); |
| 572 | vfree(ubd_dev->cow.bitmap); |
| 573 | ubd_dev->cow.bitmap = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 576 | static int ubd_open_dev(struct ubd *ubd_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | { |
| 578 | struct openflags flags; |
| 579 | char **back_ptr; |
| 580 | int err, create_cow, *create_ptr; |
| 581 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 582 | ubd_dev->openflags = ubd_dev->boot_openflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | create_cow = 0; |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 584 | create_ptr = (ubd_dev->cow.file != NULL) ? &create_cow : NULL; |
| 585 | back_ptr = ubd_dev->no_cow ? NULL : &ubd_dev->cow.file; |
| 586 | ubd_dev->fd = open_ubd_file(ubd_dev->file, &ubd_dev->openflags, ubd_dev->shared, |
| 587 | back_ptr, &ubd_dev->cow.bitmap_offset, |
| 588 | &ubd_dev->cow.bitmap_len, &ubd_dev->cow.data_offset, |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 589 | create_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 591 | if((ubd_dev->fd == -ENOENT) && create_cow){ |
| 592 | ubd_dev->fd = create_cow_file(ubd_dev->file, ubd_dev->cow.file, |
| 593 | ubd_dev->openflags, 1 << 9, PAGE_SIZE, |
| 594 | &ubd_dev->cow.bitmap_offset, |
| 595 | &ubd_dev->cow.bitmap_len, |
| 596 | &ubd_dev->cow.data_offset); |
| 597 | if(ubd_dev->fd >= 0){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | printk(KERN_INFO "Creating \"%s\" as COW file for " |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 599 | "\"%s\"\n", ubd_dev->file, ubd_dev->cow.file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 603 | if(ubd_dev->fd < 0){ |
| 604 | printk("Failed to open '%s', errno = %d\n", ubd_dev->file, |
| 605 | -ubd_dev->fd); |
| 606 | return(ubd_dev->fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 609 | if(ubd_dev->cow.file != NULL){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | err = -ENOMEM; |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 611 | ubd_dev->cow.bitmap = (void *) vmalloc(ubd_dev->cow.bitmap_len); |
| 612 | if(ubd_dev->cow.bitmap == NULL){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | printk(KERN_ERR "Failed to vmalloc COW bitmap\n"); |
| 614 | goto error; |
| 615 | } |
| 616 | flush_tlb_kernel_vm(); |
| 617 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 618 | err = read_cow_bitmap(ubd_dev->fd, ubd_dev->cow.bitmap, |
| 619 | ubd_dev->cow.bitmap_offset, |
| 620 | ubd_dev->cow.bitmap_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | if(err < 0) |
| 622 | goto error; |
| 623 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 624 | flags = ubd_dev->openflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | flags.w = 0; |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 626 | err = open_ubd_file(ubd_dev->cow.file, &flags, ubd_dev->shared, NULL, |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 627 | NULL, NULL, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | if(err < 0) goto error; |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 629 | ubd_dev->cow.fd = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | } |
| 631 | return(0); |
| 632 | error: |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 633 | os_close_file(ubd_dev->fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | return(err); |
| 635 | } |
| 636 | |
Paolo 'Blaisorblade' Giarrusso | 5f75a4f | 2006-10-30 22:07:06 -0800 | [diff] [blame] | 637 | static int ubd_disk_register(int major, u64 size, int unit, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | struct gendisk **disk_out) |
| 639 | |
| 640 | { |
| 641 | struct gendisk *disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
| 643 | disk = alloc_disk(1 << UBD_SHIFT); |
| 644 | if(disk == NULL) |
| 645 | return(-ENOMEM); |
| 646 | |
| 647 | disk->major = major; |
| 648 | disk->first_minor = unit << UBD_SHIFT; |
| 649 | disk->fops = &ubd_blops; |
| 650 | set_capacity(disk, size / 512); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 651 | if(major == MAJOR_NR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | sprintf(disk->disk_name, "ubd%c", 'a' + unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 653 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | sprintf(disk->disk_name, "ubd_fake%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
| 656 | /* sysfs register (not for ide fake devices) */ |
| 657 | if (major == MAJOR_NR) { |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 658 | ubd_devs[unit].pdev.id = unit; |
| 659 | ubd_devs[unit].pdev.name = DRIVER_NAME; |
| 660 | platform_device_register(&ubd_devs[unit].pdev); |
| 661 | disk->driverfs_dev = &ubd_devs[unit].pdev.dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 664 | disk->private_data = &ubd_devs[unit]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | disk->queue = ubd_queue; |
| 666 | add_disk(disk); |
| 667 | |
| 668 | *disk_out = disk; |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9)) |
| 673 | |
| 674 | static int ubd_add(int n) |
| 675 | { |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 676 | struct ubd *ubd_dev = &ubd_devs[n]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | int err; |
| 678 | |
Jeff Dike | ec7cf78 | 2005-09-03 15:57:29 -0700 | [diff] [blame] | 679 | err = -ENODEV; |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 680 | if(ubd_dev->file == NULL) |
Jeff Dike | ec7cf78 | 2005-09-03 15:57:29 -0700 | [diff] [blame] | 681 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 683 | err = ubd_file_size(ubd_dev, &ubd_dev->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | if(err < 0) |
Jeff Dike | 80c1374 | 2006-09-29 01:58:51 -0700 | [diff] [blame] | 685 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 687 | ubd_dev->size = ROUND_BLOCK(ubd_dev->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
Paolo 'Blaisorblade' Giarrusso | 5f75a4f | 2006-10-30 22:07:06 -0800 | [diff] [blame] | 689 | err = ubd_disk_register(MAJOR_NR, ubd_dev->size, n, &ubd_gendisk[n]); |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 690 | if(err) |
Jeff Dike | 80c1374 | 2006-09-29 01:58:51 -0700 | [diff] [blame] | 691 | goto out; |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | if(fake_major != MAJOR_NR) |
Paolo 'Blaisorblade' Giarrusso | 5f75a4f | 2006-10-30 22:07:06 -0800 | [diff] [blame] | 694 | ubd_disk_register(fake_major, ubd_dev->size, n, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | &fake_gendisk[n]); |
| 696 | |
| 697 | /* perhaps this should also be under the "if (fake_major)" above */ |
| 698 | /* using the fake_disk->disk_name and also the fakehd_set name */ |
| 699 | if (fake_ide) |
| 700 | make_ide_entries(ubd_gendisk[n]->disk_name); |
| 701 | |
Jeff Dike | ec7cf78 | 2005-09-03 15:57:29 -0700 | [diff] [blame] | 702 | err = 0; |
Jeff Dike | ec7cf78 | 2005-09-03 15:57:29 -0700 | [diff] [blame] | 703 | out: |
| 704 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | static int ubd_config(char *str) |
| 708 | { |
| 709 | int n, err; |
| 710 | |
Jeff Dike | 970d6e3 | 2006-01-06 00:18:48 -0800 | [diff] [blame] | 711 | str = kstrdup(str, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | if(str == NULL){ |
| 713 | printk(KERN_ERR "ubd_config failed to strdup string\n"); |
| 714 | return(1); |
| 715 | } |
| 716 | err = ubd_setup_common(str, &n); |
| 717 | if(err){ |
| 718 | kfree(str); |
| 719 | return(-1); |
| 720 | } |
| 721 | if(n == -1) return(0); |
| 722 | |
Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 723 | mutex_lock(&ubd_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | err = ubd_add(n); |
| 725 | if(err) |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 726 | ubd_devs[n].file = NULL; |
Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 727 | mutex_unlock(&ubd_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
| 729 | return(err); |
| 730 | } |
| 731 | |
| 732 | static int ubd_get_config(char *name, char *str, int size, char **error_out) |
| 733 | { |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 734 | struct ubd *ubd_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | int n, len = 0; |
| 736 | |
| 737 | n = parse_unit(&name); |
| 738 | if((n >= MAX_DEV) || (n < 0)){ |
| 739 | *error_out = "ubd_get_config : device number out of range"; |
| 740 | return(-1); |
| 741 | } |
| 742 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 743 | ubd_dev = &ubd_devs[n]; |
Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 744 | mutex_lock(&ubd_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 746 | if(ubd_dev->file == NULL){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | CONFIG_CHUNK(str, size, len, "", 1); |
| 748 | goto out; |
| 749 | } |
| 750 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 751 | CONFIG_CHUNK(str, size, len, ubd_dev->file, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 753 | if(ubd_dev->cow.file != NULL){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | CONFIG_CHUNK(str, size, len, ",", 0); |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 755 | CONFIG_CHUNK(str, size, len, ubd_dev->cow.file, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
| 757 | else CONFIG_CHUNK(str, size, len, "", 1); |
| 758 | |
| 759 | out: |
Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 760 | mutex_unlock(&ubd_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | return(len); |
| 762 | } |
| 763 | |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 764 | static int ubd_id(char **str, int *start_out, int *end_out) |
| 765 | { |
| 766 | int n; |
| 767 | |
| 768 | n = parse_unit(str); |
| 769 | *start_out = 0; |
| 770 | *end_out = MAX_DEV - 1; |
| 771 | return n; |
| 772 | } |
| 773 | |
| 774 | static int ubd_remove(int n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | { |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 776 | struct ubd *ubd_dev; |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 777 | int err = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 779 | mutex_lock(&ubd_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
| 781 | if(ubd_gendisk[n] == NULL) |
| 782 | goto out; |
| 783 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 784 | ubd_dev = &ubd_devs[n]; |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 785 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 786 | if(ubd_dev->file == NULL) |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 787 | goto out; |
| 788 | |
| 789 | /* you cannot remove a open disk */ |
| 790 | err = -EBUSY; |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 791 | if(ubd_dev->count > 0) |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 792 | goto out; |
| 793 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | del_gendisk(ubd_gendisk[n]); |
| 795 | put_disk(ubd_gendisk[n]); |
| 796 | ubd_gendisk[n] = NULL; |
| 797 | |
| 798 | if(fake_gendisk[n] != NULL){ |
| 799 | del_gendisk(fake_gendisk[n]); |
| 800 | put_disk(fake_gendisk[n]); |
| 801 | fake_gendisk[n] = NULL; |
| 802 | } |
| 803 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 804 | platform_device_unregister(&ubd_dev->pdev); |
| 805 | *ubd_dev = ((struct ubd) DEFAULT_UBD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | err = 0; |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 807 | out: |
Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 808 | mutex_unlock(&ubd_lock); |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 809 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | static struct mc_device ubd_mc = { |
| 813 | .name = "ubd", |
| 814 | .config = ubd_config, |
| 815 | .get_config = ubd_get_config, |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 816 | .id = ubd_id, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | .remove = ubd_remove, |
| 818 | }; |
| 819 | |
| 820 | static int ubd_mc_init(void) |
| 821 | { |
| 822 | mconsole_register_dev(&ubd_mc); |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | __initcall(ubd_mc_init); |
| 827 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 828 | static struct platform_driver ubd_driver = { |
| 829 | .driver = { |
| 830 | .name = DRIVER_NAME, |
| 831 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | }; |
| 833 | |
| 834 | int ubd_init(void) |
| 835 | { |
| 836 | int i; |
| 837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | if (register_blkdev(MAJOR_NR, "ubd")) |
| 839 | return -1; |
| 840 | |
| 841 | ubd_queue = blk_init_queue(do_ubd_request, &ubd_io_lock); |
| 842 | if (!ubd_queue) { |
| 843 | unregister_blkdev(MAJOR_NR, "ubd"); |
| 844 | return -1; |
| 845 | } |
| 846 | |
| 847 | if (fake_major != MAJOR_NR) { |
| 848 | char name[sizeof("ubd_nnn\0")]; |
| 849 | |
| 850 | snprintf(name, sizeof(name), "ubd_%d", fake_major); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | if (register_blkdev(fake_major, "ubd")) |
| 852 | return -1; |
| 853 | } |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 854 | platform_driver_register(&ubd_driver); |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 855 | for (i = 0; i < MAX_DEV; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | ubd_add(i); |
| 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | late_initcall(ubd_init); |
| 861 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 862 | int ubd_driver_init(void){ |
| 863 | unsigned long stack; |
| 864 | int err; |
| 865 | |
| 866 | /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/ |
| 867 | if(global_openflags.s){ |
| 868 | printk(KERN_INFO "ubd: Synchronous mode\n"); |
| 869 | /* Letting ubd=sync be like using ubd#s= instead of ubd#= is |
| 870 | * enough. So use anyway the io thread. */ |
| 871 | } |
| 872 | stack = alloc_stack(0, 0); |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 873 | io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *), |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 874 | &thread_fd); |
| 875 | if(io_pid < 0){ |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 876 | printk(KERN_ERR |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 877 | "ubd : Failed to start I/O thread (errno = %d) - " |
| 878 | "falling back to synchronous I/O\n", -io_pid); |
| 879 | io_pid = -1; |
| 880 | return(0); |
| 881 | } |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 882 | err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr, |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 883 | IRQF_DISABLED, "ubd", ubd_devs); |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 884 | if(err != 0) |
| 885 | printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err); |
Jeff Dike | f4c57a7 | 2006-03-31 02:30:10 -0800 | [diff] [blame] | 886 | return 0; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | device_initcall(ubd_driver_init); |
| 890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | static int ubd_open(struct inode *inode, struct file *filp) |
| 892 | { |
| 893 | struct gendisk *disk = inode->i_bdev->bd_disk; |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 894 | struct ubd *ubd_dev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | int err = 0; |
| 896 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 897 | if(ubd_dev->count == 0){ |
| 898 | err = ubd_open_dev(ubd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | if(err){ |
| 900 | printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n", |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 901 | disk->disk_name, ubd_dev->file, -err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | goto out; |
| 903 | } |
| 904 | } |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 905 | ubd_dev->count++; |
| 906 | set_disk_ro(disk, !ubd_dev->openflags.w); |
Paolo 'Blaisorblade' Giarrusso | 2c49be9 | 2005-05-01 08:58:57 -0700 | [diff] [blame] | 907 | |
| 908 | /* This should no more be needed. And it didn't work anyway to exclude |
| 909 | * read-write remounting of filesystems.*/ |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 910 | /*if((filp->f_mode & FMODE_WRITE) && !ubd_dev->openflags.w){ |
Paolo 'Blaisorblade' Giarrusso | 5f75a4f | 2006-10-30 22:07:06 -0800 | [diff] [blame] | 911 | if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | err = -EROFS; |
Paolo 'Blaisorblade' Giarrusso | 2c49be9 | 2005-05-01 08:58:57 -0700 | [diff] [blame] | 913 | }*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | out: |
| 915 | return(err); |
| 916 | } |
| 917 | |
| 918 | static int ubd_release(struct inode * inode, struct file * file) |
| 919 | { |
| 920 | struct gendisk *disk = inode->i_bdev->bd_disk; |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 921 | struct ubd *ubd_dev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 923 | if(--ubd_dev->count == 0) |
Paolo 'Blaisorblade' Giarrusso | 5f75a4f | 2006-10-30 22:07:06 -0800 | [diff] [blame] | 924 | ubd_close_dev(ubd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | return(0); |
| 926 | } |
| 927 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 928 | static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask, |
| 929 | __u64 *cow_offset, unsigned long *bitmap, |
| 930 | __u64 bitmap_offset, unsigned long *bitmap_words, |
| 931 | __u64 bitmap_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | { |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 933 | __u64 sector = io_offset >> 9; |
| 934 | int i, update_bitmap = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 936 | for(i = 0; i < length >> 9; i++){ |
| 937 | if(cow_mask != NULL) |
| 938 | ubd_set_bit(i, (unsigned char *) cow_mask); |
| 939 | if(ubd_test_bit(sector + i, (unsigned char *) bitmap)) |
| 940 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 942 | update_bitmap = 1; |
| 943 | ubd_set_bit(sector + i, (unsigned char *) bitmap); |
| 944 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 946 | if(!update_bitmap) |
| 947 | return; |
| 948 | |
| 949 | *cow_offset = sector / (sizeof(unsigned long) * 8); |
| 950 | |
| 951 | /* This takes care of the case where we're exactly at the end of the |
| 952 | * device, and *cow_offset + 1 is off the end. So, just back it up |
| 953 | * by one word. Thanks to Lynn Kerby for the fix and James McMechan |
| 954 | * for the original diagnosis. |
| 955 | */ |
| 956 | if(*cow_offset == ((bitmap_len + sizeof(unsigned long) - 1) / |
| 957 | sizeof(unsigned long) - 1)) |
| 958 | (*cow_offset)--; |
| 959 | |
| 960 | bitmap_words[0] = bitmap[*cow_offset]; |
| 961 | bitmap_words[1] = bitmap[*cow_offset + 1]; |
| 962 | |
| 963 | *cow_offset *= sizeof(unsigned long); |
| 964 | *cow_offset += bitmap_offset; |
| 965 | } |
| 966 | |
| 967 | static void cowify_req(struct io_thread_req *req, unsigned long *bitmap, |
| 968 | __u64 bitmap_offset, __u64 bitmap_len) |
| 969 | { |
| 970 | __u64 sector = req->offset >> 9; |
| 971 | int i; |
| 972 | |
| 973 | if(req->length > (sizeof(req->sector_mask) * 8) << 9) |
| 974 | panic("Operation too long"); |
| 975 | |
| 976 | if(req->op == UBD_READ) { |
| 977 | for(i = 0; i < req->length >> 9; i++){ |
| 978 | if(ubd_test_bit(sector + i, (unsigned char *) bitmap)) |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 979 | ubd_set_bit(i, (unsigned char *) |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 980 | &req->sector_mask); |
| 981 | } |
| 982 | } |
| 983 | else cowify_bitmap(req->offset, req->length, &req->sector_mask, |
| 984 | &req->cow_offset, bitmap, bitmap_offset, |
| 985 | req->bitmap_words, bitmap_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | } |
| 987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | /* Called with ubd_io_lock held */ |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 989 | static int prepare_request(struct request *req, struct io_thread_req *io_req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | { |
| 991 | struct gendisk *disk = req->rq_disk; |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 992 | struct ubd *ubd_dev = disk->private_data; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 993 | __u64 offset; |
| 994 | int len; |
| 995 | |
Paolo 'Blaisorblade' Giarrusso | 2c49be9 | 2005-05-01 08:58:57 -0700 | [diff] [blame] | 996 | /* This should be impossible now */ |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 997 | if((rq_data_dir(req) == WRITE) && !ubd_dev->openflags.w){ |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 998 | printk("Write attempted on readonly ubd device %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | disk->disk_name); |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1000 | end_request(req, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | return(1); |
| 1002 | } |
| 1003 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1004 | offset = ((__u64) req->sector) << 9; |
| 1005 | len = req->current_nr_sectors << 9; |
| 1006 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1007 | io_req->fds[0] = (ubd_dev->cow.file != NULL) ? ubd_dev->cow.fd : ubd_dev->fd; |
| 1008 | io_req->fds[1] = ubd_dev->fd; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1009 | io_req->cow_offset = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | io_req->offset = offset; |
| 1011 | io_req->length = len; |
| 1012 | io_req->error = 0; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1013 | io_req->sector_mask = 0; |
| 1014 | |
| 1015 | io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | io_req->offsets[0] = 0; |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1017 | io_req->offsets[1] = ubd_dev->cow.data_offset; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1018 | io_req->buffer = req->buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | io_req->sectorsize = 1 << 9; |
| 1020 | |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1021 | if(ubd_dev->cow.file != NULL) |
| 1022 | cowify_req(io_req, ubd_dev->cow.bitmap, ubd_dev->cow.bitmap_offset, |
| 1023 | ubd_dev->cow.bitmap_len); |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | return(0); |
| 1026 | } |
| 1027 | |
| 1028 | /* Called with ubd_io_lock held */ |
| 1029 | static void do_ubd_request(request_queue_t *q) |
| 1030 | { |
| 1031 | struct io_thread_req io_req; |
| 1032 | struct request *req; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1033 | int err, n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1035 | if(thread_fd == -1){ |
| 1036 | while((req = elv_next_request(q)) != NULL){ |
| 1037 | err = prepare_request(req, &io_req); |
| 1038 | if(!err){ |
| 1039 | do_io(&io_req); |
| 1040 | __ubd_finish(req, io_req.error); |
| 1041 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | } |
| 1043 | } |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1044 | else { |
| 1045 | if(do_ubd || (req = elv_next_request(q)) == NULL) |
| 1046 | return; |
| 1047 | err = prepare_request(req, &io_req); |
| 1048 | if(!err){ |
Paolo 'Blaisorblade' Giarrusso | 2fe30a3 | 2006-10-30 22:07:09 -0800 | [diff] [blame^] | 1049 | do_ubd = 1; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1050 | n = os_write_file(thread_fd, (char *) &io_req, |
| 1051 | sizeof(io_req)); |
| 1052 | if(n != sizeof(io_req)) |
| 1053 | printk("write to io thread failed, " |
| 1054 | "errno = %d\n", -n); |
| 1055 | } |
| 1056 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 1059 | static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 1060 | { |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1061 | struct ubd *ubd_dev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 1062 | |
| 1063 | geo->heads = 128; |
| 1064 | geo->sectors = 32; |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1065 | geo->cylinders = ubd_dev->size / (128 * 32 * 512); |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 1066 | return 0; |
| 1067 | } |
| 1068 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | static int ubd_ioctl(struct inode * inode, struct file * file, |
| 1070 | unsigned int cmd, unsigned long arg) |
| 1071 | { |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1072 | struct ubd *ubd_dev = inode->i_bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | struct hd_driveid ubd_id = { |
| 1074 | .cyls = 0, |
| 1075 | .heads = 128, |
| 1076 | .sectors = 32, |
| 1077 | }; |
| 1078 | |
| 1079 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | struct cdrom_volctrl volume; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | case HDIO_GET_IDENTITY: |
Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1082 | ubd_id.cyls = ubd_dev->size / (128 * 32 * 512); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | if(copy_to_user((char __user *) arg, (char *) &ubd_id, |
| 1084 | sizeof(ubd_id))) |
| 1085 | return(-EFAULT); |
| 1086 | return(0); |
| 1087 | |
| 1088 | case CDROMVOLREAD: |
| 1089 | if(copy_from_user(&volume, (char __user *) arg, sizeof(volume))) |
| 1090 | return(-EFAULT); |
| 1091 | volume.channel0 = 255; |
| 1092 | volume.channel1 = 255; |
| 1093 | volume.channel2 = 255; |
| 1094 | volume.channel3 = 255; |
| 1095 | if(copy_to_user((char __user *) arg, &volume, sizeof(volume))) |
| 1096 | return(-EFAULT); |
| 1097 | return(0); |
| 1098 | } |
| 1099 | return(-EINVAL); |
| 1100 | } |
| 1101 | |
Paolo 'Blaisorblade' Giarrusso | 4833aff | 2006-01-18 17:43:00 -0800 | [diff] [blame] | 1102 | static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | { |
| 1104 | struct uml_stat buf1, buf2; |
| 1105 | int err; |
| 1106 | |
Paolo 'Blaisorblade' Giarrusso | 4833aff | 2006-01-18 17:43:00 -0800 | [diff] [blame] | 1107 | if(from_cmdline == NULL) |
| 1108 | return 0; |
| 1109 | if(!strcmp(from_cmdline, from_cow)) |
| 1110 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | |
| 1112 | err = os_stat_file(from_cmdline, &buf1); |
| 1113 | if(err < 0){ |
| 1114 | printk("Couldn't stat '%s', err = %d\n", from_cmdline, -err); |
Paolo 'Blaisorblade' Giarrusso | 4833aff | 2006-01-18 17:43:00 -0800 | [diff] [blame] | 1115 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | } |
| 1117 | err = os_stat_file(from_cow, &buf2); |
| 1118 | if(err < 0){ |
| 1119 | printk("Couldn't stat '%s', err = %d\n", from_cow, -err); |
Paolo 'Blaisorblade' Giarrusso | 4833aff | 2006-01-18 17:43:00 -0800 | [diff] [blame] | 1120 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | } |
| 1122 | if((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino)) |
Paolo 'Blaisorblade' Giarrusso | 4833aff | 2006-01-18 17:43:00 -0800 | [diff] [blame] | 1123 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | |
| 1125 | printk("Backing file mismatch - \"%s\" requested,\n" |
| 1126 | "\"%s\" specified in COW header of \"%s\"\n", |
| 1127 | from_cmdline, from_cow, cow); |
Paolo 'Blaisorblade' Giarrusso | 4833aff | 2006-01-18 17:43:00 -0800 | [diff] [blame] | 1128 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | static int backing_file_mismatch(char *file, __u64 size, time_t mtime) |
| 1132 | { |
| 1133 | unsigned long modtime; |
Paolo 'Blaisorblade' Giarrusso | fe1db50 | 2006-02-24 13:03:58 -0800 | [diff] [blame] | 1134 | unsigned long long actual; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | int err; |
| 1136 | |
| 1137 | err = os_file_modtime(file, &modtime); |
| 1138 | if(err < 0){ |
| 1139 | printk("Failed to get modification time of backing file " |
| 1140 | "\"%s\", err = %d\n", file, -err); |
| 1141 | return(err); |
| 1142 | } |
| 1143 | |
| 1144 | err = os_file_size(file, &actual); |
| 1145 | if(err < 0){ |
| 1146 | printk("Failed to get size of backing file \"%s\", " |
| 1147 | "err = %d\n", file, -err); |
| 1148 | return(err); |
| 1149 | } |
| 1150 | |
| 1151 | if(actual != size){ |
| 1152 | /*__u64 can be a long on AMD64 and with %lu GCC complains; so |
| 1153 | * the typecast.*/ |
| 1154 | printk("Size mismatch (%llu vs %llu) of COW header vs backing " |
| 1155 | "file\n", (unsigned long long) size, actual); |
| 1156 | return(-EINVAL); |
| 1157 | } |
| 1158 | if(modtime != mtime){ |
| 1159 | printk("mtime mismatch (%ld vs %ld) of COW header vs backing " |
| 1160 | "file\n", mtime, modtime); |
| 1161 | return(-EINVAL); |
| 1162 | } |
| 1163 | return(0); |
| 1164 | } |
| 1165 | |
| 1166 | int read_cow_bitmap(int fd, void *buf, int offset, int len) |
| 1167 | { |
| 1168 | int err; |
| 1169 | |
| 1170 | err = os_seek_file(fd, offset); |
| 1171 | if(err < 0) |
| 1172 | return(err); |
| 1173 | |
| 1174 | err = os_read_file(fd, buf, len); |
| 1175 | if(err < 0) |
| 1176 | return(err); |
| 1177 | |
| 1178 | return(0); |
| 1179 | } |
| 1180 | |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 1181 | int open_ubd_file(char *file, struct openflags *openflags, int shared, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | char **backing_file_out, int *bitmap_offset_out, |
| 1183 | unsigned long *bitmap_len_out, int *data_offset_out, |
| 1184 | int *create_cow_out) |
| 1185 | { |
| 1186 | time_t mtime; |
| 1187 | unsigned long long size; |
| 1188 | __u32 version, align; |
| 1189 | char *backing_file; |
Paolo 'Blaisorblade' Giarrusso | 4833aff | 2006-01-18 17:43:00 -0800 | [diff] [blame] | 1190 | int fd, err, sectorsize, asked_switch, mode = 0644; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | |
| 1192 | fd = os_open_file(file, *openflags, mode); |
Paolo 'Blaisorblade' Giarrusso | a374a48 | 2006-01-18 17:43:01 -0800 | [diff] [blame] | 1193 | if (fd < 0) { |
| 1194 | if ((fd == -ENOENT) && (create_cow_out != NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | *create_cow_out = 1; |
Paolo 'Blaisorblade' Giarrusso | a374a48 | 2006-01-18 17:43:01 -0800 | [diff] [blame] | 1196 | if (!openflags->w || |
| 1197 | ((fd != -EROFS) && (fd != -EACCES))) |
| 1198 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | openflags->w = 0; |
| 1200 | fd = os_open_file(file, *openflags, mode); |
Paolo 'Blaisorblade' Giarrusso | a374a48 | 2006-01-18 17:43:01 -0800 | [diff] [blame] | 1201 | if (fd < 0) |
| 1202 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 1205 | if(shared) |
| 1206 | printk("Not locking \"%s\" on the host\n", file); |
| 1207 | else { |
| 1208 | err = os_lock_file(fd, openflags->w); |
| 1209 | if(err < 0){ |
| 1210 | printk("Failed to lock '%s', err = %d\n", file, -err); |
| 1211 | goto out_close; |
| 1212 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
Andreas Mohr | d6e05ed | 2006-06-26 18:35:02 +0200 | [diff] [blame] | 1215 | /* Successful return case! */ |
Paolo 'Blaisorblade' Giarrusso | a374a48 | 2006-01-18 17:43:01 -0800 | [diff] [blame] | 1216 | if(backing_file_out == NULL) |
| 1217 | return(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | |
| 1219 | err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime, |
| 1220 | &size, §orsize, &align, bitmap_offset_out); |
| 1221 | if(err && (*backing_file_out != NULL)){ |
| 1222 | printk("Failed to read COW header from COW file \"%s\", " |
| 1223 | "errno = %d\n", file, -err); |
| 1224 | goto out_close; |
| 1225 | } |
Paolo 'Blaisorblade' Giarrusso | a374a48 | 2006-01-18 17:43:01 -0800 | [diff] [blame] | 1226 | if(err) |
| 1227 | return(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | |
Paolo 'Blaisorblade' Giarrusso | 4833aff | 2006-01-18 17:43:00 -0800 | [diff] [blame] | 1229 | asked_switch = path_requires_switch(*backing_file_out, backing_file, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | |
Paolo 'Blaisorblade' Giarrusso | 4833aff | 2006-01-18 17:43:00 -0800 | [diff] [blame] | 1231 | /* Allow switching only if no mismatch. */ |
| 1232 | if (asked_switch && !backing_file_mismatch(*backing_file_out, size, mtime)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | printk("Switching backing file to '%s'\n", *backing_file_out); |
| 1234 | err = write_cow_header(file, fd, *backing_file_out, |
| 1235 | sectorsize, align, &size); |
Paolo 'Blaisorblade' Giarrusso | a374a48 | 2006-01-18 17:43:01 -0800 | [diff] [blame] | 1236 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | printk("Switch failed, errno = %d\n", -err); |
Paolo 'Blaisorblade' Giarrusso | 4833aff | 2006-01-18 17:43:00 -0800 | [diff] [blame] | 1238 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | } |
Paolo 'Blaisorblade' Giarrusso | a374a48 | 2006-01-18 17:43:01 -0800 | [diff] [blame] | 1240 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | *backing_file_out = backing_file; |
| 1242 | err = backing_file_mismatch(*backing_file_out, size, mtime); |
Paolo 'Blaisorblade' Giarrusso | a374a48 | 2006-01-18 17:43:01 -0800 | [diff] [blame] | 1243 | if (err) |
| 1244 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | cow_sizes(version, size, sectorsize, align, *bitmap_offset_out, |
| 1248 | bitmap_len_out, data_offset_out); |
| 1249 | |
Paolo 'Blaisorblade' Giarrusso | a374a48 | 2006-01-18 17:43:01 -0800 | [diff] [blame] | 1250 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | out_close: |
| 1252 | os_close_file(fd); |
Paolo 'Blaisorblade' Giarrusso | a374a48 | 2006-01-18 17:43:01 -0800 | [diff] [blame] | 1253 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | int create_cow_file(char *cow_file, char *backing_file, struct openflags flags, |
| 1257 | int sectorsize, int alignment, int *bitmap_offset_out, |
| 1258 | unsigned long *bitmap_len_out, int *data_offset_out) |
| 1259 | { |
| 1260 | int err, fd; |
| 1261 | |
| 1262 | flags.c = 1; |
Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 1263 | fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | if(fd < 0){ |
| 1265 | err = fd; |
| 1266 | printk("Open of COW file '%s' failed, errno = %d\n", cow_file, |
| 1267 | -err); |
| 1268 | goto out; |
| 1269 | } |
| 1270 | |
| 1271 | err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment, |
| 1272 | bitmap_offset_out, bitmap_len_out, |
| 1273 | data_offset_out); |
| 1274 | if(!err) |
| 1275 | return(fd); |
| 1276 | os_close_file(fd); |
| 1277 | out: |
| 1278 | return(err); |
| 1279 | } |
| 1280 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1281 | static int update_bitmap(struct io_thread_req *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | { |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1283 | int n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1285 | if(req->cow_offset == -1) |
| 1286 | return(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1288 | n = os_seek_file(req->fds[1], req->cow_offset); |
| 1289 | if(n < 0){ |
| 1290 | printk("do_io - bitmap lseek failed : err = %d\n", -n); |
| 1291 | return(1); |
| 1292 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1294 | n = os_write_file(req->fds[1], &req->bitmap_words, |
| 1295 | sizeof(req->bitmap_words)); |
| 1296 | if(n != sizeof(req->bitmap_words)){ |
| 1297 | printk("do_io - bitmap update failed, err = %d fd = %d\n", -n, |
| 1298 | req->fds[1]); |
| 1299 | return(1); |
| 1300 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1302 | return(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | } |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1304 | |
| 1305 | void do_io(struct io_thread_req *req) |
| 1306 | { |
| 1307 | char *buf; |
| 1308 | unsigned long len; |
| 1309 | int n, nsectors, start, end, bit; |
| 1310 | int err; |
| 1311 | __u64 off; |
| 1312 | |
| 1313 | nsectors = req->length / req->sectorsize; |
| 1314 | start = 0; |
| 1315 | do { |
| 1316 | bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask); |
| 1317 | end = start; |
| 1318 | while((end < nsectors) && |
| 1319 | (ubd_test_bit(end, (unsigned char *) |
| 1320 | &req->sector_mask) == bit)) |
| 1321 | end++; |
| 1322 | |
| 1323 | off = req->offset + req->offsets[bit] + |
| 1324 | start * req->sectorsize; |
| 1325 | len = (end - start) * req->sectorsize; |
| 1326 | buf = &req->buffer[start * req->sectorsize]; |
| 1327 | |
| 1328 | err = os_seek_file(req->fds[bit], off); |
| 1329 | if(err < 0){ |
| 1330 | printk("do_io - lseek failed : err = %d\n", -err); |
| 1331 | req->error = 1; |
| 1332 | return; |
| 1333 | } |
| 1334 | if(req->op == UBD_READ){ |
| 1335 | n = 0; |
| 1336 | do { |
| 1337 | buf = &buf[n]; |
| 1338 | len -= n; |
| 1339 | n = os_read_file(req->fds[bit], buf, len); |
| 1340 | if (n < 0) { |
| 1341 | printk("do_io - read failed, err = %d " |
| 1342 | "fd = %d\n", -n, req->fds[bit]); |
| 1343 | req->error = 1; |
| 1344 | return; |
| 1345 | } |
| 1346 | } while((n < len) && (n != 0)); |
| 1347 | if (n < len) memset(&buf[n], 0, len - n); |
| 1348 | } else { |
| 1349 | n = os_write_file(req->fds[bit], buf, len); |
| 1350 | if(n != len){ |
| 1351 | printk("do_io - write failed err = %d " |
| 1352 | "fd = %d\n", -n, req->fds[bit]); |
| 1353 | req->error = 1; |
| 1354 | return; |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | start = end; |
| 1359 | } while(start < nsectors); |
| 1360 | |
| 1361 | req->error = update_bitmap(req); |
| 1362 | } |
| 1363 | |
| 1364 | /* Changed in start_io_thread, which is serialized by being called only |
| 1365 | * from ubd_init, which is an initcall. |
| 1366 | */ |
| 1367 | int kernel_fd = -1; |
| 1368 | |
| 1369 | /* Only changed by the io thread */ |
| 1370 | int io_count = 0; |
| 1371 | |
| 1372 | int io_thread(void *arg) |
| 1373 | { |
| 1374 | struct io_thread_req req; |
| 1375 | int n; |
| 1376 | |
| 1377 | ignore_sigwinch_sig(); |
| 1378 | while(1){ |
| 1379 | n = os_read_file(kernel_fd, &req, sizeof(req)); |
| 1380 | if(n != sizeof(req)){ |
| 1381 | if(n < 0) |
| 1382 | printk("io_thread - read failed, fd = %d, " |
| 1383 | "err = %d\n", kernel_fd, -n); |
| 1384 | else { |
| 1385 | printk("io_thread - short read, fd = %d, " |
| 1386 | "length = %d\n", kernel_fd, n); |
| 1387 | } |
| 1388 | continue; |
| 1389 | } |
| 1390 | io_count++; |
| 1391 | do_io(&req); |
| 1392 | n = os_write_file(kernel_fd, &req, sizeof(req)); |
| 1393 | if(n != sizeof(req)) |
| 1394 | printk("io_thread - write failed, fd = %d, err = %d\n", |
| 1395 | kernel_fd, -n); |
| 1396 | } |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1397 | |
Jeff Dike | 1b57e9c | 2006-01-06 00:18:49 -0800 | [diff] [blame] | 1398 | return 0; |
| 1399 | } |