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